mirror of
https://github.com/TrueLayer/reqwest-middleware.git
synced 2024-12-26 10:56:32 +00:00
add default_span_name helper function (#93)
This commit is contained in:
parent
385314a298
commit
fb7a964ba3
4 changed files with 33 additions and 32 deletions
|
@ -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
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "reqwest-tracing"
|
||||
version = "0.4.3"
|
||||
version = "0.4.4"
|
||||
authors = ["Rodrigo Gryzinski <rodrigo.gryzinski@truelayer.com>"]
|
||||
edition = "2018"
|
||||
description = "Opentracing middleware for reqwest."
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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
|
||||
/// the `http.url` field in spans, you can use [`SpanBackendWithUrl`] to add it.
|
||||
/// Determine the name of the span that should be associated with this request.
|
||||
///
|
||||
/// [`TracingMiddleware`]: crate::middleware::TracingMiddleware
|
||||
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::<OtelName>() {
|
||||
/// 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::<OtelName>() {
|
||||
Cow::Borrowed(name.0.as_ref())
|
||||
} else if let Some(path_names) = ext.get::<OtelPathNames>() {
|
||||
path_names
|
||||
|
@ -104,8 +102,18 @@ impl ReqwestOtelSpanBackend for DefaultSpanBackend {
|
|||
})
|
||||
} 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.
|
||||
///
|
||||
/// [`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)
|
||||
}
|
||||
|
||||
|
@ -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::<OtelName>() {
|
||||
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())
|
||||
};
|
||||
|
||||
let name = default_span_name(req, ext);
|
||||
reqwest_otel_span!(name = name, req, http.url = %remove_credentials(req.url()))
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue