LukeMathWalker/tracing-actix-web

How to ignore logging and tracing for a specific actix endpoint (/health)

cjrh opened this issue · 2 comments

cjrh commented

Hi

My actix-web health endpoint looks like this:

#[get("/health")]
async fn health() -> actix_web::Result<impl Responder> {
    Ok(HttpResponse::Ok())
}

Following the instructions for tracing-actix-web works great and all my endpoints get log messages, but I want to exclude this endpoint from tracing and logging.

In response to a similar question here you said:

  • Create a debug level span instead of an info level span when the route matches a certain path (i.e. this is how we do it in several codebases at my workplace);
  • Use per-layer filters in tracing to match on properties like http.endpoint to filter out the span when exporting.

If it's easy for you to do, could you copy-paste an example of either of these approaches? I've been trying to set up a tracing_subscriber::filter::filter_fn to filter out logs but for the life of me I cannot seem to actually read the contents of log fields, only the metadata. Alternatively if you had an example of configuring a filter_fn in a way that can read the message text, that would also be much appreciated.

I've implemented level customisation 😁
You can find an example in the README now:

use actix_web::dev::{ServiceResponse, ServiceRequest};
use actix_web::Error;
use tracing_actix_web::{TracingLogger, DefaultRootSpanBuilder, RootSpanBuilder, Level};
use tracing::Span;

pub struct CustomLevelRootSpanBuilder;

impl RootSpanBuilder for CustomLevelRootSpanBuilder {
    fn on_request_start(request: &ServiceRequest) -> Span {
        let level = if request.path() == "/health_check" {
            Level::DEBUG
        } else {
            Level::INFO
        };
        tracing_actix_web::root_span!(level = level, request)
    }

    fn on_request_end<B>(span: Span, outcome: &Result<ServiceResponse<B>, Error>) {
        DefaultRootSpanBuilder::on_request_end(span, outcome);
    }
}

let custom_middleware = TracingLogger::<CustomLevelRootSpanBuilder>::new();
cjrh commented

Thank you! ❤️