Skip to content

Commit cab9fc0

Browse files
fix: add metrics collections in billing metrics (#1500)
1 parent 1f2d3b4 commit cab9fc0

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/handlers/http/cluster/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ struct BillingMetricsCollector {
9999
pub total_bytes_scanned_in_object_store_calls_by_date: HashMap<String, HashMap<String, u64>>,
100100
pub total_input_llm_tokens_by_date: HashMap<String, HashMap<String, HashMap<String, u64>>>, // provider -> model -> date -> count
101101
pub total_output_llm_tokens_by_date: HashMap<String, HashMap<String, HashMap<String, u64>>>,
102+
pub total_metrics_collected_by_date: HashMap<String, u64>,
102103
pub event_time: chrono::NaiveDateTime,
103104
}
104105

@@ -193,6 +194,14 @@ impl BillingMetricsCollector {
193194
&self.total_bytes_scanned_in_query_by_date,
194195
);
195196
}
197+
198+
if !self.total_metrics_collected_by_date.is_empty() {
199+
add_simple_metric(
200+
events,
201+
"total_metrics_collected",
202+
&self.total_metrics_collected_by_date,
203+
);
204+
}
196205
}
197206

198207
/// Add object store metrics (method-based) to the events vector
@@ -1263,6 +1272,7 @@ fn is_simple_metric(metric: &str) -> bool {
12631272
| "parseable_total_query_calls_by_date"
12641273
| "parseable_total_files_scanned_in_query_by_date"
12651274
| "parseable_total_bytes_scanned_in_query_by_date"
1275+
| "parseable_total_metrics_collected_by_date"
12661276
)
12671277
}
12681278

@@ -1329,6 +1339,11 @@ fn process_simple_metric(
13291339
.total_bytes_scanned_in_query_by_date
13301340
.insert(date.to_string(), value);
13311341
}
1342+
"parseable_total_metrics_collected_by_date" => {
1343+
collector
1344+
.total_metrics_collected_by_date
1345+
.insert(date.to_string(), value);
1346+
}
13321347
_ => {}
13331348
}
13341349
}

src/metrics/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,18 @@ pub static STORAGE_REQUEST_RESPONSE_TIME: Lazy<HistogramVec> = Lazy::new(|| {
373373
.expect("metric can be created")
374374
});
375375

376+
pub static TOTAL_METRICS_COLLECTED_BY_DATE: Lazy<IntCounterVec> = Lazy::new(|| {
377+
IntCounterVec::new(
378+
Opts::new(
379+
"total_metrics_collected_by_date",
380+
"Total metrics collected by date",
381+
)
382+
.namespace(METRICS_NAMESPACE),
383+
&["date"],
384+
)
385+
.expect("metric can be created")
386+
});
387+
376388
fn custom_metrics(registry: &Registry) {
377389
registry
378390
.register(Box::new(EVENTS_INGESTED.clone()))
@@ -472,6 +484,9 @@ fn custom_metrics(registry: &Registry) {
472484
registry
473485
.register(Box::new(STORAGE_REQUEST_RESPONSE_TIME.clone()))
474486
.expect("metric can be registered");
487+
registry
488+
.register(Box::new(TOTAL_METRICS_COLLECTED_BY_DATE.clone()))
489+
.expect("metric can be registered");
475490
}
476491

477492
pub fn build_metrics_handler() -> PrometheusMetrics {
@@ -619,6 +634,12 @@ pub fn increment_reasoning_llm_tokens_by_date(
619634
.inc_by(tokens);
620635
}
621636

637+
pub fn increment_metrics_collected_by_date(date: &str) {
638+
TOTAL_METRICS_COLLECTED_BY_DATE
639+
.with_label_values(&[date])
640+
.inc();
641+
}
642+
622643
use actix_web::HttpResponse;
623644
use prometheus::Encoder;
624645

src/otel/metrics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use opentelemetry_proto::tonic::metrics::v1::{
2424
};
2525
use serde_json::{Map, Value};
2626

27+
use crate::metrics::increment_metrics_collected_by_date;
2728
use crate::otel::otel_utils::flatten_attributes;
2829

2930
use super::otel_utils::{
@@ -500,6 +501,9 @@ pub fn flatten_metrics_record(metrics_record: &Metric) -> Vec<Map<String, Value>
500501
if data_points_json.is_empty() {
501502
data_points_json.push(metric_json);
502503
}
504+
505+
let current_date = chrono::Utc::now().date_naive().to_string();
506+
increment_metrics_collected_by_date(&current_date);
503507
data_points_json
504508
}
505509

0 commit comments

Comments
 (0)