@@ -9,20 +9,20 @@ use prometheus_client::{
99use serde_json:: json;
1010use serde_json:: Value ;
1111use std:: env;
12- use std:: sync:: Arc ;
12+ use std:: sync:: { Arc , LazyLock } ;
1313use std:: { path:: PathBuf , time:: Duration } ;
1414use tokio:: time;
1515
16+ use crate :: utilities:: constants:: { CLI_VERSION , CONTEXT , CTX_SESSION_ID } ;
17+ use crate :: utilities:: decode_object;
1618use chrono:: Utc ;
19+ use log:: warn;
1720
18- use crate :: utilities:: constants:: { self , CONTEXT , CTX_SESSION_ID } ;
19- use crate :: utilities:: decode_object;
2021const DEFAULT_ANONYMOUS_METRICS_URL : & str =
2122 "https://moosefood.514.dev/ingest/MooseSessionTelemetry/0.6" ;
22- lazy_static:: lazy_static! {
23- static ref ANONYMOUS_METRICS_URL : String = env:: var( "MOOSE_METRICS_DEST" )
24- . unwrap_or_else( |_| DEFAULT_ANONYMOUS_METRICS_URL . to_string( ) ) ;
25- }
23+ static ANONYMOUS_METRICS_URL : LazyLock < String > = LazyLock :: new ( || {
24+ env:: var ( "MOOSE_METRICS_DEST" ) . unwrap_or_else ( |_| DEFAULT_ANONYMOUS_METRICS_URL . to_string ( ) )
25+ } ) ;
2626const ANONYMOUS_METRICS_REPORTING_INTERVAL : Duration = Duration :: from_secs ( 10 ) ;
2727pub const TOTAL_LATENCY : & str = "moose_total_latency" ;
2828pub const LATENCY : & str = "moose_latency" ;
@@ -314,7 +314,7 @@ impl Metrics {
314314 while let Some ( message) = rx. recv ( ) . await {
315315 match message {
316316 MetricsMessage :: GetMetricsRegistryAsString ( v) => {
317- let _ = v. send ( formatted_registry ( & registry) . await ) ;
317+ let _ = v. send ( formatted_registry ( & registry) ) ;
318318 }
319319 MetricsMessage :: HTTPLatency {
320320 path,
@@ -444,19 +444,26 @@ impl Metrics {
444444 }
445445 } ) ;
446446
447- let cloned_metadata = self . telemetry_metadata . clone ( ) ;
448-
449- let metric_labels = match cloned_metadata. metric_labels {
450- Some ( labels) => match decode_object:: decode_base64_to_json ( labels. as_str ( ) ) {
451- Ok ( decoded) => decoded,
452- Err ( e) => {
453- log:: warn!( "Failed to decode metric labels: {:?}" , e) ;
454- serde_json:: Value :: Null
455- }
456- } ,
457- None => serde_json:: Value :: Null ,
447+ let metric_labels = match self
448+ . telemetry_metadata
449+ . metric_labels
450+ . as_deref ( )
451+ . map ( decode_object:: decode_base64_to_json)
452+ {
453+ None => None ,
454+ Some ( Ok ( Value :: Object ( map) ) ) => Some ( map) ,
455+ Some ( Ok ( v) ) => {
456+ warn ! ( "Unexpected JSON value for metric_labels {}" , v) ;
457+ None
458+ }
459+ Some ( Err ( e) ) => {
460+ warn ! ( "Invalid JSON for metric_labels {}" , e) ;
461+ None
462+ }
458463 } ;
459464
465+ let cloned_metadata = self . telemetry_metadata . clone ( ) ;
466+
460467 if self . telemetry_metadata . anonymous_telemetry_enabled {
461468 tokio:: spawn ( async move {
462469 let client = reqwest:: Client :: new ( ) ;
@@ -506,7 +513,7 @@ impl Metrics {
506513 "project" : cloned_metadata. project_name. clone( ) ,
507514 "isProd" : cloned_metadata. is_production. clone( ) ,
508515 "isMooseDeveloper" : cloned_metadata. is_moose_developer. clone( ) ,
509- "cliVersion" : constants :: CLI_VERSION ,
516+ "cliVersion" : CLI_VERSION ,
510517 "sessionDurationInSec" : session_duration_in_sec,
511518 "ingestedEventsCount" : cloned_data_ref. http_ingested_request_count. get( ) ,
512519 "ingestedEventsTotalBytes" : cloned_data_ref. http_ingested_total_bytes. get( ) ,
@@ -523,10 +530,9 @@ impl Metrics {
523530 } ) ;
524531
525532 // Merge metric_labels into telemetry_payload
526- if let Some ( payload_obj) = telemetry_payload. as_object_mut ( ) {
527- if let Value :: Object ( labels_obj) = metric_labels. clone ( ) {
528- payload_obj. extend ( labels_obj) ;
529- }
533+ let payload_obj = telemetry_payload. as_object_mut ( ) . unwrap ( ) ;
534+ if let Some ( labels_obj) = & metric_labels {
535+ payload_obj. extend ( labels_obj. iter ( ) . map ( |( k, v) | ( k. clone ( ) , v. clone ( ) ) ) ) ;
530536 }
531537
532538 let _ = client
@@ -540,7 +546,7 @@ impl Metrics {
540546 }
541547}
542548
543- pub async fn formatted_registry ( data : & Registry ) -> String {
549+ fn formatted_registry ( data : & Registry ) -> String {
544550 let mut buffer = String :: new ( ) ;
545551 let _ = encode ( & mut buffer, data) ;
546552 buffer
0 commit comments