Skip to content

Commit

Permalink
Use resource instead of metric attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
aterentic-ethernal committed Jan 23, 2025
1 parent 355ae76 commit a7d884c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
11 changes: 6 additions & 5 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,30 +294,31 @@ 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()),
("peerID", peer_id.to_string()),
("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 {
Expand Down
19 changes: 12 additions & 7 deletions core/src/telemetry/otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Metrics> {
let attributes: Attributes = attributes.into_iter().collect();
let exporter = MetricExporter::builder()
.with_tonic()
.with_endpoint(&ot_config.ot_collector_endpoint)
Expand All @@ -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::<Vec<_>>();

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);
Expand All @@ -257,6 +262,6 @@ pub fn initialize(
counters,
u64_gauges,
f64_gauges,
attributes: Arc::new(Mutex::new(attributes)),
attributes: Default::default(),
})
}
11 changes: 6 additions & 5 deletions crawler/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -172,25 +172,26 @@ async fn run(config: Config, db: DB, shutdown: Controller<String>) -> Result<()>
crawler_sender,
)));

let metric_attributes = vec![
let resource_attributes = vec![
("role", "crawler".to_string()),
("origin", config.origin.to_string()),
("version", version.to_string()),
("peerID", p2p_peer_id.to_string()),
("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 {
Expand Down
11 changes: 6 additions & 5 deletions fat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -194,24 +194,25 @@ async fn run(config: Config, db: DB, shutdown: Controller<String>) -> 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 {
Expand Down

0 comments on commit a7d884c

Please sign in to comment.