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

How to include request span trace id in log? #38

Open
npuichigo opened this issue Nov 1, 2023 · 2 comments
Open

How to include request span trace id in log? #38

npuichigo opened this issue Nov 1, 2023 · 2 comments

Comments

@npuichigo
Copy link

npuichigo commented Nov 1, 2023

I use axum and opentelemetry, currently I include the trace id in response header with middleware's help. But how can I also include that trace id in the output of this formatter?

The trace id in response header can be used with Jaeger for searching, but I would like to locate in logs as well.

sample code use opentelemetry: https://github.com/AmineDiro/cria/blob/5b80514171930725bfb57212f37243e16c4394ba/src/lib.rs#L95

@LukeMathWalker
Copy link
Owner

This formatter outputs whatever is attached to the span—there's no special work or setup you need to do here. If the field is correctly attached to a span, it'll show up in the output.

@npuichigo
Copy link
Author

Thanks. I used tower-http middleware to include both request/respond headers to attach to a span and it works now to show both request_id added as x-request-id and trace_id added by opentelemetry.

let svc = ServiceBuilder::new()
    // make sure to set request ids before the request reaches `TraceLayer`
    .set_x_request_id(MyMakeRequestId::default())
    // log requests and responses
    .layer(
        TraceLayer::new_for_http()
            .make_span_with(DefaultMakeSpan::new().include_headers(true))
            .on_response(DefaultOnResponse::new().include_headers(true))
    )
    // propagate the header to the response before the response reaches `TraceLayer`
    .propagate_x_request_id()
    .service(handler);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants