adding trace_id field to all emited logs using tokio tracing

1k Views Asked by At

Having trace_id field on all log events is common practice in distributed applications. This enables us to be able to follow logs for a specific request in the application.

I have an actix-web middleware that extracts a header from the http requests (X-Amzn-Trace-Id: Root=1-63441c4a-abcdef012345678912345678) and sets the string content to a tokio tracing span field called trace_id.

I can't figure out a way to have this field on all log events.

Potential approaches:

  • Use request extensions and pass trace_id as a function parameter down (doesn't seem clean)
  • log all spans in the spans array field (inefficient as you will log a lot of duplicate data as all the child spans will have parent spans logged)
  • somehow propagate trace_id field to all child spans, this way we can only log the current span and have trace_id field as well.
0

There are 0 best solutions below