From 54f7a2188801c3f82f4354afd442024702bed393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Terenti=C4=87?= Date: Wed, 22 Jan 2025 09:40:30 +0100 Subject: [PATCH] Use resource instead of metric attributes --- client/src/main.rs | 11 ++++++----- core/src/telemetry/otlp.rs | 19 ++++++++++++------- crawler/src/main.rs | 11 ++++++----- fat/src/main.rs | 11 ++++++----- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/client/src/main.rs b/client/src/main.rs index 31e0c3025..d4597a023 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -294,7 +294,7 @@ async fn run( let operating_mode: Mode = cfg.libp2p.kademlia.operation_mode.into(); // construct Metric Attributes and initialize Metrics - let metric_attributes = vec![ + let resource_attributes = vec![ ("version", version.to_string()), ("role", "lightnode".to_string()), ("origin", cfg.origin.to_string()), @@ -302,22 +302,23 @@ async fn run( ("avail_address", identity_cfg.avail_public_key), ("network", Network::name(&cfg.genesis_hash)), ("client_id", client_id.to_string()), - ("execution_id", execution_id.to_string()), ( "client_alias", cfg.client_alias.clone().unwrap_or("".to_string()), ), - (ATTRIBUTE_OPERATING_MODE, operating_mode.to_string()), ]; - let metrics = telemetry::otlp::initialize( + let mut metrics = telemetry::otlp::initialize( cfg.project_name.clone(), &cfg.origin, cfg.otel.clone(), - metric_attributes, + resource_attributes, ) .wrap_err("Unable to initialize OpenTelemetry service")?; + metrics.set_attribute("execution_id", execution_id.to_string()); + metrics.set_attribute(ATTRIBUTE_OPERATING_MODE, operating_mode.to_string()); + let mut state = ClientState::new(metrics); spawn_in_span(shutdown.with_cancel(async move { diff --git a/core/src/telemetry/otlp.rs b/core/src/telemetry/otlp.rs index a1f1e6901..f799b8ce9 100644 --- a/core/src/telemetry/otlp.rs +++ b/core/src/telemetry/otlp.rs @@ -223,9 +223,8 @@ pub fn initialize( project_name: ProjectName, origin: &Origin, ot_config: OtelConfig, - attributes: Vec<(&'static str, String)>, + resource_attributes: Vec<(&'static str, String)>, ) -> Result { - let attributes: Attributes = attributes.into_iter().collect(); let exporter = MetricExporter::builder() .with_tonic() .with_endpoint(&ot_config.ot_collector_endpoint) @@ -238,12 +237,18 @@ pub fn initialize( .with_timeout(Duration::from_secs(ot_config.ot_export_timeout)) // Timeout for each export .build(); + let service_name = KeyValue::new("service.name", project_name.to_string()); + + let mut resource = resource_attributes + .iter() + .map(|(k, v)| KeyValue::new(*k, v.clone())) + .collect::>(); + + resource.push(service_name); + let provider = SdkMeterProvider::builder() .with_reader(reader) - .with_resource(Resource::new(vec![KeyValue::new( - "service.name", - project_name.to_string(), - )])) + .with_resource(Resource::new(resource)) .build(); global::set_meter_provider(provider); @@ -257,6 +262,6 @@ pub fn initialize( counters, u64_gauges, f64_gauges, - attributes: Arc::new(Mutex::new(attributes)), + attributes: Default::default(), }) } diff --git a/crawler/src/main.rs b/crawler/src/main.rs index e944f18d6..9bc9f655e 100644 --- a/crawler/src/main.rs +++ b/crawler/src/main.rs @@ -8,7 +8,7 @@ use avail_light_core::{ shutdown::Controller, telemetry::{ otlp::{self, Metrics}, - MetricCounter, MetricValue, + MetricCounter, MetricValue, ATTRIBUTE_OPERATING_MODE, }, types::{BlockVerified, ProjectName}, utils::{default_subscriber, install_panic_hooks, json_subscriber, spawn_in_span}, @@ -172,7 +172,7 @@ async fn run(config: Config, db: DB, shutdown: Controller) -> Result<()> crawler_sender, ))); - let metric_attributes = vec![ + let resource_attributes = vec![ ("role", "crawler".to_string()), ("origin", config.origin.to_string()), ("version", version.to_string()), @@ -180,17 +180,18 @@ async fn run(config: Config, db: DB, shutdown: Controller) -> Result<()> ("partition_size", partition_size), ("network", Network::name(&config.genesis_hash)), ("client_alias", config.client_alias), - ("operating_mode", "client".to_string()), ]; - let metrics = otlp::initialize( + let mut metrics = otlp::initialize( ProjectName::new("avail".to_string()), &config.origin, config.otel.clone(), - metric_attributes, + resource_attributes, ) .wrap_err("Unable to initialize OpenTelemetry service")?; + metrics.set_attribute(ATTRIBUTE_OPERATING_MODE, "client".to_string()); + let mut state = CrawlerState::new(metrics); spawn_in_span(shutdown.with_cancel(async move { diff --git a/fat/src/main.rs b/fat/src/main.rs index 09b104427..21b4ea831 100644 --- a/fat/src/main.rs +++ b/fat/src/main.rs @@ -8,7 +8,7 @@ use avail_light_core::{ Network, }, shutdown::Controller, - telemetry::{self, otlp::Metrics, MetricCounter, MetricValue}, + telemetry::{self, otlp::Metrics, MetricCounter, MetricValue, ATTRIBUTE_OPERATING_MODE}, types::{BlockVerified, ClientChannels, IdentityConfig, Origin, ProjectName}, utils::{default_subscriber, install_panic_hooks, json_subscriber, spawn_in_span}, }; @@ -194,24 +194,25 @@ async fn run(config: Config, db: DB, shutdown: Controller) -> Result<()> shutdown.clone(), ))); - let metric_attributes = vec![ + let resource_attributes = vec![ ("role", "fat".to_string()), ("origin", Origin::FatClient.to_string()), ("version", version.to_string()), ("peerID", p2p_peer_id.to_string()), ("partition_size", partition_size), ("network", Network::name(&config.genesis_hash)), - ("operating_mode", "client".to_string()), ]; - let metrics = telemetry::otlp::initialize( + let mut metrics = telemetry::otlp::initialize( ProjectName::new("avail".to_string()), &Origin::FatClient, config.otel.clone(), - metric_attributes, + resource_attributes, ) .wrap_err("Unable to initialize OpenTelemetry service")?; + metrics.set_attribute(ATTRIBUTE_OPERATING_MODE, "client".to_string()); + let mut state = FatState::new(metrics); spawn_in_span(shutdown.with_cancel(async move {