From 642fbed0798b9dfa15c442bd34899a6dbe9e091d Mon Sep 17 00:00:00 2001 From: saying121 Date: Wed, 4 Dec 2024 09:52:29 +0800 Subject: [PATCH] chore(opentelemetry): update to 0.27 --- Cargo.lock | 27 ++++++++++++++------------- Cargo.toml | 6 +++--- src/otlp/config.rs | 8 ++++---- src/otlp/gauges/disk.rs | 2 +- src/otlp/gauges/interrupt.rs | 2 +- src/otlp/gauges/memory.rs | 2 +- src/otlp/gauges/network.rs | 2 +- src/otlp/mod.rs | 32 +++++++++++++++----------------- 8 files changed, 40 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4f3bcd6..c37a77c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1985,23 +1985,23 @@ dependencies = [ [[package]] name = "opentelemetry" -version = "0.25.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "803801d3d3b71cd026851a53f974ea03df3d179cb758b260136a6c9e22e196af" +checksum = "ab70038c28ed37b97d8ed414b6429d343a8bbf44c9f79ec854f3a643029ba6d7" dependencies = [ "futures-core", "futures-sink", "js-sys", - "once_cell", "pin-project-lite", "thiserror", + "tracing", ] [[package]] name = "opentelemetry-http" -version = "0.25.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "88d8c2b76e5f7848a289aa9666dbe56b16f8a22a4c5246ef37a14941818d2913" +checksum = "10a8a7f5f6ba7c1b286c2fbca0454eaba116f63bbe69ed250b642d36fbb04d80" dependencies = [ "async-trait", "bytes", @@ -2012,9 +2012,9 @@ dependencies = [ [[package]] name = "opentelemetry-otlp" -version = "0.25.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "596b1719b3cab83addb20bcbffdf21575279d9436d9ccccfe651a3bf0ab5ab06" +checksum = "91cf61a1868dacc576bf2b2a1c3e9ab150af7272909e80085c3173384fe11f76" dependencies = [ "async-trait", "futures-core", @@ -2028,13 +2028,14 @@ dependencies = [ "thiserror", "tokio", "tonic", + "tracing", ] [[package]] name = "opentelemetry-proto" -version = "0.25.0" +version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c43620e8f93359eb7e627a3b16ee92d8585774986f24f2ab010817426c5ce61" +checksum = "a6e05acbfada5ec79023c85368af14abd0b307c015e9064d249b2a950ef459a6" dependencies = [ "opentelemetry", "opentelemetry_sdk", @@ -2044,16 +2045,15 @@ dependencies = [ [[package]] name = "opentelemetry_sdk" -version = "0.25.0" +version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0da0d6b47a3dbc6e9c9e36a0520e25cf943e046843818faaa3f87365a548c82" +checksum = "231e9d6ceef9b0b2546ddf52335785ce41252bc7474ee8ba05bfad277be13ab8" dependencies = [ "async-trait", "futures-channel", "futures-executor", "futures-util", "glob", - "once_cell", "opentelemetry", "opentelemetry-http", "percent-encoding", @@ -2062,6 +2062,7 @@ dependencies = [ "thiserror", "tokio", "tokio-stream", + "tracing", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 02bc17e..1a08100 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,9 +62,9 @@ libc = "^0.2" chrono = "^0.4" nix = "^0.29" clap = "^4" -opentelemetry-otlp = "^0.25" -opentelemetry = "^0.25" -opentelemetry_sdk = "^0.25" +opentelemetry-otlp = "^0.27" +opentelemetry = "^0.27" +opentelemetry_sdk = "^0.27" rinfluxdb = "0.2.0" tonic = "^0.12" tonic-build = "0.12" diff --git a/src/otlp/config.rs b/src/otlp/config.rs index 1cabbbd..c919c1a 100644 --- a/src/otlp/config.rs +++ b/src/otlp/config.rs @@ -56,7 +56,7 @@ impl From for ExportConfig { panic!("Not support protocol, just support Grpc, HttpBinary, HttpJson") }; Self { - endpoint: val.endpoint, + endpoint: Some(val.endpoint), protocol, timeout: val.timeout, } @@ -74,7 +74,7 @@ impl From<&OtlpConfig> for ExportConfig { panic!("Not support protocol, just support Grpc, HttpBinary, HttpJson") }; Self { - endpoint: val.endpoint.clone(), + endpoint: Some(val.endpoint.clone()), protocol, timeout: val.timeout, } @@ -154,7 +154,7 @@ nanos = 0 "Grpc".to_owned(), ); let export_config = ExportConfig { - endpoint: "http://localhost:4317".to_owned(), + endpoint: Some("http://localhost:4317".to_owned()), timeout: Duration::from_secs(3), protocol: Protocol::Grpc, }; @@ -167,7 +167,7 @@ nanos = 0 "HttpJson".to_owned(), ); let export_config = ExportConfig { - endpoint: "http://localhost:7878".to_owned(), + endpoint: Some("http://localhost:7878".to_owned()), timeout: Duration::new(2, 20), protocol: Protocol::HttpJson, }; diff --git a/src/otlp/gauges/disk.rs b/src/otlp/gauges/disk.rs index c37d3c9..112d2a8 100644 --- a/src/otlp/gauges/disk.rs +++ b/src/otlp/gauges/disk.rs @@ -166,6 +166,6 @@ pub fn start( }); } }) - .try_init()?; + .build(); Ok(gauge) } diff --git a/src/otlp/gauges/interrupt.rs b/src/otlp/gauges/interrupt.rs index 043912a..844d41b 100644 --- a/src/otlp/gauges/interrupt.rs +++ b/src/otlp/gauges/interrupt.rs @@ -47,6 +47,6 @@ pub fn start( } } }) - .try_init()?; + .build(); Ok(gauge) } diff --git a/src/otlp/gauges/memory.rs b/src/otlp/gauges/memory.rs index a92281b..ae3c25a 100644 --- a/src/otlp/gauges/memory.rs +++ b/src/otlp/gauges/memory.rs @@ -137,6 +137,6 @@ pub fn start( gauge.observe(m, &[KeyValue::new("token", token.clone()), kv]); }) }) - .try_init()?; + .build(); Ok(gauge) } diff --git a/src/otlp/gauges/network.rs b/src/otlp/gauges/network.rs index ddf1387..f843b5a 100644 --- a/src/otlp/gauges/network.rs +++ b/src/otlp/gauges/network.rs @@ -159,6 +159,6 @@ pub fn start( }) } }) - .try_init()?; + .build(); Ok(gauge) } diff --git a/src/otlp/mod.rs b/src/otlp/mod.rs index 6764128..17829f7 100644 --- a/src/otlp/mod.rs +++ b/src/otlp/mod.rs @@ -19,12 +19,9 @@ use std::time::Duration; use anyhow::Result; use opentelemetry::{metrics::MeterProvider, KeyValue}; -use opentelemetry_otlp::{ExportConfig, WithExportConfig}; +use opentelemetry_otlp::{ExportConfig, MetricExporter, WithExportConfig, WithTonicConfig}; use opentelemetry_sdk::{ - metrics::{ - reader::{DefaultAggregationSelector, DefaultTemporalitySelector}, - SdkMeterProvider, - }, + metrics::{PeriodicReader, SdkMeterProvider}, runtime, Resource, }; use tonic::{metadata::MetadataMap, transport::ClientTlsConfig}; @@ -32,22 +29,23 @@ use tonic::{metadata::MetadataMap, transport::ClientTlsConfig}; pub fn meter_provider(export_config: ExportConfig, token: String) -> Result { let mut meta = MetadataMap::new(); meta.insert("authorization", format!("Bearer {}", token).parse()?); - let otlp_exporter = opentelemetry_otlp::new_exporter() - .tonic() + let otlp_exporter = MetricExporter::builder() + .with_tonic() .with_tls_config(ClientTlsConfig::new().with_native_roots()) .with_metadata(meta) - .with_export_config(export_config); + .with_timeout(Duration::from_secs(10)) + .with_export_config(export_config) + .build()?; + let reader = PeriodicReader::builder(otlp_exporter, runtime::Tokio) + .with_interval(Duration::from_secs(1)) + .build(); - opentelemetry_otlp::new_pipeline() - .metrics(runtime::Tokio) - .with_exporter(otlp_exporter) + let a = SdkMeterProvider::builder() + .with_reader(reader) .with_resource(Resource::new(vec![KeyValue::new("service.name", "PSH")])) - .with_period(Duration::from_secs(1)) - .with_timeout(Duration::from_secs(10)) - .with_aggregation_selector(DefaultAggregationSelector::new()) - .with_temporality_selector(DefaultTemporalitySelector::new()) - .build() - .map_err(Into::into) + .build(); + + Ok(a) } pub async fn otlp_tasks(export_config: ExportConfig, token: String) -> anyhow::Result<()> {