taoensso / timbre

Pure Clojure/Script logging library
https://www.taoensso.com/timbre
Eclipse Public License 1.0
1.44k stars 171 forks source link

How to add traceid/spanid to logs? #348

Closed crinklywrappr closed 1 year ago

crinklywrappr commented 2 years ago

https://docs.datadoghq.com/tracing/connect_logs_and_traces/java/?tab=log4j2

is with-context the right way to do this?

ptaoussanis commented 2 years ago

Hi Daniel! Sorry I'm not quite sure what you're asking, could you please expand a little on what you're trying to do?

crinklywrappr commented 2 years ago

traceid & spanid allows datadog to correlate logs with higher-level things eg HTTP requests and database queries.

They provide instructions (in the link provided) how to add traceid and spanid to log4j/slf4j logs.

I would like to do the same with timbre but don't know how.

ptaoussanis commented 2 years ago

I can try offer some ideas, though the best solution will depend on the details of your setup and the specific tradeoffs you'd prefer to make.

It sounds like the goal is to have some sort of useful logical id available at the relevant logging callsites. That's something you'd need to design yourself - it's outside the scope of a logging library.

Common options would include setting up the relevant logical id context using binding and your own dynamic var, or using Timbre's with-context (which is itself just a dynamic var).

If you're talking about a Ring server, this pattern is often quite easy to do with a middleware that setups up the dynamic context before calling subsequent request handlers.

Though the semantics of what constitutes a logical span/id, how these ids are generated, etc. are all quite app dependent.

Hope that helps!

crinklywrappr commented 1 year ago

Thanks!