open-telemetry / opentelemetry-specification

Specifications for OpenTelemetry
https://opentelemetry.io
Apache License 2.0
3.75k stars 890 forks source link

Provide a way for knowing when a Context is made current #1163

Open HaloFour opened 4 years ago

HaloFour commented 4 years ago

What are you trying to achieve?

I need to know whenever the "current" Span changes for a given thread so that I can apply the trace ID and span ID to the thread local context for the purposes of logging. It is a functional requirement that our logs and spans can be correlated to one another. I am currently achieving this by decorating the Tracer Java interface and intercepting the withSpan method. However, that method has been removed from the specification and will be removed in the next release of the Java OpenTelemetry API. With the new Context API being static and largely self-contained it doesn't seem like there is a way to intercept when a given Context is made current or when the resulting scope is closed.

Oberon00 commented 4 years ago

Can't you work the other way round, i.e. whenever you log something, access the current OpenTelemetry context?

HaloFour commented 4 years ago

We have at least several thousand separate log lines written by our Spring services. That does not include the logs written by the frameworks themselves which is code that we can't control. I'd suggest that's impractical, if not impossible, to change our logging in such a manner.

Oberon00 commented 4 years ago

But you presumably only have one logger framework. Those usually allow you to hook into logging in such a way that you can capture context information when a log line is generated.

jkwatson commented 4 years ago

Corresponding issue in the java repo: https://github.com/open-telemetry/opentelemetry-java/issues/922

HaloFour commented 4 years ago

Our services use multiple logging frameworks, and we use slf4j to abstract away those differences. The slf4j library offers no way to intercept log lines being written, it would have to be done differently for each logging framework.

tsloughter commented 4 years ago

An OnSetToCurrent span processor might be nice. Having that added when BeforeEnd is added after GA would make sense, I think.

At first I didn't think it would be too useful in Erlang but it actually might be worth it so that the logger doesn't have to read it from the context on each call but instead can have it set to the logger's process metadata each time a different span becomes the current span in the process.

Oberon00 commented 4 years ago

This would, if added, be in the Context layer. It would not be a Span-specific callback.

tigrannajaryan commented 3 years ago

Related to https://github.com/open-telemetry/opentelemetry-specification/issues/894