-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
Copy pathprom_utils.rs
268 lines (258 loc) · 10.5 KB
/
prom_utils.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Parseable Server (C) 2022 - 2024 Parseable, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use crate::handlers::http::base_path_without_preceding_slash;
use crate::handlers::http::ingest::PostError;
use crate::handlers::http::modal::IngestorMetadata;
use crate::option::Mode;
use crate::parseable::PARSEABLE;
use crate::HTTP_CLIENT;
use actix_web::http::header;
use chrono::NaiveDateTime;
use chrono::Utc;
use prometheus_parse::Sample as PromSample;
use prometheus_parse::Value as PromValue;
use serde::Serialize;
use serde_json::Error as JsonError;
use serde_json::Value as JsonValue;
use tracing::error;
use tracing::warn;
use url::Url;
#[derive(Debug, Serialize, Clone)]
pub struct Metrics {
address: String,
parseable_events_ingested: f64, // all streams
parseable_events_ingested_size: f64,
parseable_lifetime_events_ingested: f64, // all streams
parseable_lifetime_events_ingested_size: f64,
parseable_deleted_events_ingested: f64, // all streams
parseable_deleted_events_ingested_size: f64,
parseable_staging_files: f64,
process_resident_memory_bytes: f64,
parseable_storage_size: StorageMetrics,
parseable_lifetime_storage_size: StorageMetrics,
parseable_deleted_storage_size: StorageMetrics,
event_type: String,
event_time: NaiveDateTime,
commit: String,
staging: String,
}
#[derive(Debug, Serialize, Default, Clone)]
struct StorageMetrics {
staging: f64,
data: f64,
}
impl Default for Metrics {
fn default() -> Self {
// for now it is only for ingestor
let url = PARSEABLE.options.get_url(Mode::Ingest);
let address = format!(
"http://{}:{}",
url.domain()
.unwrap_or(url.host_str().expect("should have a host")),
url.port().unwrap_or_default()
);
Metrics {
address,
parseable_events_ingested: 0.0,
parseable_events_ingested_size: 0.0,
parseable_staging_files: 0.0,
process_resident_memory_bytes: 0.0,
parseable_storage_size: StorageMetrics::default(),
parseable_lifetime_events_ingested: 0.0,
parseable_lifetime_events_ingested_size: 0.0,
parseable_deleted_events_ingested: 0.0,
parseable_deleted_events_ingested_size: 0.0,
parseable_deleted_storage_size: StorageMetrics::default(),
parseable_lifetime_storage_size: StorageMetrics::default(),
event_type: "cluster-metrics".to_string(),
event_time: Utc::now().naive_utc(),
commit: "".to_string(),
staging: "".to_string(),
}
}
}
impl Metrics {
fn new(address: String) -> Self {
Metrics {
address,
parseable_events_ingested: 0.0,
parseable_events_ingested_size: 0.0,
parseable_staging_files: 0.0,
process_resident_memory_bytes: 0.0,
parseable_storage_size: StorageMetrics::default(),
parseable_lifetime_events_ingested: 0.0,
parseable_lifetime_events_ingested_size: 0.0,
parseable_deleted_events_ingested: 0.0,
parseable_deleted_events_ingested_size: 0.0,
parseable_deleted_storage_size: StorageMetrics::default(),
parseable_lifetime_storage_size: StorageMetrics::default(),
event_type: "cluster-metrics".to_string(),
event_time: Utc::now().naive_utc(),
commit: "".to_string(),
staging: "".to_string(),
}
}
}
impl Metrics {
pub fn get_daily_stats_from_samples(
samples: Vec<PromSample>,
stream_name: &str,
date: &str,
) -> (u64, u64, u64) {
let mut events_ingested: u64 = 0;
let mut ingestion_size: u64 = 0;
let mut storage_size: u64 = 0;
for sample in samples {
if let PromValue::Gauge(val) = sample.value {
match sample.metric.as_str() {
"parseable_events_ingested_date" => {
if sample.labels.get("stream").expect("stream name is present")
== stream_name
&& sample.labels.get("date").expect("date is present") == date
{
events_ingested = val as u64;
}
}
"parseable_events_ingested_size_date" => {
if sample.labels.get("stream").expect("stream name is present")
== stream_name
&& sample.labels.get("date").expect("date is present") == date
{
ingestion_size = val as u64;
}
}
"parseable_events_storage_size_date" => {
if sample.labels.get("stream").expect("stream name is present")
== stream_name
&& sample.labels.get("date").expect("date is present") == date
{
storage_size = val as u64;
}
}
_ => {}
}
}
}
(events_ingested, ingestion_size, storage_size)
}
pub async fn from_prometheus_samples(
samples: Vec<PromSample>,
ingestor_metadata: &IngestorMetadata,
) -> Result<Self, PostError> {
let mut prom_dress = Metrics::new(ingestor_metadata.domain_name.to_string());
for sample in samples {
if let PromValue::Gauge(val) = sample.value {
match sample.metric.as_str() {
"parseable_events_ingested" => prom_dress.parseable_events_ingested += val,
"parseable_events_ingested_size" => {
prom_dress.parseable_events_ingested_size += val
}
"parseable_lifetime_events_ingested" => {
prom_dress.parseable_lifetime_events_ingested += val
}
"parseable_lifetime_events_ingested_size" => {
prom_dress.parseable_lifetime_events_ingested_size += val
}
"parseable_events_deleted" => {
prom_dress.parseable_deleted_events_ingested += val
}
"parseable_events_deleted_size" => {
prom_dress.parseable_deleted_events_ingested_size += val
}
"parseable_staging_files" => prom_dress.parseable_staging_files += val,
"process_resident_memory_bytes" => {
prom_dress.process_resident_memory_bytes += val
}
"parseable_storage_size" => {
if sample.labels.get("type").expect("type is present") == "staging" {
prom_dress.parseable_storage_size.staging += val;
}
if sample.labels.get("type").expect("type is present") == "data" {
prom_dress.parseable_storage_size.data += val;
}
}
"parseable_lifetime_events_storage_size" => {
if sample.labels.get("type").expect("type is present") == "data" {
prom_dress.parseable_lifetime_storage_size.data += val;
}
}
"parseable_deleted_events_storage_size" => {
if sample.labels.get("type").expect("type is present") == "data" {
prom_dress.parseable_deleted_storage_size.data += val;
}
}
_ => {}
}
}
}
let (commit_id, staging) = Self::from_about_api_response(ingestor_metadata.clone())
.await
.map_err(|err| {
error!("Fatal: failed to get ingestor info: {:?}", err);
PostError::Invalid(err.into())
})?;
prom_dress.commit = commit_id;
prom_dress.staging = staging;
Ok(prom_dress)
}
pub async fn from_about_api_response(
ingestor_metadata: IngestorMetadata,
) -> Result<(String, String), PostError> {
let uri = Url::parse(&format!(
"{}{}/about",
&ingestor_metadata.domain_name,
base_path_without_preceding_slash()
))
.map_err(|err| {
PostError::Invalid(anyhow::anyhow!("Invalid URL in Ingestor Metadata: {}", err))
})?;
let res = HTTP_CLIENT
.get(uri)
.header(header::CONTENT_TYPE, "application/json")
.header(header::AUTHORIZATION, ingestor_metadata.token)
.send()
.await;
if let Ok(res) = res {
let about_api_json = res.text().await.map_err(PostError::NetworkError)?;
let about_api_json: serde_json::Value =
serde_json::from_str(&about_api_json).map_err(PostError::SerdeError)?;
let commit_id = about_api_json
.get("commit")
.and_then(|x| x.as_str())
.unwrap_or_default();
let staging = about_api_json
.get("staging")
.and_then(|x| x.as_str())
.unwrap_or_default();
Ok((commit_id.to_string(), staging.to_string()))
} else {
warn!(
"Failed to fetch about API response from ingestor: {}\n",
&ingestor_metadata.domain_name,
);
Err(PostError::Invalid(anyhow::anyhow!(
"Failed to fetch about API response from ingestor: {}\n",
&ingestor_metadata.domain_name
)))
}
}
#[allow(unused)]
pub fn to_json(&self) -> Result<JsonValue, JsonError> {
serde_json::to_value(self)
}
}