diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 97bd292..71d3072 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,7 @@ jobs: - run: cargo test --features=prometheus-exporter,prometheus-0_13 - run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing - run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_20 + - run: cargo test --features=prometheus-exporter,prometheus-client-0_21,exemplars-tracing-opentelemetry-0_21 - run: cargo test --features=prometheus-exporter,opentelemetry-0_20 # Build the crate using the other optional features diff --git a/autometrics/Cargo.toml b/autometrics/Cargo.toml index 408b40a..db32c9a 100644 --- a/autometrics/Cargo.toml +++ b/autometrics/Cargo.toml @@ -73,7 +73,12 @@ exemplars-tracing = ["tracing", "tracing-subscriber"] exemplars-tracing-opentelemetry-0_20 = [ "opentelemetry_api/trace", "tracing", - "tracing-opentelemetry", + "dep:tracing-opentelemetry-0-20", +] +exemplars-tracing-opentelemetry-0_21 = [ + "opentelemetry_api/trace", + "tracing", + "dep:tracing-opentelemetry-0-21", ] # Custom objectives @@ -113,7 +118,8 @@ tracing-subscriber = { version = "0.3", default-features = false, features = [ ], optional = true } # Used for exemplars-tracing-opentelemetry feature -tracing-opentelemetry = { version = "0.20", default-features = false, optional = true } +tracing-opentelemetry-0-20 = { package = "tracing-opentelemetry", version = "0.20.0", default-features = false, optional = true } +tracing-opentelemetry-0-21 = { package = "tracing-opentelemetry", version = "0.21.0", default-features = false, optional = true } [dev-dependencies] axum = { version = "0.6", features = ["tokio"] } @@ -124,7 +130,6 @@ opentelemetry-stdout = { version = "0.1", features = ["trace"] } prometheus-client = "0.21" tokio = { version = "1", features = ["full"] } tracing = "0.1" -tracing-opentelemetry = "0.20" tracing-subscriber = "0.3" trybuild = "1.0" uuid = { version = "1", features = ["v4"] } diff --git a/autometrics/build.rs b/autometrics/build.rs index 9917a0b..17ae3d0 100644 --- a/autometrics/build.rs +++ b/autometrics/build.rs @@ -12,7 +12,7 @@ pub fn main() { #[cfg(feature = "prometheus-client")] println!("cargo:warning=The `prometheus-client` feature is deprecated and will be removed in the next version. Please use `prometheus-client-0_21` instead."); #[cfg(feature = "exemplars-tracing-opentelemetry")] - println!("cargo:warning=The `exemplars-tracing-opentelemetry` feature is deprecated and will be removed in the next version. Please use `exemplars-tracing-opentelemetry-0_20` instead."); + println!("cargo:warning=The `exemplars-tracing-opentelemetry` feature is deprecated and will be removed in the next version. Please use `exemplars-tracing-opentelemetry-0_20` or `exemplars-tracing-opentelemetry-0_21` instead."); cfg_aliases! { // Backends @@ -32,7 +32,7 @@ pub fn main() { // Exemplars exemplars: { any(exemplars_tracing, exemplars_tracing_opentelemetry) }, exemplars_tracing: { feature = "exemplars-tracing" }, - exemplars_tracing_opentelemetry: { any(feature = "exemplars-tracing-opentelemetry-0_20", feature = "exemplars-tracing-opentelemetry") }, + exemplars_tracing_opentelemetry: { any(feature = "exemplars-tracing-opentelemetry-0_20", feature = "exemplars-tracing-opentelemetry-0_21", feature = "exemplars-tracing-opentelemetry") }, // Custom objectives custom_objective_percentile: { feature = "custom-objective-percentile" }, diff --git a/autometrics/src/README.md b/autometrics/src/README.md index d163d0c..fb72294 100644 --- a/autometrics/src/README.md +++ b/autometrics/src/README.md @@ -158,7 +158,7 @@ If you require more customization than these offered feature flags, enable just See the [exemplars module docs](https://docs.rs/autometrics/latest/autometrics/exemplars/index.html) for details about these features. Currently only supported with the `prometheus-client` backend. - `exemplars-tracing` - extract arbitrary fields from `tracing::Span`s -- `exemplars-tracing-opentelemetry-0_20` - extract the `trace_id` and `span_id` from the `opentelemetry::Context`, which is attached to `tracing::Span`s by the `tracing-opentelemetry` crate +- `exemplars-tracing-opentelemetry-0_20` or `exemplars-tracing-opentelemetry-0_21` - extract the `trace_id` and `span_id` from the `opentelemetry::Context`, which is attached to `tracing::Span`s by the `tracing-opentelemetry` crate ### Custom objective values diff --git a/autometrics/src/exemplars/tracing_opentelemetry.rs b/autometrics/src/exemplars/tracing_opentelemetry.rs index 07ca105..9e7b314 100644 --- a/autometrics/src/exemplars/tracing_opentelemetry.rs +++ b/autometrics/src/exemplars/tracing_opentelemetry.rs @@ -2,11 +2,22 @@ use super::TraceLabels; use opentelemetry_api::trace::TraceContextExt; use std::iter::FromIterator; use tracing::Span; -use tracing_opentelemetry::OpenTelemetrySpanExt; + +#[cfg(all( + not(doc), + all( + feature = "exemplars-tracing-opentelemetry-0_20", + feature = "exemplars-tracing-opentelemetry-0_21" + ) +))] +compile_error!("Only one of the `exemplars-tracing-opentelemetry-0_20` and `exemplars-tracing-opentelemetry-0_21` features can be enabled at a time"); pub fn get_exemplar() -> Option { // Get the OpenTelemetry Context from the tracing span - let context = Span::current().context(); + #[cfg(feature = "exemplars-tracing-opentelemetry-0_20")] + let context = tracing_opentelemetry_0_20::OpenTelemetrySpanExt::context(&Span::current()); + #[cfg(feature = "exemplars-tracing-opentelemetry-0_21")] + let context = tracing_opentelemetry_0_21::OpenTelemetrySpanExt::context(&Span::current()); // Now get the OpenTelemetry "span" from the Context // (it's confusing because the word "span" is used by both tracing and OpenTelemetry diff --git a/autometrics/src/lib.rs b/autometrics/src/lib.rs index 46261c2..12623af 100644 --- a/autometrics/src/lib.rs +++ b/autometrics/src/lib.rs @@ -10,6 +10,7 @@ mod constants; feature = "exemplars-tracing", feature = "exemplars-tracing-opentelemetry", feature = "exemplars-tracing-opentelemetry-0_20", + feature = "exemplars-tracing-opentelemetry-0_21", ))] pub mod exemplars; mod labels; diff --git a/autometrics/tests/exemplars_test.rs b/autometrics/tests/exemplars_test.rs index 655f7fe..a32e5b1 100644 --- a/autometrics/tests/exemplars_test.rs +++ b/autometrics/tests/exemplars_test.rs @@ -70,7 +70,10 @@ fn tracing_opentelemetry_context() { let tracer = provider.tracer("test"); // This adds the OpenTelemetry Context to every tracing Span - let otel_layer = tracing_opentelemetry::layer().with_tracer(tracer); + #[cfg(feature = "exemplars-tracing-opentelemetry-0_20")] + let otel_layer = tracing_opentelemetry_0_20::layer().with_tracer(tracer); + #[cfg(feature = "exemplars-tracing-opentelemetry-0_21")] + let otel_layer = tracing_opentelemetry_0_21::layer().with_tracer(tracer); let subscriber = Registry::default().with(otel_layer); #[autometrics] diff --git a/examples/exemplars-tracing-opentelemetry/Cargo.toml b/examples/exemplars-tracing-opentelemetry/Cargo.toml index e6292be..369fd8b 100644 --- a/examples/exemplars-tracing-opentelemetry/Cargo.toml +++ b/examples/exemplars-tracing-opentelemetry/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" autometrics = { path = "../../autometrics", features = [ "prometheus-client-0_21", "prometheus-exporter", - "exemplars-tracing-opentelemetry-0_20", + "exemplars-tracing-opentelemetry-0_21", ] } autometrics-example-util = { path = "../util" } axum = { version = "0.6", features = ["json"] } @@ -17,5 +17,5 @@ opentelemetry-stdout = { version = "0.1", features = ["trace"] } reqwest = { version = "0.11", features = ["json"] } tokio = { version = "1", features = ["full"] } tracing = "0.1" -tracing-opentelemetry = "0.20" +tracing-opentelemetry = "0.21" tracing-subscriber = { version = "0.3", features = ["env-filter"] }