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 <ethanboxx@gmail.com>

* bump version

---------

Co-authored-by: Ethan Brierley <ethanboxx@gmail.com>
This commit is contained in:
Aitor Fernández 2024-07-15 11:53:31 +01:00 committed by GitHub
parent cb2121478a
commit 4ce811911b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 79 additions and 3 deletions

View file

@ -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

View file

@ -1,6 +1,6 @@
[package]
name = "reqwest-tracing"
version = "0.5.1"
version = "0.5.2"
authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"]
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" }
@ -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"] }

View file

@ -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;

View file

@ -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());
}
}
}
}

View file

@ -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)