feat: support opentelemetry 0.23 (#156)

pull/160/head
Marcus Griep 2024-06-03 03:12:56 -06:00 committed by GitHub
parent 2927624652
commit 88dbfe67fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 50 additions and 0 deletions

View File

@ -15,6 +15,7 @@ jobs:
- opentelemetry_0_20 - opentelemetry_0_20
- opentelemetry_0_21 - opentelemetry_0_21
- opentelemetry_0_22 - opentelemetry_0_22
- opentelemetry_0_23
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
@ -57,6 +58,7 @@ jobs:
- opentelemetry_0_20 - opentelemetry_0_20
- opentelemetry_0_21 - opentelemetry_0_21
- opentelemetry_0_22 - opentelemetry_0_22
- opentelemetry_0_23
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4
@ -91,6 +93,7 @@ jobs:
- opentelemetry_0_20 - opentelemetry_0_20
- opentelemetry_0_21 - opentelemetry_0_21
- opentelemetry_0_22 - opentelemetry_0_22
- opentelemetry_0_23
steps: steps:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v4 uses: actions/checkout@v4

View File

@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Added
- Added support for `opentelemetry` version `0.23`.
## [0.3.1] ## [0.3.1]
### Fixed ### Fixed

View File

@ -13,6 +13,7 @@ categories = ["web-programming::http-client"]
opentelemetry_0_20 = ["opentelemetry_0_20_pkg", "tracing-opentelemetry_0_21_pkg"] 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_21 = ["opentelemetry_0_21_pkg", "tracing-opentelemetry_0_22_pkg"]
opentelemetry_0_22 = ["opentelemetry_0_22_pkg", "tracing-opentelemetry_0_23_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"]
[dependencies] [dependencies]
@ -28,9 +29,11 @@ tracing = "0.1.26"
opentelemetry_0_20_pkg = { package = "opentelemetry", version = "0.20.0", optional = true } opentelemetry_0_20_pkg = { package = "opentelemetry", version = "0.20.0", optional = true }
opentelemetry_0_21_pkg = { package = "opentelemetry", version = "0.21.0", optional = true } 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_22_pkg = { package = "opentelemetry", version = "0.22.0", optional = true }
opentelemetry_0_23_pkg = { package = "opentelemetry", version = "0.23.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 }
tracing-opentelemetry_0_24_pkg = { package = "tracing-opentelemetry", version = "0.24.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"] }
@ -43,6 +46,8 @@ reqwest = { version = "0.12.0", features = ["rustls-tls"]}
opentelemetry_sdk_0_21 = { package = "opentelemetry_sdk", version = "0.21.0", features = ["trace"] } 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_22 = { package = "opentelemetry_sdk", version = "0.22.0", features = ["trace"] }
opentelemetry_sdk_0_23 = { package = "opentelemetry_sdk", version = "0.23.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"] }
opentelemetry_stdout_0_4 = { package = "opentelemetry-stdout", version = "0.4.0", features = ["trace"] }

View File

@ -87,6 +87,7 @@ mod middleware;
feature = "opentelemetry_0_20", feature = "opentelemetry_0_20",
feature = "opentelemetry_0_21", feature = "opentelemetry_0_21",
feature = "opentelemetry_0_22", feature = "opentelemetry_0_22",
feature = "opentelemetry_0_23",
))] ))]
mod otel; mod otel;
mod reqwest_otel_span_builder; mod reqwest_otel_span_builder;

View File

@ -49,6 +49,7 @@ where
feature = "opentelemetry_0_20", feature = "opentelemetry_0_20",
feature = "opentelemetry_0_21", feature = "opentelemetry_0_21",
feature = "opentelemetry_0_22", feature = "opentelemetry_0_22",
feature = "opentelemetry_0_23",
))] ))]
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

@ -26,6 +26,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_23")]
opentelemetry_0_23_pkg::global::get_text_map_propagator(|injector| {
use tracing_opentelemetry_0_24_pkg::OpenTelemetrySpanExt;
let context = Span::current().context();
injector.inject_context(&context, &mut RequestCarrier::new(&mut request))
});
request request
} }
@ -74,6 +81,13 @@ impl<'a> opentelemetry_0_22_pkg::propagation::Injector for RequestCarrier<'a> {
} }
} }
#[cfg(feature = "opentelemetry_0_23")]
impl<'a> opentelemetry_0_23_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;
@ -163,6 +177,29 @@ mod test {
subscriber.with(telemetry) subscriber.with(telemetry)
}; };
#[cfg(feature = "opentelemetry_0_23")]
let subscriber = {
use opentelemetry_0_23_pkg::trace::TracerProvider;
use opentelemetry_stdout_0_4::SpanExporterBuilder;
let exporter = SpanExporterBuilder::default()
.with_writer(std::io::sink())
.build();
let provider = opentelemetry_sdk_0_23::trace::TracerProvider::builder()
.with_simple_exporter(exporter)
.build();
let tracer = provider.tracer_builder("reqwest").build();
let _ = opentelemetry_0_23_pkg::global::set_tracer_provider(provider);
opentelemetry_0_23_pkg::global::set_text_map_propagator(
opentelemetry_sdk_0_23::propagation::TraceContextPropagator::new(),
);
let telemetry = tracing_opentelemetry_0_24_pkg::layer().with_tracer(tracer);
subscriber.with(telemetry)
};
tracing::subscriber::set_global_default(subscriber).unwrap(); tracing::subscriber::set_global_default(subscriber).unwrap();
}); });