Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to control level of Tracing spans #124

Merged
merged 6 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tower-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- None.
- `Trace`: Add `DefaultMakeSpan::level` to make log level of tracing spans easily configurable ([#124])

[#124]: https://github.com/tower-rs/tower-http/pull/124

# 0.1.2 (November 13, 2021)

Expand Down
81 changes: 64 additions & 17 deletions tower-http/src/trace/make_span.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use http::Request;
use tracing::Span;
use tracing::{Level, Span};

use super::DEFAULT_MESSAGE_LEVEL;

/// Trait used to generate [`Span`]s from requests. [`Trace`] wraps all request handling in this
/// span.
Expand Down Expand Up @@ -30,19 +32,31 @@ where
///
/// [`Span`]: tracing::Span
/// [`Trace`]: super::Trace
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct DefaultMakeSpan {
level: Level,
include_headers: bool,
}

impl DefaultMakeSpan {
/// Create a new `DefaultMakeSpan`.
pub fn new() -> Self {
Self {
level: DEFAULT_MESSAGE_LEVEL,
include_headers: false,
}
}

/// Set the [`Level`] used for the [tracing span].
///
/// Defaults to [`Level::DEBUG`].
///
/// [tracing span]: https://docs.rs/tracing/latest/tracing/#spans
pub fn level(mut self, level: Level) -> Self {
self.level = level;
self
}

/// Include request headers on the [`Span`].
///
/// By default headers are not included.
Expand All @@ -54,23 +68,56 @@ impl DefaultMakeSpan {
}
}

impl Default for DefaultMakeSpan {
fn default() -> Self {
Self::new()
}
}

impl<B> MakeSpan<B> for DefaultMakeSpan {
fn make_span(&mut self, request: &Request<B>) -> Span {
if self.include_headers {
tracing::debug_span!(
"request",
method = %request.method(),
uri = %request.uri(),
version = ?request.version(),
headers = ?request.headers(),
)
} else {
tracing::debug_span!(
"request",
method = %request.method(),
uri = %request.uri(),
version = ?request.version(),
)
// This ugly macro is needed, unfortunately, because `tracing::span!`
// required the level argument to be static. Meaning we can't just pass
// `self.level`.
macro_rules! make_span {
($level:expr) => {
if self.include_headers {
tracing::span!(
$level,
"request",
method = %request.method(),
uri = %request.uri(),
version = ?request.version(),
headers = ?request.headers(),
)
} else {
tracing::span!(
$level,
"request",
method = %request.method(),
uri = %request.uri(),
version = ?request.version(),
)
}
}
}

match self.level {
Level::ERROR => {
make_span!(Level::ERROR)
}
Level::WARN => {
make_span!(Level::WARN)
}
Level::INFO => {
make_span!(Level::INFO)
}
Level::DEBUG => {
make_span!(Level::DEBUG)
}
Level::TRACE => {
make_span!(Level::TRACE)
}
}
}
}
7 changes: 6 additions & 1 deletion tower-http/src/trace/on_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,15 @@ impl DefaultOnRequest {

/// Set the [`Level`] used for [tracing events].
///
/// Please note that while this will set the level for the tracing events
/// themselves, it might cause them to lack expected information, like
/// request method or path. You can address this using
/// [`DefaultMakeSpan::level`].
///
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
/// Defaults to [`Level::DEBUG`].
///
/// [tracing events]: https://docs.rs/tracing/latest/tracing/#events
/// [`Level::DEBUG`]: https://docs.rs/tracing/latest/tracing/struct.Level.html#associatedconstant.DEBUG
/// [`DefaultMakeSpan::level`]: crate::trace::DefaultMakeSpan::level
pub fn level(mut self, level: Level) -> Self {
self.level = level;
self
Expand Down
7 changes: 6 additions & 1 deletion tower-http/src/trace/on_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ impl DefaultOnResponse {

/// Set the [`Level`] used for [tracing events].
///
/// Please note that while this will set the level for the tracing events
/// themselves, it might cause them to lack expected information, like
/// request method or path. You can address this using
/// [`DefaultMakeSpan::level`].
///
/// Defaults to [`Level::DEBUG`].
///
/// [tracing events]: https://docs.rs/tracing/latest/tracing/#events
/// [`Level::DEBUG`]: https://docs.rs/tracing/latest/tracing/struct.Level.html#associatedconstant.DEBUG
/// [`DefaultMakeSpan::level`]: crate::trace::DefaultMakeSpan::level
pub fn level(mut self, level: Level) -> Self {
self.level = level;
self
Expand Down