From c2437bcf2c018af6516457cabb7cb18eb7d1ef9a Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sun, 25 Feb 2024 20:33:31 +0100 Subject: [PATCH] Upgrade to opentelemetry 0.22 --- Cargo.toml | 16 ++++----- examples/opentelemetry-otlp.rs | 8 ++--- src/metrics.rs | 19 ++-------- tests/metrics_publishing.rs | 59 ++++---------------------------- tests/parents.rs | 18 ++++++++-- tests/trace_state_propagation.rs | 4 +-- 6 files changed, 38 insertions(+), 86 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c939a86..c48d26e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ default = ["tracing-log", "metrics"] metrics = ["opentelemetry/metrics","opentelemetry_sdk/metrics", "smallvec"] [dependencies] -opentelemetry = { version = "0.21.0", default-features = false, features = ["trace"] } -opentelemetry_sdk = { version = "0.21.0", default-features = false, features = ["trace"] } +opentelemetry = { version = "0.22.0", default-features = false, features = ["trace"] } +opentelemetry_sdk = { version = "0.22.0", default-features = false, features = ["trace"] } tracing = { version = "0.1.35", default-features = false, features = ["std"] } tracing-core = "0.1.28" tracing-subscriber = { version = "0.3.0", default-features = false, features = ["registry", "std"] } @@ -41,12 +41,12 @@ smallvec = { version = "1.0", optional = true } [dev-dependencies] async-trait = "0.1.56" criterion = { version = "0.5.1", default-features = false, features = ["html_reports"] } -opentelemetry = { version = "0.21.0", features = ["trace", "metrics"] } -opentelemetry_sdk = { version = "0.21.0", default-features = false, features = ["trace", "rt-tokio"] } -opentelemetry-jaeger = "0.20.0" -opentelemetry-stdout = { version = "0.2.0", features = ["trace", "metrics"] } -opentelemetry-otlp = { version = "0.14.0", features = ["metrics"] } -opentelemetry-semantic-conventions = "0.13.0" +opentelemetry = { version = "0.22.0", features = ["trace", "metrics"] } +opentelemetry_sdk = { version = "0.22.0", default-features = false, features = ["trace", "rt-tokio"] } +opentelemetry-jaeger = "0.21.0" +opentelemetry-stdout = { version = "0.3.0", features = ["trace", "metrics"] } +opentelemetry-otlp = { version = "0.15.0", features = ["metrics"] } +opentelemetry-semantic-conventions = "0.14.0" futures-util = { version = "0.3", default-features = false } tokio = { version = "1", features = ["full"] } tokio-stream = "0.1" diff --git a/examples/opentelemetry-otlp.rs b/examples/opentelemetry-otlp.rs index 77eb167..1355570 100644 --- a/examples/opentelemetry-otlp.rs +++ b/examples/opentelemetry-otlp.rs @@ -2,7 +2,7 @@ use opentelemetry::{global, Key, KeyValue}; use opentelemetry_sdk::{ metrics::{ reader::{DefaultAggregationSelector, DefaultTemporalitySelector}, - Aggregation, Instrument, MeterProvider, PeriodicReader, Stream, + Aggregation, Instrument, MeterProviderBuilder, PeriodicReader, SdkMeterProvider, Stream, }, runtime, trace::{BatchConfig, RandomIdGenerator, Sampler, Tracer}, @@ -29,7 +29,7 @@ fn resource() -> Resource { } // Construct MeterProvider for MetricsLayer -fn init_meter_provider() -> MeterProvider { +fn init_meter_provider() -> SdkMeterProvider { let exporter = opentelemetry_otlp::new_exporter() .tonic() .build_metrics_exporter( @@ -78,7 +78,7 @@ fn init_meter_provider() -> MeterProvider { } }; - let meter_provider = MeterProvider::builder() + let meter_provider = MeterProviderBuilder::default() .with_resource(resource()) .with_reader(reader) .with_reader(stdout_reader) @@ -128,7 +128,7 @@ fn init_tracing_subscriber() -> OtelGuard { } struct OtelGuard { - meter_provider: MeterProvider, + meter_provider: SdkMeterProvider, } impl Drop for OtelGuard { diff --git a/src/metrics.rs b/src/metrics.rs index 8a6eae2..5151720 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -30,7 +30,6 @@ pub(crate) struct Instruments { i64_up_down_counter: MetricsMap>, f64_up_down_counter: MetricsMap>, u64_histogram: MetricsMap>, - i64_histogram: MetricsMap>, f64_histogram: MetricsMap>, } @@ -43,7 +42,6 @@ pub(crate) enum InstrumentType { UpDownCounterI64(i64), UpDownCounterF64(f64), HistogramU64(u64), - HistogramI64(i64), HistogramF64(f64), } @@ -119,14 +117,6 @@ impl Instruments { |rec| rec.record(value, attributes), ); } - InstrumentType::HistogramI64(value) => { - update_or_insert( - &self.i64_histogram, - metric_name, - || meter.i64_histogram(metric_name).init(), - |rec| rec.record(value, attributes), - ); - } InstrumentType::HistogramF64(value) => { update_or_insert( &self.f64_histogram, @@ -198,9 +188,6 @@ impl<'a> Visit for MetricVisitor<'a> { } else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_COUNTER) { self.visited_metrics .push((metric_name, InstrumentType::UpDownCounterI64(value))); - } else if let Some(metric_name) = field.name().strip_prefix(METRIC_PREFIX_HISTOGRAM) { - self.visited_metrics - .push((metric_name, InstrumentType::HistogramI64(value))); } else { self.attributes.push(KeyValue::new(field.name(), value)); } @@ -231,12 +218,12 @@ impl<'a> Visit for MetricVisitor<'a> { /// use tracing_opentelemetry::MetricsLayer; /// use tracing_subscriber::layer::SubscriberExt; /// use tracing_subscriber::Registry; -/// # use opentelemetry_sdk::metrics::MeterProvider; +/// # use opentelemetry_sdk::metrics::SdkMeterProvider; /// /// // Constructing a MeterProvider is out-of-scope for the docs here, but there /// // are examples in the opentelemetry repository. See: /// // https://github.com/open-telemetry/opentelemetry-rust/blob/dfeac078ff7853e7dc814778524b93470dfa5c9c/examples/metrics-basic/src/main.rs#L7 -/// # let meter_provider: MeterProvider = unimplemented!(); +/// # let meter_provider: SdkMeterProvider = unimplemented!(); /// /// let opentelemetry_metrics = MetricsLayer::new(meter_provider); /// let subscriber = Registry::default().with(opentelemetry_metrics); @@ -318,7 +305,7 @@ impl<'a> Visit for MetricVisitor<'a> { /// # use tracing::info; /// // adds attributes bar="baz" and qux=2 to the `foo` counter. /// info!(monotonic_counter.foo = 1, bar = "baz", qux = 2); -/// ``` +/// ``` /// /// # Implementation Details /// diff --git a/tests/metrics_publishing.rs b/tests/metrics_publishing.rs index 3a5d32e..f847b58 100644 --- a/tests/metrics_publishing.rs +++ b/tests/metrics_publishing.rs @@ -1,13 +1,12 @@ use opentelemetry::{metrics::MetricsError, KeyValue}; use opentelemetry_sdk::{ metrics::{ - data, - data::{Histogram, Sum}, + data::{self, Histogram, Sum}, reader::{ AggregationSelector, DefaultAggregationSelector, DefaultTemporalitySelector, MetricReader, TemporalitySelector, }, - InstrumentKind, ManualReader, MeterProvider, + InstrumentKind, ManualReader, MeterProviderBuilder, SdkMeterProvider, }, AttributeSet, Resource, }; @@ -132,22 +131,6 @@ async fn u64_histogram_is_exported() { exporter.export().unwrap(); } -#[tokio::test] -async fn i64_histogram_is_exported() { - let (subscriber, exporter) = init_subscriber( - "abcdefg_auenatsou".to_string(), - InstrumentKind::Histogram, - -19_i64, - None, - ); - - tracing::subscriber::with_default(subscriber, || { - tracing::info!(histogram.abcdefg_auenatsou = -19_i64); - }); - - exporter.export().unwrap(); -} - #[tokio::test] async fn f64_histogram_is_exported() { let (subscriber, exporter) = init_subscriber( @@ -324,38 +307,6 @@ async fn u64_histogram_with_attributes_is_exported() { exporter.export().unwrap(); } -#[tokio::test] -async fn i64_histogram_with_attributes_is_exported() { - let (subscriber, exporter) = init_subscriber( - "hello_world".to_string(), - InstrumentKind::Histogram, - -1_i64, - Some(AttributeSet::from( - [ - KeyValue::new("u64_key_1", 1_i64), - KeyValue::new("i64_key_1", 2_i64), - KeyValue::new("f64_key_1", 3_f64), - KeyValue::new("str_key_1", "foo"), - KeyValue::new("bool_key_1", true), - ] - .as_slice(), - )), - ); - - tracing::subscriber::with_default(subscriber, || { - tracing::info!( - histogram.hello_world = -1_i64, - u64_key_1 = 1_u64, - i64_key_1 = 2_i64, - f64_key_1 = 3_f64, - str_key_1 = "foo", - bool_key_1 = true, - ); - }); - - exporter.export().unwrap(); -} - #[tokio::test] async fn f64_histogram_with_attributes_is_exported() { let (subscriber, exporter) = init_subscriber( @@ -464,7 +415,9 @@ fn init_subscriber( inner: Arc::new(reader), }; - let provider = MeterProvider::builder().with_reader(reader.clone()).build(); + let provider = MeterProviderBuilder::default() + .with_reader(reader.clone()) + .build(); let exporter = TestExporter { expected_metric_name, expected_instrument_kind, @@ -521,7 +474,7 @@ struct TestExporter { expected_value: T, expected_attributes: Option, reader: TestReader, - _meter_provider: MeterProvider, + _meter_provider: SdkMeterProvider, } impl TestExporter diff --git a/tests/parents.rs b/tests/parents.rs index 1621ee0..2934d20 100644 --- a/tests/parents.rs +++ b/tests/parents.rs @@ -33,7 +33,11 @@ fn test_tracer() -> (Tracer, TracerProvider, TestExporter, impl Subscriber) { let tracer = provider.tracer("test"); let subscriber = tracing_subscriber::registry() - .with(layer().with_tracer(tracer.clone()).with_filter(LevelFilter::DEBUG)) + .with( + layer() + .with_tracer(tracer.clone()) + .with_filter(LevelFilter::DEBUG), + ) .with(tracing_subscriber::fmt::layer().with_filter(LevelFilter::TRACE)); (tracer, provider, exporter, subscriber) @@ -81,7 +85,11 @@ fn explicit_parents_of_events() { let expected_root_events = ["1", "2", "5", "8", "9", "13"]; let root_span = spans.iter().find(|s| s.name == "root").unwrap(); - let actual_events: Vec<_> = root_span.events.iter().map(|event| event.name.to_string()).collect(); + let actual_events: Vec<_> = root_span + .events + .iter() + .map(|event| event.name.to_string()) + .collect(); assert_eq!(&expected_root_events, &actual_events[..]); } @@ -91,7 +99,11 @@ fn explicit_parents_of_events() { let expected_child_events = ["4", "6", "10", "14"]; let child_span = spans.iter().find(|s| s.name == "child").unwrap(); - let actual_events: Vec<_> = child_span.events.iter().map(|event| event.name.to_string()).collect(); + let actual_events: Vec<_> = child_span + .events + .iter() + .map(|event| event.name.to_string()) + .collect(); assert_eq!(&expected_child_events, &actual_events[..]); } diff --git a/tests/trace_state_propagation.rs b/tests/trace_state_propagation.rs index a020a74..70ba836 100644 --- a/tests/trace_state_propagation.rs +++ b/tests/trace_state_propagation.rs @@ -1,12 +1,12 @@ use futures_util::future::BoxFuture; use opentelemetry::{ - propagation::TextMapPropagator, + propagation::{TextMapCompositePropagator, TextMapPropagator}, trace::{SpanContext, TraceContextExt, Tracer as _, TracerProvider as _}, Context, }; use opentelemetry_sdk::{ export::trace::{ExportResult, SpanData, SpanExporter}, - propagation::{BaggagePropagator, TextMapCompositePropagator, TraceContextPropagator}, + propagation::{BaggagePropagator, TraceContextPropagator}, trace::{Tracer, TracerProvider}, }; use std::collections::{HashMap, HashSet};