-
-
Notifications
You must be signed in to change notification settings - Fork 517
patch #2735
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
base: master
Are you sure you want to change the base?
patch #2735
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'sentry-ruby' | ||
require 'concurrent-ruby' | ||
|
||
module Sentry | ||
module ConcurrentRuby | ||
SENTRY_THREAD_KEY = :sentry_hub | ||
SENTRY_SPAN_KEY = :sentry_span | ||
|
||
def self.install | ||
patch_futures | ||
patch_promises | ||
end | ||
|
||
|
||
def self.patch_futures | ||
::Concurrent::Promises.singleton_class.class_eval do | ||
alias_method :original_future, :future | ||
|
||
def future(*args, &block) | ||
current_hub = Sentry.get_current_hub | ||
current_span = Sentry.get_current_scope.get_span | ||
original_future(*args) do | ||
#set the hub and span | ||
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_THREAD_KEY, current_hub) | ||
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_SPAN_KEY, current_span) | ||
Sentry.with_child_span(op: "api.request", description: "Concurrent::Future") do |child_span| | ||
Sentry.get_current_scope.set_span(child_span) | ||
block.call | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
def self.patch_promises | ||
::Concurrent::Promises::Future.class_eval do | ||
alias_method :original_on_resolution, :on_resolution | ||
|
||
def on_resolution(&block) | ||
current_hub = Thread.current.thread_variable_get(Sentry::ConcurrentRuby::SENTRY_THREAD_KEY) || Sentry.get_current_hub | ||
current_span = Sentry.get_current_scope.get_span | ||
original_on_resolution do |*args| | ||
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_THREAD_KEY, current_hub) | ||
Thread.current.thread_variable_set(Sentry::ConcurrentRuby::SENTRY_SPAN_KEY, current_span) | ||
Sentry.with_child_span(op: "api.request", description: "Concurrent::Promise") do |child_span| | ||
Sentry.get_current_scope.set_span(child_span) | ||
block.call(*args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Span Propagation InconsistencySpan context propagation is incomplete. In |
||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Install after Sentry loaded | ||
Sentry::ConcurrentRuby.install if defined?(Sentry) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential bug: The integration accesses
|
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.
Potential bug: The
on_resolution
method retrievescurrent_hub
from a thread variable but getscurrent_span
fromSentry.get_current_scope
, which may use a different hub, breaking trace context.Description: In the
on_resolution
method,current_hub
is retrieved from either a thread-local variable (SENTRY_THREAD_KEY
) orSentry.get_current_hub
. However,current_span
is always fetched viaSentry.get_current_scope.get_span
, which internally callsSentry.get_current_hub
. If the thread variable contains a hub different from the current thread's hub (e.g., when a promise chain crosses threads), the span will be associated with the wrong hub. This leads to an incorrect span hierarchy and broken tracing information. The implementation also ignores the stored span in theSENTRY_SPAN_KEY
thread variable.Suggested fix: Retrieve the span from the same source as the hub. When
current_hub
is loaded from the thread variable, the corresponding span should also be loaded from its thread variable (SENTRY_SPAN_KEY
) to ensure the hub and span contexts are consistent.severity: 0.6, confidence: 0.9
Did we get this right? 👍 / 👎 to inform future reviews.