Skip to content

Commit 4ce8119

Browse files
Aitor Fernándezeopb
Aitor Fernández
andauthored
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 <[email protected]> * bump version --------- Co-authored-by: Ethan Brierley <[email protected]>
1 parent cb21214 commit 4ce8119

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

reqwest-tracing/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.5.2] - 2024-07-15
11+
12+
### Added
13+
- 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
14+
1015
## [0.5.1] - 2024-06-28
1116

1217
### Added

reqwest-tracing/Cargo.toml

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "reqwest-tracing"
3-
version = "0.5.1"
3+
version = "0.5.2"
44
authors = ["Rodrigo Gryzinski <[email protected]>"]
55
edition = "2018"
66
description = "Opentracing middleware for reqwest."
@@ -14,7 +14,10 @@ opentelemetry_0_20 = ["opentelemetry_0_20_pkg", "tracing-opentelemetry_0_21_pkg"
1414
opentelemetry_0_21 = ["opentelemetry_0_21_pkg", "tracing-opentelemetry_0_22_pkg"]
1515
opentelemetry_0_22 = ["opentelemetry_0_22_pkg", "tracing-opentelemetry_0_23_pkg"]
1616
opentelemetry_0_23 = ["opentelemetry_0_23_pkg", "tracing-opentelemetry_0_24_pkg"]
17-
17+
# This feature ensures that both the old (deprecated) and new attributes are published simultaneously.
18+
# By doing so, we maintain backward compatibility, allowing existing code that relies on the old attributes
19+
# to continue functioning while encouraging the transition to the new attributes.
20+
deprecated_attributes = []
1821

1922
[dependencies]
2023
reqwest-middleware = { version = "0.3.0", path = "../reqwest-middleware" }
@@ -42,7 +45,7 @@ getrandom = { version = "0.2.0", features = ["js"] }
4245
tokio = { version = "1.0.0", features = ["macros"] }
4346
tracing_subscriber = { package = "tracing-subscriber", version = "0.3.0" }
4447
wiremock = "0.6.0"
45-
reqwest = { version = "0.12.0", features = ["rustls-tls"]}
48+
reqwest = { version = "0.12.0", features = ["rustls-tls"] }
4649

4750
opentelemetry_sdk_0_21 = { package = "opentelemetry_sdk", version = "0.21.0", features = ["trace"] }
4851
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"
5154
opentelemetry_stdout_0_2 = { package = "opentelemetry-stdout", version = "0.2.0", features = ["trace"] }
5255
opentelemetry_stdout_0_3 = { package = "opentelemetry-stdout", version = "0.3.0", features = ["trace"] }
5356
opentelemetry_stdout_0_4 = { package = "opentelemetry-stdout", version = "0.4.0", features = ["trace"] }
57+

reqwest-tracing/src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,10 @@ pub use reqwest_otel_span_builder::{
100100
SERVER_ADDRESS, SERVER_PORT, URL_FULL, URL_SCHEME, USER_AGENT_ORIGINAL,
101101
};
102102

103+
#[cfg(feature = "deprecated_attributes")]
104+
pub use reqwest_otel_span_builder::{
105+
HTTP_HOST, HTTP_METHOD, HTTP_SCHEME, HTTP_STATUS_CODE, HTTP_URL, HTTP_USER_AGENT, NET_HOST_PORT,
106+
};
107+
103108
#[doc(hidden)]
104109
pub mod reqwest_otel_span_macro;

reqwest-tracing/src/reqwest_otel_span_builder.rs

+31
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,28 @@ pub const ERROR_MESSAGE: &str = "error.message";
3333
/// The `error.cause_chain` field added to the span by [`reqwest_otel_span`]
3434
pub const ERROR_CAUSE_CHAIN: &str = "error.cause_chain";
3535

36+
/// The `http.method` field added to the span by [`reqwest_otel_span`]
37+
#[cfg(feature = "deprecated_attributes")]
38+
pub const HTTP_METHOD: &str = "http.method";
39+
/// The `http.scheme` field added to the span by [`reqwest_otel_span`]
40+
#[cfg(feature = "deprecated_attributes")]
41+
pub const HTTP_SCHEME: &str = "http.scheme";
42+
/// The `http.host` field added to the span by [`reqwest_otel_span`]
43+
#[cfg(feature = "deprecated_attributes")]
44+
pub const HTTP_HOST: &str = "http.host";
45+
/// The `http.url` field added to the span by [`reqwest_otel_span`]
46+
#[cfg(feature = "deprecated_attributes")]
47+
pub const HTTP_URL: &str = "http.url";
48+
/// The `host.port` field added to the span by [`reqwest_otel_span`]
49+
#[cfg(feature = "deprecated_attributes")]
50+
pub const NET_HOST_PORT: &str = "net.host.port";
51+
/// The `http.status_code` field added to the span by [`reqwest_otel_span`]
52+
#[cfg(feature = "deprecated_attributes")]
53+
pub const HTTP_STATUS_CODE: &str = "http.status_code";
54+
/// The `http.user_agent` added to the span by [`reqwest_otel_span`]
55+
#[cfg(feature = "deprecated_attributes")]
56+
pub const HTTP_USER_AGENT: &str = "http.user_agent";
57+
3658
/// [`ReqwestOtelSpanBackend`] allows you to customise the span attached by
3759
/// [`TracingMiddleware`] to incoming requests.
3860
///
@@ -64,6 +86,11 @@ pub fn default_on_request_success(span: &Span, response: &Response) {
6486
span.record(OTEL_STATUS_CODE, span_status);
6587
}
6688
span.record(HTTP_RESPONSE_STATUS_CODE, response.status().as_u16());
89+
#[cfg(feature = "deprecated_attributes")]
90+
{
91+
span.record(HTTP_STATUS_CODE, response.status().as_u16());
92+
span.record(HTTP_USER_AGENT, user_agent.as_str());
93+
}
6794
}
6895

6996
/// 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) {
77104
if let Error::Reqwest(e) = e {
78105
if let Some(status) = e.status() {
79106
span.record(HTTP_RESPONSE_STATUS_CODE, status.as_u16());
107+
#[cfg(feature = "deprecated_attributes")]
108+
{
109+
span.record(HTTP_STATUS_CODE, status.as_u16());
110+
}
80111
}
81112
}
82113
}

reqwest-tracing/src/reqwest_otel_span_macro.rs

+31
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ macro_rules! reqwest_otel_span {
127127
let header_default = &::http::HeaderValue::from_static("");
128128
let user_agent = format!("{:?}", $request.headers().get("user-agent").unwrap_or(header_default)).replace('"', "");
129129

130+
#[cfg(not(feature = "deprecated_attributes"))]
130131
macro_rules! request_span {
131132
($lvl:expr) => {
132133
$crate::reqwest_otel_span_macro::private::span!(
@@ -148,6 +149,36 @@ macro_rules! reqwest_otel_span {
148149
}
149150
}
150151

152+
// With the deprecated attributes flag enabled, we publish both the old and new attributes.
153+
#[cfg(feature = "deprecated_attributes")]
154+
macro_rules! request_span {
155+
($lvl:expr) => {
156+
$crate::reqwest_otel_span_macro::private::span!(
157+
$lvl,
158+
"HTTP request",
159+
http.request.method = %method,
160+
url.scheme = %scheme,
161+
server.address = %host,
162+
server.port = %host_port,
163+
user_agent.original = %user_agent,
164+
otel.kind = "client",
165+
otel.name = %otel_name,
166+
otel.status_code = tracing::field::Empty,
167+
http.response.status_code = tracing::field::Empty,
168+
error.message = tracing::field::Empty,
169+
error.cause_chain = tracing::field::Empty,
170+
// old attributes
171+
http.method = %method,
172+
http.scheme = %scheme,
173+
http.host = %host,
174+
net.host.port = %host_port,
175+
http.user_agent = tracing::field::Empty,
176+
http.status_code = tracing::field::Empty,
177+
$($field)*
178+
)
179+
}
180+
}
181+
151182
let span = match $level {
152183
$crate::reqwest_otel_span_macro::private::Level::TRACE => {
153184
request_span!($crate::reqwest_otel_span_macro::private::Level::TRACE)

0 commit comments

Comments
 (0)