LukeMathWalker/tracing-actix-web

Omitting fields and using string constant for field naming

tumbl3w33d opened this issue · 3 comments

I just started using tracing mainly for automatically adding a request id to the log output, but I have the special requirement to log it with a certain field name. I was able to add it with a custom RootSpanBuilder as described in the documentation. Unfortunately, I was not able to get rid of the original field request_id that already exists. I've tried setting it to Empty in two places, but it has no effect:

impl RootSpanBuilder for MyRootSpanBuilder {
    fn on_request_start(request: &ServiceRequest) -> Span {
        let req_id = get_req_id_from_req(request);
        let request_id = req_id.as_str();
        tracing_actix_web::root_span!(request, "X-Request-ID" = &request_id, request_id = Empty)
    }

    fn on_request_end<B>(span: Span, outcome: &Result<ServiceResponse<B>, Error>) {
        let _enter = span.in_scope(|| {
            span.record("request_id", &Empty);
        });
        DefaultRootSpanBuilder::on_request_end(span, outcome);
    }
}

Is there an easier way to omit a few (not all) of the original fields?

P.S.: Since I'm already asking about this snippet… I have a constant for my header name…

pub(crate) const HEADER_REQUEST_ID: &str = "X-Request-ID";

that I would like to use to name the field. As you can see, I needed to write a literal value there (X-Request-ID). When I use the constant, it uses the constant name as field name instead of the content value.

Unfortunately, I was not able to get rid of the original field request_id that already exists.
Is there an easier way to omit a few (not all) of the original fields?

I don't think there is an easy way to accomplish it at the moment.

As you can see, I needed to write a literal value there (X-Request-ID). When I use the constant, it uses the constant name as field name instead of the content value.

I have to double-check, but I am fairly confident this is a limitation of tracing.

Okay, thank you for clarifying, so I don't need to spend more time on trying. :)

Closing this one, given there is nothing to be done on tracing-actix-web at the moment.