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 10, 2023
1 parent 6150e0e commit 8c62943
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"]

Check warning on line 33 in sentry-ruby/lib/sentry/propagation_context.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L32-L33

Added lines #L32 - L33 were not covered by tests

if sentry_trace_header
sentry_trace_data = extract_sentry_trace(sentry_trace_header)

Check warning on line 36 in sentry-ruby/lib/sentry/propagation_context.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L35-L36

Added lines #L35 - L36 were not covered by tests

if sentry_trace_data
@trace_id, @parent_span_id, _ = sentry_trace_data

Check warning on line 39 in sentry-ruby/lib/sentry/propagation_context.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L38-L39

Added lines #L38 - L39 were not covered by tests

@baggage = if baggage_header && !baggage_header.empty?
Baggage.from_incoming_header(baggage_header)

Check warning on line 42 in sentry-ruby/lib/sentry/propagation_context.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L41-L42

Added lines #L41 - L42 were not covered by tests
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({})

Check warning on line 47 in sentry-ruby/lib/sentry/propagation_context.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L47

Added line #L47 was not covered by tests
end

@baggage.freeze!

Check warning on line 50 in sentry-ruby/lib/sentry/propagation_context.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L50

Added line #L50 was not covered by tests
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 8c62943

Please sign in to comment.