From 4ce811911bf841db6e7d91efd65ba7e5e40a00f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Fern=C3=A1ndez?= Date: Mon, 15 Jul 2024 11:53:31 +0100 Subject: [PATCH] Add feature flag for deprecated fields (#167) * Add feature flag for deprecated fields * revert editor format * rename flag * remove unnecessary cfg not * add comment and publish old and new attributes on request_span macro * add comment * update changelog * Update reqwest-tracing/CHANGELOG.md Co-authored-by: Ethan Brierley * bump version --------- Co-authored-by: Ethan Brierley --- reqwest-tracing/CHANGELOG.md | 5 +++ reqwest-tracing/Cargo.toml | 10 ++++-- reqwest-tracing/src/lib.rs | 5 +++ .../src/reqwest_otel_span_builder.rs | 31 +++++++++++++++++++ .../src/reqwest_otel_span_macro.rs | 31 +++++++++++++++++++ 5 files changed, 79 insertions(+), 3 deletions(-) diff --git a/reqwest-tracing/CHANGELOG.md b/reqwest-tracing/CHANGELOG.md index 31ba8f6..a5fa25f 100644 --- a/reqwest-tracing/CHANGELOG.md +++ b/reqwest-tracing/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.5.2] - 2024-07-15 + +### Added +- Added feature flag, `deprecated_attributes`, for emitting [deprecated opentelemetry HTTP attributes](https://opentelemetry.io/docs/specs/semconv/http/migration-guide/) alongside the stable ones used by default + ## [0.5.1] - 2024-06-28 ### Added diff --git a/reqwest-tracing/Cargo.toml b/reqwest-tracing/Cargo.toml index 3321b74..227f097 100644 --- a/reqwest-tracing/Cargo.toml +++ b/reqwest-tracing/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reqwest-tracing" -version = "0.5.1" +version = "0.5.2" authors = ["Rodrigo Gryzinski "] edition = "2018" description = "Opentracing middleware for reqwest." @@ -14,7 +14,10 @@ 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"] - +# 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. +deprecated_attributes = [] [dependencies] reqwest-middleware = { version = "0.3.0", path = "../reqwest-middleware" } @@ -42,7 +45,7 @@ getrandom = { version = "0.2.0", features = ["js"] } tokio = { version = "1.0.0", features = ["macros"] } tracing_subscriber = { package = "tracing-subscriber", version = "0.3.0" } wiremock = "0.6.0" -reqwest = { version = "0.12.0", features = ["rustls-tls"]} +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"] } @@ -51,3 +54,4 @@ opentelemetry_stdout_0_1 = { package = "opentelemetry-stdout", version = "0.1.0" 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"] } + diff --git a/reqwest-tracing/src/lib.rs b/reqwest-tracing/src/lib.rs index 726b0d4..570a31f 100644 --- a/reqwest-tracing/src/lib.rs +++ b/reqwest-tracing/src/lib.rs @@ -100,5 +100,10 @@ pub use reqwest_otel_span_builder::{ SERVER_ADDRESS, SERVER_PORT, URL_FULL, URL_SCHEME, USER_AGENT_ORIGINAL, }; +#[cfg(feature = "deprecated_attributes")] +pub use reqwest_otel_span_builder::{ + HTTP_HOST, HTTP_METHOD, HTTP_SCHEME, HTTP_STATUS_CODE, HTTP_URL, HTTP_USER_AGENT, NET_HOST_PORT, +}; + #[doc(hidden)] pub mod reqwest_otel_span_macro; diff --git a/reqwest-tracing/src/reqwest_otel_span_builder.rs b/reqwest-tracing/src/reqwest_otel_span_builder.rs index cf3772c..86bce52 100644 --- a/reqwest-tracing/src/reqwest_otel_span_builder.rs +++ b/reqwest-tracing/src/reqwest_otel_span_builder.rs @@ -33,6 +33,28 @@ pub const ERROR_MESSAGE: &str = "error.message"; /// The `error.cause_chain` field added to the span by [`reqwest_otel_span`] pub const ERROR_CAUSE_CHAIN: &str = "error.cause_chain"; +/// The `http.method` field added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const HTTP_METHOD: &str = "http.method"; +/// The `http.scheme` field added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const HTTP_SCHEME: &str = "http.scheme"; +/// The `http.host` field added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const HTTP_HOST: &str = "http.host"; +/// The `http.url` field added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const HTTP_URL: &str = "http.url"; +/// The `host.port` field added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const NET_HOST_PORT: &str = "net.host.port"; +/// The `http.status_code` field added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const HTTP_STATUS_CODE: &str = "http.status_code"; +/// The `http.user_agent` added to the span by [`reqwest_otel_span`] +#[cfg(feature = "deprecated_attributes")] +pub const HTTP_USER_AGENT: &str = "http.user_agent"; + /// [`ReqwestOtelSpanBackend`] allows you to customise the span attached by /// [`TracingMiddleware`] to incoming requests. /// @@ -64,6 +86,11 @@ pub fn default_on_request_success(span: &Span, response: &Response) { span.record(OTEL_STATUS_CODE, span_status); } span.record(HTTP_RESPONSE_STATUS_CODE, response.status().as_u16()); + #[cfg(feature = "deprecated_attributes")] + { + span.record(HTTP_STATUS_CODE, response.status().as_u16()); + span.record(HTTP_USER_AGENT, user_agent.as_str()); + } } /// Populates default failure fields for a given [`reqwest_otel_span!`] span. @@ -77,6 +104,10 @@ pub fn default_on_request_failure(span: &Span, e: &Error) { if let Error::Reqwest(e) = e { if let Some(status) = e.status() { span.record(HTTP_RESPONSE_STATUS_CODE, status.as_u16()); + #[cfg(feature = "deprecated_attributes")] + { + span.record(HTTP_STATUS_CODE, status.as_u16()); + } } } } diff --git a/reqwest-tracing/src/reqwest_otel_span_macro.rs b/reqwest-tracing/src/reqwest_otel_span_macro.rs index b60a7fb..f35cb0a 100644 --- a/reqwest-tracing/src/reqwest_otel_span_macro.rs +++ b/reqwest-tracing/src/reqwest_otel_span_macro.rs @@ -127,6 +127,7 @@ macro_rules! reqwest_otel_span { let header_default = &::http::HeaderValue::from_static(""); let user_agent = format!("{:?}", $request.headers().get("user-agent").unwrap_or(header_default)).replace('"', ""); + #[cfg(not(feature = "deprecated_attributes"))] macro_rules! request_span { ($lvl:expr) => { $crate::reqwest_otel_span_macro::private::span!( @@ -148,6 +149,36 @@ macro_rules! reqwest_otel_span { } } + // With the deprecated attributes flag enabled, we publish both the old and new attributes. + #[cfg(feature = "deprecated_attributes")] + macro_rules! request_span { + ($lvl:expr) => { + $crate::reqwest_otel_span_macro::private::span!( + $lvl, + "HTTP request", + http.request.method = %method, + url.scheme = %scheme, + server.address = %host, + server.port = %host_port, + user_agent.original = %user_agent, + otel.kind = "client", + otel.name = %otel_name, + otel.status_code = tracing::field::Empty, + http.response.status_code = tracing::field::Empty, + error.message = tracing::field::Empty, + error.cause_chain = tracing::field::Empty, + // old attributes + http.method = %method, + http.scheme = %scheme, + http.host = %host, + net.host.port = %host_port, + http.user_agent = tracing::field::Empty, + http.status_code = tracing::field::Empty, + $($field)* + ) + } + } + let span = match $level { $crate::reqwest_otel_span_macro::private::Level::TRACE => { request_span!($crate::reqwest_otel_span_macro::private::Level::TRACE)