Skip to content

Commit

Permalink
feat: support for 'tracing-opentelemetry:0.21'
Browse files Browse the repository at this point in the history
  • Loading branch information
woshilapin committed Oct 23, 2023
1 parent c9bd7e1 commit e5f0cf4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"] }
Expand All @@ -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"] }
Expand Down
4 changes: 2 additions & 2 deletions autometrics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" },
Expand Down
2 changes: 1 addition & 1 deletion autometrics/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 13 additions & 2 deletions autometrics/src/exemplars/tracing_opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TraceLabels> {
// 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
Expand Down
1 change: 1 addition & 0 deletions autometrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion autometrics/tests/exemplars_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions examples/exemplars-tracing-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"] }

0 comments on commit e5f0cf4

Please sign in to comment.