feat: support OTel 0.27 (#201)

* feat: support OTel 0.27

* tracer no longer a builder

* typ0
This commit is contained in:
Simen Bekkhus 2024-11-18 12:16:33 +01:00 committed by GitHub
parent 88d2743ee1
commit 8a494c1657
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 0 deletions

View file

@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- Added support for OpenTelemetry `0.27` ([#201](https://github.com/TrueLayer/reqwest-middleware/pull/201))
## [0.5.4] - 2024-11-08 ## [0.5.4] - 2024-11-08
### Added ### Added

View file

@ -17,6 +17,7 @@ opentelemetry_0_23 = ["opentelemetry_0_23_pkg", "tracing-opentelemetry_0_24_pkg"
opentelemetry_0_24 = ["opentelemetry_0_24_pkg", "tracing-opentelemetry_0_25_pkg"] opentelemetry_0_24 = ["opentelemetry_0_24_pkg", "tracing-opentelemetry_0_25_pkg"]
opentelemetry_0_25 = ["opentelemetry_0_25_pkg", "tracing-opentelemetry_0_26_pkg"] opentelemetry_0_25 = ["opentelemetry_0_25_pkg", "tracing-opentelemetry_0_26_pkg"]
opentelemetry_0_26 = ["opentelemetry_0_26_pkg", "tracing-opentelemetry_0_27_pkg"] opentelemetry_0_26 = ["opentelemetry_0_26_pkg", "tracing-opentelemetry_0_27_pkg"]
opentelemetry_0_27 = ["opentelemetry_0_27_pkg", "tracing-opentelemetry_0_28_pkg"]
# This feature ensures that both the old (deprecated) and new attributes are published simultaneously. # 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 # 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. # to continue functioning while encouraging the transition to the new attributes.
@ -39,6 +40,7 @@ opentelemetry_0_23_pkg = { package = "opentelemetry", version = "0.23.0", option
opentelemetry_0_24_pkg = { package = "opentelemetry", version = "0.24.0", optional = true } opentelemetry_0_24_pkg = { package = "opentelemetry", version = "0.24.0", optional = true }
opentelemetry_0_25_pkg = { package = "opentelemetry", version = "0.25.0", optional = true } opentelemetry_0_25_pkg = { package = "opentelemetry", version = "0.25.0", optional = true }
opentelemetry_0_26_pkg = { package = "opentelemetry", version = "0.26.0", optional = true } opentelemetry_0_26_pkg = { package = "opentelemetry", version = "0.26.0", optional = true }
opentelemetry_0_27_pkg = { package = "opentelemetry", version = "0.27.0", optional = true }
tracing-opentelemetry_0_21_pkg = { package = "tracing-opentelemetry", version = "0.21.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_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_23_pkg = { package = "tracing-opentelemetry", version = "0.23.0", optional = true }
@ -46,6 +48,7 @@ tracing-opentelemetry_0_24_pkg = { package = "tracing-opentelemetry", version =
tracing-opentelemetry_0_25_pkg = { package = "tracing-opentelemetry", version = "0.25.0", optional = true } tracing-opentelemetry_0_25_pkg = { package = "tracing-opentelemetry", version = "0.25.0", optional = true }
tracing-opentelemetry_0_26_pkg = { package = "tracing-opentelemetry", version = "0.26.0", optional = true } tracing-opentelemetry_0_26_pkg = { package = "tracing-opentelemetry", version = "0.26.0", optional = true }
tracing-opentelemetry_0_27_pkg = { package = "tracing-opentelemetry", version = "0.27.0", optional = true } tracing-opentelemetry_0_27_pkg = { package = "tracing-opentelemetry", version = "0.27.0", optional = true }
tracing-opentelemetry_0_28_pkg = { package = "tracing-opentelemetry", version = "0.28.0", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.0", features = ["js"] } getrandom = { version = "0.2.0", features = ["js"] }
@ -62,6 +65,7 @@ opentelemetry_sdk_0_23 = { package = "opentelemetry_sdk", version = "0.23.0", fe
opentelemetry_sdk_0_24 = { package = "opentelemetry_sdk", version = "0.24.1", features = ["trace"] } opentelemetry_sdk_0_24 = { package = "opentelemetry_sdk", version = "0.24.1", features = ["trace"] }
opentelemetry_sdk_0_25 = { package = "opentelemetry_sdk", version = "0.25.0", features = ["trace"] } opentelemetry_sdk_0_25 = { package = "opentelemetry_sdk", version = "0.25.0", features = ["trace"] }
opentelemetry_sdk_0_26 = { package = "opentelemetry_sdk", version = "0.26.0", features = ["trace"] } opentelemetry_sdk_0_26 = { package = "opentelemetry_sdk", version = "0.26.0", features = ["trace"] }
opentelemetry_sdk_0_27 = { package = "opentelemetry_sdk", version = "0.27.0", features = ["trace"] }
opentelemetry_stdout_0_1 = { package = "opentelemetry-stdout", version = "0.1.0", 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_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_3 = { package = "opentelemetry-stdout", version = "0.3.0", features = ["trace"] }

View file

@ -91,6 +91,7 @@ mod middleware;
feature = "opentelemetry_0_24", feature = "opentelemetry_0_24",
feature = "opentelemetry_0_25", feature = "opentelemetry_0_25",
feature = "opentelemetry_0_26", feature = "opentelemetry_0_26",
feature = "opentelemetry_0_27",
))] ))]
mod otel; mod otel;
mod reqwest_otel_span_builder; mod reqwest_otel_span_builder;

View file

@ -53,6 +53,7 @@ where
feature = "opentelemetry_0_24", feature = "opentelemetry_0_24",
feature = "opentelemetry_0_25", feature = "opentelemetry_0_25",
feature = "opentelemetry_0_26", feature = "opentelemetry_0_26",
feature = "opentelemetry_0_27",
))] ))]
let req = if extensions.get::<crate::DisableOtelPropagation>().is_none() { 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. // Adds tracing headers to the given request to propagate the OpenTelemetry context to downstream revivers of the request.

View file

@ -54,6 +54,13 @@ pub fn inject_opentelemetry_context_into_request(mut request: Request) -> Reques
injector.inject_context(&context, &mut RequestCarrier::new(&mut request)) injector.inject_context(&context, &mut RequestCarrier::new(&mut request))
}); });
#[cfg(feature = "opentelemetry_0_27")]
opentelemetry_0_27_pkg::global::get_text_map_propagator(|injector| {
use tracing_opentelemetry_0_28_pkg::OpenTelemetrySpanExt;
let context = Span::current().context();
injector.inject_context(&context, &mut RequestCarrier::new(&mut request))
});
request request
} }
@ -130,6 +137,13 @@ impl<'a> opentelemetry_0_26_pkg::propagation::Injector for RequestCarrier<'a> {
} }
} }
#[cfg(feature = "opentelemetry_0_27")]
impl<'a> opentelemetry_0_27_pkg::propagation::Injector for RequestCarrier<'a> {
fn set(&mut self, key: &str, value: String) {
self.set_inner(key, value)
}
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use std::sync::OnceLock; use std::sync::OnceLock;
@ -297,6 +311,22 @@ mod test {
subscriber.with(telemetry) subscriber.with(telemetry)
}; };
#[cfg(feature = "opentelemetry_0_27")]
let subscriber = {
use opentelemetry_0_27_pkg::trace::TracerProvider;
let provider = opentelemetry_sdk_0_27::trace::TracerProvider::builder().build();
let tracer = provider.tracer("reqwest");
let _ = opentelemetry_0_27_pkg::global::set_tracer_provider(provider);
opentelemetry_0_27_pkg::global::set_text_map_propagator(
opentelemetry_sdk_0_27::propagation::TraceContextPropagator::new(),
);
let telemetry = tracing_opentelemetry_0_28_pkg::layer().with_tracer(tracer);
subscriber.with(telemetry)
};
tracing::subscriber::set_global_default(subscriber).unwrap(); tracing::subscriber::set_global_default(subscriber).unwrap();
}); });