Skip to content

Commit

Permalink
Add feature flag for deprecated fields (#167)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* bump version

---------

Co-authored-by: Ethan Brierley <[email protected]>
  • Loading branch information
aitorfernandez and eopb authored Jul 15, 2024
1 parent cb21214 commit 4ce8119
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
5 changes: 5 additions & 0 deletions reqwest-tracing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions reqwest-tracing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reqwest-tracing"
version = "0.5.1"
version = "0.5.2"
authors = ["Rodrigo Gryzinski <[email protected]>"]
edition = "2018"
description = "Opentracing middleware for reqwest."
Expand All @@ -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" }
Expand Down Expand Up @@ -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"] }
Expand All @@ -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"] }

5 changes: 5 additions & 0 deletions reqwest-tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
31 changes: 31 additions & 0 deletions reqwest-tracing/src/reqwest_otel_span_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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.
Expand All @@ -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());
}
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions reqwest-tracing/src/reqwest_otel_span_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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)
Expand Down

0 comments on commit 4ce8119

Please sign in to comment.