Skip to content

Upgrade to otel 0.30 #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: v0.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Unreleased

### Breaking Changes

- Upgrade from opentelemetry 0.29.0 to 0.30.0. Refer to the upstream
[changelog](https://github.com/open-telemetry/opentelemetry-rust/blob/main/opentelemetry-sdk/CHANGELOG.md#0300)
for more information.

# 0.31.0 (May 23, 2025)

### Added

- Add `OpenTelemetrySpanExt::add_event` and `OpenTelemetrySpanExt::add_event_with_timestamp`
Expand Down
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tracing-opentelemetry"
version = "0.30.0"
version = "0.31.0"
description = "OpenTelemetry integration for tracing"
homepage = "https://github.com/tokio-rs/tracing-opentelemetry"
repository = "https://github.com/tokio-rs/tracing-opentelemetry"
Expand All @@ -23,8 +23,8 @@ metrics = ["opentelemetry/metrics","opentelemetry_sdk/metrics", "smallvec"]
metrics_gauge_unstable = []

[dependencies]
opentelemetry = { version = "0.29.0", default-features = false, features = ["trace"] }
opentelemetry_sdk = { version = "0.29.0", default-features = false, features = ["trace"] }
opentelemetry = { version = "0.30.0", default-features = false, features = ["trace"] }
opentelemetry_sdk = { version = "0.30.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"] }
Expand All @@ -43,11 +43,11 @@ 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.29.0", features = ["trace", "metrics"] }
opentelemetry_sdk = { version = "0.29.0", default-features = false, features = ["trace", "rt-tokio"] }
opentelemetry-stdout = { version = "0.29.0", features = ["trace", "metrics"] }
opentelemetry-otlp = { version = "0.29.0", features = ["metrics", "grpc-tonic"] }
opentelemetry-semantic-conventions = { version = "0.29.0", features = ["semconv_experimental"] }
opentelemetry = { version = "0.30.0", features = ["trace", "metrics"] }
opentelemetry_sdk = { version = "0.30.0", default-features = false, features = ["trace", "rt-tokio", "experimental_metrics_custom_reader"] }
opentelemetry-stdout = { version = "0.30.0", features = ["trace", "metrics"] }
opentelemetry-otlp = { version = "0.30.0", features = ["metrics", "grpc-tonic"] }
opentelemetry-semantic-conventions = { version = "0.30.0", features = ["semconv_experimental"] }
futures-util = { version = "0.3.17", default-features = false }
tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
Expand Down
103 changes: 65 additions & 38 deletions tests/metrics_publishing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use opentelemetry_sdk::{
metrics::{
data::{self, Gauge, Histogram, Sum},
reader::MetricReader,
InstrumentKind, ManualReader, MeterProviderBuilder, MetricError, SdkMeterProvider,
InstrumentKind, ManualReader, MeterProviderBuilder, SdkMeterProvider,
},
Resource,
};

use std::{fmt::Debug, sync::Arc};
Expand Down Expand Up @@ -548,10 +547,7 @@ impl MetricReader for TestReader {
self.inner.register_pipeline(pipeline);
}

fn collect(
&self,
rm: &mut data::ResourceMetrics,
) -> opentelemetry_sdk::metrics::MetricResult<()> {
fn collect(&self, rm: &mut data::ResourceMetrics) -> OTelSdkResult {
self.inner.collect(rm)
}

Expand All @@ -566,6 +562,10 @@ impl MetricReader for TestReader {
fn temporality(&self, kind: InstrumentKind) -> opentelemetry_sdk::metrics::Temporality {
self.inner.temporality(kind)
}

fn shutdown_with_timeout(&self, timeout: std::time::Duration) -> OTelSdkResult {
self.inner.shutdown_with_timeout(timeout)
}
}

struct TestExporter<T> {
Expand All @@ -577,78 +577,105 @@ struct TestExporter<T> {
_meter_provider: SdkMeterProvider,
}

trait AsAny {
fn as_any(&self) -> &dyn std::any::Any;
}

impl AsAny for data::AggregatedMetrics {
fn as_any(&self) -> &dyn std::any::Any {
match self {
data::AggregatedMetrics::F64(x) => match x {
data::MetricData::Gauge(x) => x as &dyn std::any::Any,
data::MetricData::Sum(x) => x as &dyn std::any::Any,
data::MetricData::Histogram(x) => x as &dyn std::any::Any,
data::MetricData::ExponentialHistogram(x) => x as &dyn std::any::Any,
},
data::AggregatedMetrics::U64(x) => match x {
data::MetricData::Gauge(x) => x as &dyn std::any::Any,
data::MetricData::Sum(x) => x as &dyn std::any::Any,
data::MetricData::Histogram(x) => x as &dyn std::any::Any,
data::MetricData::ExponentialHistogram(x) => x as &dyn std::any::Any,
},
data::AggregatedMetrics::I64(x) => match x {
data::MetricData::Gauge(x) => x as &dyn std::any::Any,
data::MetricData::Sum(x) => x as &dyn std::any::Any,
data::MetricData::Histogram(x) => x as &dyn std::any::Any,
data::MetricData::ExponentialHistogram(x) => x as &dyn std::any::Any,
},
}
}
}

impl<T> TestExporter<T>
where
T: Debug + PartialEq + Copy + std::iter::Sum + 'static,
{
fn export(&self) -> Result<(), MetricError> {
let mut rm = data::ResourceMetrics {
resource: Resource::builder().build(),
scope_metrics: Vec::new(),
};
fn export(&self) -> OTelSdkResult {
let mut rm = data::ResourceMetrics::default();
self.reader.collect(&mut rm)?;

assert!(!rm.scope_metrics.is_empty());
let mut scope_metrics = rm.scope_metrics().peekable();

assert!(scope_metrics.peek().is_some());

rm.scope_metrics.into_iter().for_each(|scope_metrics| {
assert_eq!(scope_metrics.scope.name(), INSTRUMENTATION_LIBRARY_NAME);
assert_eq!(scope_metrics.scope.version().unwrap(), CARGO_PKG_VERSION);
scope_metrics.for_each(|scope_metrics| {
assert_eq!(scope_metrics.scope().name(), INSTRUMENTATION_LIBRARY_NAME);
assert_eq!(scope_metrics.scope().version().unwrap(), CARGO_PKG_VERSION);

scope_metrics.metrics.into_iter().for_each(|metric| {
assert_eq!(metric.name, self.expected_metric_name);
scope_metrics.metrics().for_each(|metric| {
assert_eq!(metric.name(), self.expected_metric_name);

match self.expected_instrument_kind {
InstrumentKind::Counter | InstrumentKind::UpDownCounter => {
let sum = metric.data.as_any().downcast_ref::<Sum<T>>().unwrap();
let sum = metric.data().as_any().downcast_ref::<Sum<T>>().unwrap();
assert_eq!(
self.expected_value,
sum.data_points
.iter()
.map(|data_point| data_point.value)
.sum()
sum.data_points().map(|data_point| data_point.value()).sum()
);

if let Some(expected_attributes) = self.expected_attributes.as_ref() {
sum.data_points.iter().for_each(|data_point| {
sum.data_points().for_each(|data_point| {
assert!(compare_attributes(
expected_attributes,
&data_point.attributes,
data_point.attributes().cloned().collect(),
))
});
}
}
InstrumentKind::Gauge => {
let gauge = metric.data.as_any().downcast_ref::<Gauge<T>>().unwrap();
let gauge = metric.data().as_any().downcast_ref::<Gauge<T>>().unwrap();
assert_eq!(
self.expected_value,
gauge
.data_points
.iter()
.map(|data_point| data_point.value)
.next_back()
.data_points()
.map(|data_point| data_point.value())
.last()
.unwrap()
);

if let Some(expected_attributes) = self.expected_attributes.as_ref() {
gauge.data_points.iter().for_each(|data_point| {
gauge.data_points().for_each(|data_point| {
assert!(compare_attributes(
expected_attributes,
&data_point.attributes,
data_point.attributes().cloned().collect(),
))
});
}
}
InstrumentKind::Histogram => {
let histogram =
metric.data.as_any().downcast_ref::<Histogram<T>>().unwrap();
let histogram_data = histogram.data_points.first().unwrap();
assert!(histogram_data.count > 0);
assert_eq!(histogram_data.sum, self.expected_value);
let histogram = metric
.data()
.as_any()
.downcast_ref::<Histogram<T>>()
.unwrap();
let histogram_data = histogram.data_points().next().unwrap();
assert!(histogram_data.count() > 0);
assert_eq!(histogram_data.sum(), self.expected_value);

if let Some(expected_attributes) = self.expected_attributes.as_ref() {
assert!(compare_attributes(
expected_attributes,
&histogram_data.attributes
histogram_data.attributes().cloned().collect(),
))
}
}
Expand All @@ -666,7 +693,7 @@ where
// After sorting the KeyValue vec, compare them.
// Return true if they are equal.
#[allow(clippy::ptr_arg)]
fn compare_attributes(expected: &Vec<KeyValue>, actual: &Vec<KeyValue>) -> bool {
fn compare_attributes(expected: &Vec<KeyValue>, actual: Vec<KeyValue>) -> bool {
let mut expected = expected.clone();
let mut actual = actual.clone();

Expand Down