add default_span_name helper function (#93)

This commit is contained in:
Conrad Ludgate 2023-05-15 14:55:56 +01:00 committed by GitHub
parent 385314a298
commit fb7a964ba3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 32 deletions

View file

@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [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 ## [0.4.3] - 2023-05-15
### Fixed ### Fixed

View file

@ -1,6 +1,6 @@
[package] [package]
name = "reqwest-tracing" name = "reqwest-tracing"
version = "0.4.3" version = "0.4.4"
authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"] authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"]
edition = "2018" edition = "2018"
description = "Opentracing middleware for reqwest." description = "Opentracing middleware for reqwest."

View file

@ -96,9 +96,10 @@ mod reqwest_otel_span_builder;
pub use middleware::TracingMiddleware; pub use middleware::TracingMiddleware;
pub use reqwest_otel_span_builder::{ pub use reqwest_otel_span_builder::{
default_on_request_end, default_on_request_failure, default_on_request_success, default_on_request_end, default_on_request_failure, default_on_request_success,
DefaultSpanBackend, OtelName, OtelPathNames, ReqwestOtelSpanBackend, SpanBackendWithUrl, default_span_name, DefaultSpanBackend, OtelName, OtelPathNames, ReqwestOtelSpanBackend,
ERROR_CAUSE_CHAIN, ERROR_MESSAGE, HTTP_HOST, HTTP_METHOD, HTTP_SCHEME, HTTP_STATUS_CODE, SpanBackendWithUrl, ERROR_CAUSE_CHAIN, ERROR_MESSAGE, HTTP_HOST, HTTP_METHOD, HTTP_SCHEME,
HTTP_URL, HTTP_USER_AGENT, NET_HOST_PORT, OTEL_KIND, OTEL_NAME, OTEL_STATUS_CODE, HTTP_STATUS_CODE, HTTP_URL, HTTP_USER_AGENT, NET_HOST_PORT, OTEL_KIND, OTEL_NAME,
OTEL_STATUS_CODE,
}; };
#[doc(hidden)] #[doc(hidden)]

View file

@ -84,15 +84,13 @@ pub fn default_on_request_failure(span: &Span, e: &Error) {
} }
} }
/// The default [`ReqwestOtelSpanBackend`] for [`TracingMiddleware`]. Note that it doesn't include /// Determine the name of the span that should be associated with this request.
/// the `http.url` field in spans, you can use [`SpanBackendWithUrl`] to add it.
/// ///
/// [`TracingMiddleware`]: crate::middleware::TracingMiddleware /// This tries to be PII safe by default, not including any path information unless
pub struct DefaultSpanBackend; /// specifically opted in using either [`OtelName`] or [`OtelPathNames`]
#[inline]
impl ReqwestOtelSpanBackend for DefaultSpanBackend { pub fn default_span_name<'a>(req: &'a Request, ext: &'a Extensions) -> Cow<'a, str> {
fn on_request_start(req: &Request, ext: &mut Extensions) -> Span { if let Some(name) = ext.get::<OtelName>() {
let name = if let Some(name) = ext.get::<OtelName>() {
Cow::Borrowed(name.0.as_ref()) Cow::Borrowed(name.0.as_ref())
} else if let Some(path_names) = ext.get::<OtelPathNames>() { } else if let Some(path_names) = ext.get::<OtelPathNames>() {
path_names path_names
@ -104,8 +102,18 @@ impl ReqwestOtelSpanBackend for DefaultSpanBackend {
}) })
} else { } else {
Cow::Borrowed(req.method().as_str()) 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.
///
/// [`TracingMiddleware`]: crate::middleware::TracingMiddleware
pub struct DefaultSpanBackend;
impl ReqwestOtelSpanBackend for DefaultSpanBackend {
fn on_request_start(req: &Request, ext: &mut Extensions) -> Span {
let name = default_span_name(req, ext);
reqwest_otel_span!(name = name, req) reqwest_otel_span!(name = name, req)
} }
@ -126,20 +134,7 @@ pub struct SpanBackendWithUrl;
impl ReqwestOtelSpanBackend for SpanBackendWithUrl { impl ReqwestOtelSpanBackend for SpanBackendWithUrl {
fn on_request_start(req: &Request, ext: &mut Extensions) -> Span { fn on_request_start(req: &Request, ext: &mut Extensions) -> Span {
let name = if let Some(name) = ext.get::<OtelName>() { let name = default_span_name(req, ext);
Cow::Borrowed(name.0.as_ref())
} else if let Some(path_names) = ext.get::<OtelPathNames>() {
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())
};
reqwest_otel_span!(name = name, req, http.url = %remove_credentials(req.url())) reqwest_otel_span!(name = name, req, http.url = %remove_credentials(req.url()))
} }