From fb7a964ba3d521aaf4da40cb6c838a7727639ccf Mon Sep 17 00:00:00 2001 From: Conrad Ludgate Date: Mon, 15 May 2023 14:55:56 +0100 Subject: [PATCH] add default_span_name helper function (#93) --- reqwest-tracing/CHANGELOG.md | 5 ++ reqwest-tracing/Cargo.toml | 2 +- reqwest-tracing/src/lib.rs | 7 +-- .../src/reqwest_otel_span_builder.rs | 51 +++++++++---------- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/reqwest-tracing/CHANGELOG.md b/reqwest-tracing/CHANGELOG.md index c2fecab..432fcc7 100644 --- a/reqwest-tracing/CHANGELOG.md +++ b/reqwest-tracing/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.4.4] - 2023-05-15 + +### Added +- A new `default_span_name` method for use in custom span backends. + ## [0.4.3] - 2023-05-15 ### Fixed diff --git a/reqwest-tracing/Cargo.toml b/reqwest-tracing/Cargo.toml index ce5f5a6..1493a0f 100644 --- a/reqwest-tracing/Cargo.toml +++ b/reqwest-tracing/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reqwest-tracing" -version = "0.4.3" +version = "0.4.4" authors = ["Rodrigo Gryzinski "] edition = "2018" description = "Opentracing middleware for reqwest." diff --git a/reqwest-tracing/src/lib.rs b/reqwest-tracing/src/lib.rs index 8a22943..562da88 100644 --- a/reqwest-tracing/src/lib.rs +++ b/reqwest-tracing/src/lib.rs @@ -96,9 +96,10 @@ mod reqwest_otel_span_builder; pub use middleware::TracingMiddleware; pub use reqwest_otel_span_builder::{ default_on_request_end, default_on_request_failure, default_on_request_success, - DefaultSpanBackend, OtelName, OtelPathNames, ReqwestOtelSpanBackend, SpanBackendWithUrl, - ERROR_CAUSE_CHAIN, ERROR_MESSAGE, HTTP_HOST, HTTP_METHOD, HTTP_SCHEME, HTTP_STATUS_CODE, - HTTP_URL, HTTP_USER_AGENT, NET_HOST_PORT, OTEL_KIND, OTEL_NAME, OTEL_STATUS_CODE, + default_span_name, DefaultSpanBackend, OtelName, OtelPathNames, ReqwestOtelSpanBackend, + SpanBackendWithUrl, ERROR_CAUSE_CHAIN, ERROR_MESSAGE, HTTP_HOST, HTTP_METHOD, HTTP_SCHEME, + HTTP_STATUS_CODE, HTTP_URL, HTTP_USER_AGENT, NET_HOST_PORT, OTEL_KIND, OTEL_NAME, + OTEL_STATUS_CODE, }; #[doc(hidden)] diff --git a/reqwest-tracing/src/reqwest_otel_span_builder.rs b/reqwest-tracing/src/reqwest_otel_span_builder.rs index a762eec..83605dc 100644 --- a/reqwest-tracing/src/reqwest_otel_span_builder.rs +++ b/reqwest-tracing/src/reqwest_otel_span_builder.rs @@ -84,6 +84,27 @@ pub fn default_on_request_failure(span: &Span, e: &Error) { } } +/// Determine the name of the span that should be associated with this request. +/// +/// This tries to be PII safe by default, not including any path information unless +/// specifically opted in using either [`OtelName`] or [`OtelPathNames`] +#[inline] +pub fn default_span_name<'a>(req: &'a Request, ext: &'a Extensions) -> Cow<'a, str> { + if let Some(name) = ext.get::() { + Cow::Borrowed(name.0.as_ref()) + } else if let Some(path_names) = ext.get::() { + path_names + .find(req.url().path()) + .map(|path| Cow::Owned(format!("{} {}", req.method(), path))) + .unwrap_or_else(|| { + warn!("no OTEL path name found"); + Cow::Owned(format!("{} UNKNOWN", req.method().as_str())) + }) + } else { + Cow::Borrowed(req.method().as_str()) + } +} + /// The default [`ReqwestOtelSpanBackend`] for [`TracingMiddleware`]. Note that it doesn't include /// the `http.url` field in spans, you can use [`SpanBackendWithUrl`] to add it. /// @@ -92,20 +113,7 @@ pub struct DefaultSpanBackend; impl ReqwestOtelSpanBackend for DefaultSpanBackend { fn on_request_start(req: &Request, ext: &mut Extensions) -> Span { - let name = if let Some(name) = ext.get::() { - Cow::Borrowed(name.0.as_ref()) - } else if let Some(path_names) = ext.get::() { - path_names - .find(req.url().path()) - .map(|path| Cow::Owned(format!("{} {}", req.method(), path))) - .unwrap_or_else(|| { - warn!("no OTEL path name found"); - Cow::Owned(format!("{} UNKNOWN", req.method().as_str())) - }) - } else { - Cow::Borrowed(req.method().as_str()) - }; - + let name = default_span_name(req, ext); reqwest_otel_span!(name = name, req) } @@ -126,20 +134,7 @@ pub struct SpanBackendWithUrl; impl ReqwestOtelSpanBackend for SpanBackendWithUrl { fn on_request_start(req: &Request, ext: &mut Extensions) -> Span { - let name = if let Some(name) = ext.get::() { - Cow::Borrowed(name.0.as_ref()) - } else if let Some(path_names) = ext.get::() { - path_names - .find(req.url().path()) - .map(|path| Cow::Owned(format!("{} {}", req.method(), path))) - .unwrap_or_else(|| { - warn!("no OTEL path name found"); - Cow::Owned(format!("{} UNKNOWN", req.method().as_str())) - }) - } else { - Cow::Borrowed(req.method().as_str()) - }; - + let name = default_span_name(req, ext); reqwest_otel_span!(name = name, req, http.url = %remove_credentials(req.url())) }