[Following up on https://github.com/scala/bug/issues/9835]
Scala seems to lack a blessed way to propagate request-scoped values through an execution path. Canonical use case is to propagate a traceId for log correlation, and when sent to upstream services via http header or similar, for distributed tracing. I’m aware of 4 approaches:
Use the call stack to pass it, perhaps using an implicit parameter. Pro: it’s straightforward and easy to reason about. Con: Will only work with code that declares the parameter, typically user code and not library code. This is the approach the Go designers advocate via context.Context.
Inject an overloaded ExecutionContext.prepare() that propagates ThreadLocal Storage (TLS) across threads, as originally demonstrated here https://yanns.github.io/blog/2014/05/04/slf4j-mapped-diagnostic-context-mdc-with-play-framework/ Pro: no change to user code. Con: fragile if app does not have complete control of ExecutionContexts in use, and prepare() is now deprecated.
Kamon, and I think Cinnamon, are similar to above in their use of TLS, but work at runtime via bytecode weaving to modify Future internals to inject the propagation. Pro: no change to user code. Con: bytecode weaving and TLS complexity, Cinnamon is not free to use.
Twitter/Finagle locals also use TLS, but their Future implementation propagates them across thread boundaries. I don’t have hands-on experience with this approach. Pro: SDK support. Con: approach seems good but requires commitment to Finagle stack.
I believe the Twitter approach was considered when designing scala Futures, but not sure why it was rejected. I’m curious what the reasons were, and if a similar mechanism would be considered at some point.
And more generally, can the Scala community promote a recommended approach for context propagation? Given the tradeoffs of current choices, I don’t see an obvious best answer, and I think better guidance would lead to user code and libraries and tools working together to deliver runtime observability of multi-threaded and distributed systems.