Skip to content

Commit

Permalink
Upgrade to opentelemetry 0.22
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Feb 26, 2024
1 parent f10aecd commit c2437bc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 86 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions examples/opentelemetry-otlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -128,7 +128,7 @@ fn init_tracing_subscriber() -> OtelGuard {
}

struct OtelGuard {
meter_provider: MeterProvider,
meter_provider: SdkMeterProvider,
}

impl Drop for OtelGuard {
Expand Down
19 changes: 3 additions & 16 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub(crate) struct Instruments {
i64_up_down_counter: MetricsMap<UpDownCounter<i64>>,
f64_up_down_counter: MetricsMap<UpDownCounter<f64>>,
u64_histogram: MetricsMap<Histogram<u64>>,
i64_histogram: MetricsMap<Histogram<i64>>,
f64_histogram: MetricsMap<Histogram<f64>>,
}

Expand All @@ -43,7 +42,6 @@ pub(crate) enum InstrumentType {
UpDownCounterI64(i64),
UpDownCounterF64(f64),
HistogramU64(u64),
HistogramI64(i64),
HistogramF64(f64),
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
///
Expand Down
59 changes: 6 additions & 53 deletions tests/metrics_publishing.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -464,7 +415,9 @@ fn init_subscriber<T>(
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,
Expand Down Expand Up @@ -521,7 +474,7 @@ struct TestExporter<T> {
expected_value: T,
expected_attributes: Option<AttributeSet>,
reader: TestReader,
_meter_provider: MeterProvider,
_meter_provider: SdkMeterProvider,
}

impl<T> TestExporter<T>
Expand Down
18 changes: 15 additions & 3 deletions tests/parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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[..]);
}
Expand All @@ -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[..]);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/trace_state_propagation.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down

0 comments on commit c2437bc

Please sign in to comment.