Skip to content
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

OpenTelemetry 0.24 support #171

Merged
merged 4 commits into from
Jul 30, 2024
Merged
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
5 changes: 5 additions & 0 deletions reqwest-tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ opentelemetry_0_20 = ["opentelemetry_0_20_pkg", "tracing-opentelemetry_0_21_pkg"
opentelemetry_0_21 = ["opentelemetry_0_21_pkg", "tracing-opentelemetry_0_22_pkg"]
opentelemetry_0_22 = ["opentelemetry_0_22_pkg", "tracing-opentelemetry_0_23_pkg"]
opentelemetry_0_23 = ["opentelemetry_0_23_pkg", "tracing-opentelemetry_0_24_pkg"]
opentelemetry_0_24 = ["opentelemetry_0_24_pkg", "tracing-opentelemetry_0_25_pkg"]
# This feature ensures that both the old (deprecated) and new attributes are published simultaneously.
# By doing so, we maintain backward compatibility, allowing existing code that relies on the old attributes
# to continue functioning while encouraging the transition to the new attributes.
Expand All @@ -33,10 +34,12 @@ opentelemetry_0_20_pkg = { package = "opentelemetry", version = "0.20.0", option
opentelemetry_0_21_pkg = { package = "opentelemetry", version = "0.21.0", optional = true }
opentelemetry_0_22_pkg = { package = "opentelemetry", version = "0.22.0", optional = true }
opentelemetry_0_23_pkg = { package = "opentelemetry", version = "0.23.0", optional = true }
opentelemetry_0_24_pkg = { package = "opentelemetry", version = "0.24.0", optional = true }
tracing-opentelemetry_0_21_pkg = { package = "tracing-opentelemetry", version = "0.21.0", optional = true }
tracing-opentelemetry_0_22_pkg = { package = "tracing-opentelemetry", version = "0.22.0", optional = true }
tracing-opentelemetry_0_23_pkg = { package = "tracing-opentelemetry", version = "0.23.0", optional = true }
tracing-opentelemetry_0_24_pkg = { package = "tracing-opentelemetry", version = "0.24.0", optional = true }
tracing-opentelemetry_0_25_pkg = { package = "tracing-opentelemetry", version = "0.25.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.0", features = ["js"] }
Expand All @@ -50,8 +53,10 @@ reqwest = { version = "0.12.0", features = ["rustls-tls"] }
opentelemetry_sdk_0_21 = { package = "opentelemetry_sdk", version = "0.21.0", features = ["trace"] }
opentelemetry_sdk_0_22 = { package = "opentelemetry_sdk", version = "0.22.0", features = ["trace"] }
opentelemetry_sdk_0_23 = { package = "opentelemetry_sdk", version = "0.23.0", features = ["trace"] }
opentelemetry_sdk_0_24 = { package = "opentelemetry_sdk", version = "0.24.1", features = ["trace"] }
opentelemetry_stdout_0_1 = { package = "opentelemetry-stdout", version = "0.1.0", features = ["trace"] }
opentelemetry_stdout_0_2 = { package = "opentelemetry-stdout", version = "0.2.0", features = ["trace"] }
opentelemetry_stdout_0_3 = { package = "opentelemetry-stdout", version = "0.3.0", features = ["trace"] }
opentelemetry_stdout_0_4 = { package = "opentelemetry-stdout", version = "0.4.0", features = ["trace"] }
opentelemetry_stdout_0_5 = { package = "opentelemetry-stdout", version = "0.5.0", features = ["trace"] }

1 change: 1 addition & 0 deletions reqwest-tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mod middleware;
feature = "opentelemetry_0_21",
feature = "opentelemetry_0_22",
feature = "opentelemetry_0_23",
feature = "opentelemetry_0_24",
))]
mod otel;
mod reqwest_otel_span_builder;
Expand Down
1 change: 1 addition & 0 deletions reqwest-tracing/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ where
feature = "opentelemetry_0_21",
feature = "opentelemetry_0_22",
feature = "opentelemetry_0_23",
feature = "opentelemetry_0_24",
))]
let req = if extensions.get::<crate::DisableOtelPropagation>().is_none() {
// Adds tracing headers to the given request to propagate the OpenTelemetry context to downstream revivers of the request.
Expand Down
37 changes: 37 additions & 0 deletions reqwest-tracing/src/otel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ pub fn inject_opentelemetry_context_into_request(mut request: Request) -> Reques
injector.inject_context(&context, &mut RequestCarrier::new(&mut request))
});

#[cfg(feature = "opentelemetry_0_24")]
opentelemetry_0_24_pkg::global::get_text_map_propagator(|injector| {
use tracing_opentelemetry_0_25_pkg::OpenTelemetrySpanExt;
let context = Span::current().context();
injector.inject_context(&context, &mut RequestCarrier::new(&mut request))
});

request
}

Expand Down Expand Up @@ -88,6 +95,13 @@ impl<'a> opentelemetry_0_23_pkg::propagation::Injector for RequestCarrier<'a> {
}
}

#[cfg(feature = "opentelemetry_0_24")]
impl<'a> opentelemetry_0_24_pkg::propagation::Injector for RequestCarrier<'a> {
fn set(&mut self, key: &str, value: String) {
self.set_inner(key, value)
}
}

#[cfg(test)]
mod test {
use std::sync::OnceLock;
Expand Down Expand Up @@ -200,6 +214,29 @@ mod test {
subscriber.with(telemetry)
};

#[cfg(feature = "opentelemetry_0_24")]
let subscriber = {
use opentelemetry_0_24_pkg::trace::TracerProvider;
use opentelemetry_stdout_0_5::SpanExporterBuilder;

let exporter = SpanExporterBuilder::default()
.with_writer(std::io::sink())
.build();

let provider = opentelemetry_sdk_0_24::trace::TracerProvider::builder()
.with_simple_exporter(exporter)
.build();

let tracer = provider.tracer_builder("reqwest").build();
let _ = opentelemetry_0_24_pkg::global::set_tracer_provider(provider);
opentelemetry_0_24_pkg::global::set_text_map_propagator(
opentelemetry_sdk_0_24::propagation::TraceContextPropagator::new(),
);

let telemetry = tracing_opentelemetry_0_25_pkg::layer().with_tracer(tracer);
subscriber.with(telemetry)
};

tracing::subscriber::set_global_default(subscriber).unwrap();
});

Expand Down
Loading