Add support for `opentelemetry` version `0.21` (#112)

* Add support for `opentelemetry` version `0.21`

* Fix tracer provider import
main
Julian Tescher 2024-01-19 03:17:54 -08:00 committed by GitHub
parent d297b1c5df
commit 32cc268f07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 6 deletions

View File

@ -20,6 +20,7 @@ jobs:
- opentelemetry_0_18
- opentelemetry_0_19
- opentelemetry_0_20
- opentelemetry_0_21
steps:
- name: Checkout repository
uses: actions/checkout@v2
@ -67,6 +68,7 @@ jobs:
- opentelemetry_0_18
- opentelemetry_0_19
- opentelemetry_0_20
- opentelemetry_0_21
steps:
- name: Checkout repository
uses: actions/checkout@v2
@ -97,6 +99,7 @@ jobs:
- opentelemetry_0_18
- opentelemetry_0_19
- opentelemetry_0_20
- opentelemetry_0_21
steps:
- name: Checkout repository
uses: actions/checkout@v2

View File

@ -18,6 +18,7 @@ opentelemetry_0_17 = ["opentelemetry_0_17_pkg", "tracing-opentelemetry_0_17_pkg"
opentelemetry_0_18 = ["opentelemetry_0_18_pkg", "tracing-opentelemetry_0_18_pkg"]
opentelemetry_0_19 = ["opentelemetry_0_19_pkg", "tracing-opentelemetry_0_19_pkg"]
opentelemetry_0_20 = ["opentelemetry_0_20_pkg", "tracing-opentelemetry_0_20_pkg"]
opentelemetry_0_21 = ["opentelemetry_0_21_pkg", "tracing-opentelemetry_0_22_pkg"]
[dependencies]
@ -38,6 +39,7 @@ opentelemetry_0_17_pkg = { package = "opentelemetry", version = "0.17.0", option
opentelemetry_0_18_pkg = { package = "opentelemetry", version = "0.18.0", optional = true }
opentelemetry_0_19_pkg = { package = "opentelemetry", version = "0.19.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 }
tracing-opentelemetry_0_12_pkg = { package = "tracing-opentelemetry", version = "0.12.0", optional = true }
tracing-opentelemetry_0_13_pkg = { package = "tracing-opentelemetry", version = "0.13.0", optional = true }
tracing-opentelemetry_0_14_pkg = { package = "tracing-opentelemetry", version = "0.14.0", optional = true }
@ -46,6 +48,7 @@ tracing-opentelemetry_0_17_pkg = { package = "tracing-opentelemetry", version =
tracing-opentelemetry_0_18_pkg = { package = "tracing-opentelemetry", version = "0.18.0", optional = true }
tracing-opentelemetry_0_19_pkg = { package = "tracing-opentelemetry", version = "0.19.0", optional = true }
tracing-opentelemetry_0_20_pkg = { package = "tracing-opentelemetry", version = "0.20.0", optional = true }
tracing-opentelemetry_0_22_pkg = { package = "tracing-opentelemetry", version = "0.22.0", optional = true }
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.0", features = ["js"] }
@ -56,4 +59,6 @@ tracing_subscriber_0_2 = { package = "tracing-subscriber", version = "0.2.0" }
tracing_subscriber_0_3 = { package = "tracing-subscriber", version = "0.3.0" }
wiremock = "0.5.0"
opentelemetry-stdout = { version = "0.1.0", features = ["trace"] }
opentelemetry_sdk_0_21 = { package = "opentelemetry_sdk", version = "0.21.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"] }

View File

@ -92,8 +92,9 @@ an opentelemetry version feature:
reqwest-tracing = { version = "0.3.1", features = ["opentelemetry_0_18"] }
```
Available opentelemetry features are `opentelemetry_0_20`, `opentelemetry_0_19`, `opentelemetry_0_18`, `opentelemetry_0_17`, `opentelemetry_0_16`, `opentelemetry_0_15`, `opentelemetry_0_14` and
`opentelemetry_0_13`.
Available opentelemetry features are `opentelemetry_0_21`, `opentelemetry_0_20`,
`opentelemetry_0_19`, `opentelemetry_0_18`, `opentelemetry_0_17`, `opentelemetry_0_16`,
`opentelemetry_0_15`, `opentelemetry_0_14` and `opentelemetry_0_13`.
#### License

View File

@ -92,6 +92,7 @@ mod middleware;
feature = "opentelemetry_0_18",
feature = "opentelemetry_0_19",
feature = "opentelemetry_0_20",
feature = "opentelemetry_0_21",
))]
mod otel;
mod reqwest_otel_span_builder;

View File

@ -54,6 +54,7 @@ where
feature = "opentelemetry_0_18",
feature = "opentelemetry_0_19",
feature = "opentelemetry_0_20",
feature = "opentelemetry_0_21",
))]
let req = if !extensions.contains::<crate::DisableOtelPropagation>() {
// Adds tracing headers to the given request to propagate the OpenTelemetry context to downstream revivers of the request.

View File

@ -27,6 +27,9 @@ use opentelemetry_0_19_pkg as opentelemetry;
#[cfg(feature = "opentelemetry_0_20")]
use opentelemetry_0_20_pkg as opentelemetry;
#[cfg(feature = "opentelemetry_0_21")]
use opentelemetry_0_21_pkg as opentelemetry;
#[cfg(feature = "opentelemetry_0_13")]
pub use tracing_opentelemetry_0_12_pkg as tracing_opentelemetry;
@ -51,6 +54,9 @@ pub use tracing_opentelemetry_0_19_pkg as tracing_opentelemetry;
#[cfg(feature = "opentelemetry_0_20")]
pub use tracing_opentelemetry_0_20_pkg as tracing_opentelemetry;
#[cfg(feature = "opentelemetry_0_21")]
pub use tracing_opentelemetry_0_22_pkg as tracing_opentelemetry;
use opentelemetry::global;
use opentelemetry::propagation::Injector;
use tracing_opentelemetry::OpenTelemetrySpanExt;
@ -96,7 +102,10 @@ mod test {
use super::*;
use crate::{DisableOtelPropagation, TracingMiddleware};
#[cfg(not(feature = "opentelemetry_0_21"))]
use opentelemetry::sdk::propagation::TraceContextPropagator;
#[cfg(feature = "opentelemetry_0_21")]
use opentelemetry_sdk_0_21::propagation::TraceContextPropagator;
use reqwest::Response;
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Extension};
use tracing::{info_span, Instrument, Level};
@ -118,19 +127,33 @@ mod test {
static TELEMETRY: OnceLock<()> = OnceLock::new();
TELEMETRY.get_or_init(|| {
#[cfg(not(feature = "opentelemetry_0_20"))]
#[cfg(all(
not(feature = "opentelemetry_0_20"),
not(feature = "opentelemetry_0_21")
))]
let tracer = opentelemetry::sdk::export::trace::stdout::new_pipeline()
.with_writer(std::io::sink())
.install_simple();
#[cfg(feature = "opentelemetry_0_20")]
#[cfg(any(feature = "opentelemetry_0_20", feature = "opentelemetry_0_21"))]
let tracer = {
use opentelemetry::trace::TracerProvider;
let exporter = opentelemetry_stdout::SpanExporterBuilder::default()
#[cfg(feature = "opentelemetry_0_20")]
use opentelemetry_stdout_0_1::SpanExporterBuilder;
#[cfg(feature = "opentelemetry_0_21")]
use opentelemetry_stdout_0_2::SpanExporterBuilder;
let exporter = SpanExporterBuilder::default()
.with_writer(std::io::sink())
.build();
#[cfg(feature = "opentelemetry_0_20")]
let provider = opentelemetry::sdk::trace::TracerProvider::builder()
.with_simple_exporter(exporter)
.build();
#[cfg(feature = "opentelemetry_0_21")]
let provider = opentelemetry_sdk_0_21::trace::TracerProvider::builder()
.with_simple_exporter(exporter)
.build();
let tracer = provider.versioned_tracer("reqwest", None::<&str>, None::<&str>, None);
let _ = global::set_tracer_provider(provider);
tracer