Skip to content

Commit

Permalink
Support passing env to PropagationContext.new
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 16, 2023
1 parent 944e8db commit 4a20fb7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
31 changes: 28 additions & 3 deletions sentry-ruby/lib/sentry/propagation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,37 @@ class PropagationContext
# @return [String]
attr_reader :parent_span_id

def initialize(scope)
def initialize(scope, env = nil)
@scope = scope
@trace_id = SecureRandom.uuid.delete("-")
@span_id = SecureRandom.uuid.delete("-").slice(0, 16)
@parent_span_id = nil
@baggage = nil

if env
sentry_trace_header = env["HTTP_SENTRY_TRACE"]
baggage_header = env["HTTP_BAGGAGE"]

if sentry_trace_header
sentry_trace_data = extract_sentry_trace(sentry_trace_header)

if sentry_trace_data
@trace_id, @parent_span_id, _ = sentry_trace_data

@baggage = if baggage_header && !baggage_header.empty?
Baggage.from_incoming_header(baggage_header)
else
# If there's an incoming sentry-trace but no incoming baggage header,
# for instance in traces coming from older SDKs,
# baggage will be empty and frozen and won't be populated as head SDK.
Baggage.new({})
end

@baggage.freeze!
end
end
end

@trace_id ||= SecureRandom.uuid.delete("-")
@span_id = SecureRandom.uuid.delete("-").slice(0, 16)
end

# Extract the trace_id, parent_span_id and parent_sampled values from a sentry-trace header.
Expand Down
11 changes: 7 additions & 4 deletions sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,13 @@ def add_event_processor(&block)
@event_processors << block
end

# Generate a new propagation context either from the incoming env headers or from scratch.
# @param env [Hash, nil]
# @return [void]
def generate_propagation_context(env = nil)
@propagation_context = PropagationContext.new(self, env)
end

protected

# for duplicating scopes internally
Expand Down Expand Up @@ -307,10 +314,6 @@ def set_new_breadcrumb_buffer
@breadcrumbs = BreadcrumbBuffer.new(@max_breadcrumbs)
end

def generate_propagation_context
@propagation_context = PropagationContext.new(self)
end

class << self
# @return [Hash]
def os_context
Expand Down

0 comments on commit 4a20fb7

Please sign in to comment.