How to exclude routes from tracing? #355
-
I use axum with tower-http for tracing, e.g.: let app = Router::new()
.route("/", post(MyController::create))
.route("/health", get(health))
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &Request<Body>| {
tracing::info!("http request");
tracing::info_span!(
"http-request",
...
})
.on_response(
|response: &Response<BoxBody>, latency: Duration, span: &Span| {
span.record("http.status_code", &display(response.status()));
tracing::info!("http response");
})
)
axum::Server::bind(addr)
.serve(app.into_make_service())
.await How can i exclude tracing from |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Add the let app = Router::new()
.route("/", post(MyController::create))
.layer(...)
.route("/health", get(health)); See the example for |
Beta Was this translation helpful? Give feedback.
-
Hi @davidpdrsn Any idea how to skip /health but still pass the Extension(pool) and ignore the trace (request) ? let app = Router::new()
.route("/", get(handlers::main).options(handlers::health)))
.layer(
ServiceBuilder::new()
.layer(Extension(pool))
.layer(TraceLayer::new_for_http())
.layer(cors),
)
.route("/health", get(handlers::health).options(handlers::health))
.merge(swagger); But since I need the database (pool) I get now this error:
Adding the layer only to the handler works but I still get request traces: .route("/health", get(handlers::health).options(handlers::health));
.layer(Extension(pool)) So wondering how to remove the request: [
{
"name": "started processing request",
"time": "2024-02-20T07:04:27.091193689Z",
"attrs": {
"code.filepath": "/root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tower-http-0.5.1/src/trace/on_request.rs",
"code.lineno": "80",
"code.namespace": "tower_http::trace::on_request",
"level": "DEBUG",
"target": "tower_http::trace::on_request"
}
},
{
"name": "finished processing request",
"time": "2024-02-20T07:04:27.091244771Z",
"attrs": {
"code.filepath": "/root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tower-http-0.5.1/src/trace/on_response.rs",
"code.lineno": "114",
"code.namespace": "tower_http::trace::on_response",
"latency": "0 ms",
"level": "DEBUG",
"status": "200",
"target": "tower_http::trace::on_response"
}
}
] |
Beta Was this translation helpful? Give feedback.
Add the
/health
route after the tracing middleware:See the example for
Router::layer
: https://docs.rs/axum/0.2.5/axum/routing/struct.Router.html#method.layer