-
Notifications
You must be signed in to change notification settings - Fork 246
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
rfc: Allow reading current context for another thread #842
rfc: Allow reading current context for another thread #842
Conversation
I've been working on integrating Datadog's continuous profiler with opentelemetry traces (see DataDog/dd-trace-rb#1568). The profiler runs on a background thread, and needs to be able to access the current context for a thread, to be able to get the current span's trace id and span ids (if any active). To do so, I was originally using ```ruby thread = ... ::OpenTelemetry::Trace.current_span(thread[::OpenTelemetry::Context::KEY]) ``` Unfortunately, after open-telemetry#807, this interface was changed, and more importantly, `Context::KEY` was removed and replaced with `Context::STACK_KEY`. `STACK_KEY` was marked as `private_constant`. With 1.0.0.rc2, the only way of getting this information is by relying on private implementation details, which isn't great. Thus, I would like to ask if it'd be reasonable to add an optional `thread` parameter to `Context.current`. This would make it easy to access the needed information, and it would even be more future-proof as the outside code doesn't need to care anymore where and how the context is stored.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hey friends, @ivoanjo is a (much brighter) colleague of mine. This change seems fine to me in terms of complying with the Specification as defined https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/context.md
But I am concerned whether the change (by not calling stack
directly we don't initialize storage), is a concern or has any impact elsewhere, as it's a change in behavior if i understand correctly?
@fbogsany @robertlaurin wdyt?
I'm mildly terrified by this proposal. Accessing another thread's thread locals seems fraught with problems, and it is pretty violating too. I need to 🤔 about this more. Some initial poking around turned up a bug in Puma earlier this year, triggering a segfault in ruby 2.6 when accessing other threads' thread locals. That doesn't inspire confidence. Any such segfault will point to otel ruby as the culprit. |
To be fair, any issues would only appear when the API is exercised, so if anything, I'd expect a "we enabled the profiler and our app broke" than the reverse 🤣 . The stack trace will also be pointing at the profiler calling into otel-ruby. Ruby being a managed VM, a segfault should never happen. The Ruby team seems to be quite responsive to those kinds of issues, so I'm sure such reports would be treated seriously. Is there anything I can add that would help to derisk this in your view? |
@ivoanjo We talked about this today in the Ruby SIG - we'd like to continue to push back since we think this is largely only safe in MRI and we can't guarantee it wouldn't blow up in other implementations. Given that dd-trace will need to monkey-patch If it turns out to be impossible, we may be able to provide a "safety off" flag that still allows this, but we'd prefer not to if possible. |
Thanks for the feedback!
I was actually not planning on going the monkey patch route to provide the correlation. Instead, I created a "Span Processor" that users added in their configuration block and that would add the needed information on
There's actually no need for a monkey patch. Since this is Ruby, I can always reach in and access the My concern is that, well I spent some time brainstorming some alternative approaches:
|
There are differences other than just performance, specifically around threading (concurrent-ruby exists to abstract these differences) and processes (e.g. There is a 5th option, which is for you to completely replace the We're currently quite close to a 1.0 release for Tracing, and our highest priorities are stability, spec compliance, and minimizing the surface area of the API we have to support. Adding an optional |
@ivoanjo and I talked a little bit this morning and the general outcome was:
I think I'm going to take a stab at option 2 this week, just to see if I can come up with something that seems reasonable. I'd be interested if others have additional thoughts here... I'm taking this as an opportunity for us to start building out some debug helpers in the SDK for folks, which seem like something we'll want as it grows in popularity. 😄 |
That was my intent, yes. It is not a large class, and it is self-contained - nothing in the API is reaching into its internals. As long as you match the public API (which we have to commit to supporting for something like 3 years IIRC), you should be fine ™️ . |
👋 This pull request has been marked as stale because it has been open with no activity. You can: comment on the issue or remove the stale label to hold stale off for a while, add the |
I've been working on integrating Datadog's continuous profiler with opentelemetry traces (see DataDog/dd-trace-rb#1568).
The profiler runs on a background thread, and needs to be able to access the current context for a thread, to be able to get the current span's trace id and span ids (if any active).
To do so, I was originally using
Unfortunately, after #807, this interface was changed, and more importantly,
Context::KEY
was removed and replaced withContext::STACK_KEY
.STACK_KEY
was marked asprivate_constant
.With 1.0.0.rc2, the only way of getting this information is by relying on private implementation details, which isn't great.
Thus, I would like to ask if it'd be reasonable to add an optional
thread
parameter toContext.current
. This would make it easy to access the needed information, and it would even be more future-proof as the outside code doesn't need to care anymore where and how the context is stored.