Skip to content

Commit

Permalink
Implement Sentry.continue_trace and deprecate Sentry.from_sentry_trace
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 10, 2023
1 parent 8c62943 commit 5e6ffc8
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
9 changes: 9 additions & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ def get_baggage
get_current_hub.get_baggage
end

# Continue an incoming trace from a rack env like hash.
#
# @param env [Hash]
# @return [Transaction, nil]
def continue_trace(env, **options)
return nil unless initialized?
get_current_hub.continue_trace(env, **options)

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

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L515-L516

Added lines #L515 - L516 were not covered by tests
end

##### Helpers #####

# @!visibility private
Expand Down
18 changes: 18 additions & 0 deletions sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,24 @@ def get_baggage
current_scope.propagation_context&.get_baggage&.serialize
end

def continue_trace(env, **options)
configure_scope { |s| s.generate_propagation_context(env) }

Check warning on line 247 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L247

Added line #L247 was not covered by tests

return nil unless configuration.tracing_enabled?

Check warning on line 249 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L249

Added line #L249 was not covered by tests

propagation_context = current_scope.propagation_context
return nil unless propagation_context.incoming_trace

Check warning on line 252 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L251-L252

Added lines #L251 - L252 were not covered by tests

Transaction.new(

Check warning on line 254 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L254

Added line #L254 was not covered by tests
hub: self,
trace_id: propagation_context.trace_id,
parent_span_id: propagation_context.parent_span_id,
parent_sampled: propagation_context.parent_sampled,
baggage: propagation_context.baggage,
**options
)
end

private

def current_layer
Expand Down
22 changes: 17 additions & 5 deletions sentry-ruby/lib/sentry/propagation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,35 @@ class PropagationContext
# @return [String]
attr_reader :span_id
# Span parent's span_id.
# @return [String]
# @return [String, nil]
attr_reader :parent_span_id
# The sampling decision of the parent transaction.
# @return [Boolean, nil]
attr_reader :parent_sampled
# Is there an incoming trace or not?
# @return [Boolean]
attr_reader :incoming_trace
# This is only for accessing the current baggage variable.
# Please use the #get_baggage method for interfacing outside this class.
# @return [Baggage, nil]
attr_reader :baggage

def initialize(scope, env = nil)
@scope = scope
@parent_span_id = nil
@parent_sampled = nil
@baggage = nil
@incoming_trace = false

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

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

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L44-L45

Added lines #L44 - L45 were not covered by tests

if sentry_trace_header
sentry_trace_data = extract_sentry_trace(sentry_trace_header)
sentry_trace_data = self.class.extract_sentry_trace(sentry_trace_header)

Check warning on line 48 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-L48

Added lines #L47 - L48 were not covered by tests

if sentry_trace_data
@trace_id, @parent_span_id, _ = sentry_trace_data
@trace_id, @parent_span_id, @parent_sampled = sentry_trace_data

Check warning on line 51 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-L51

Added lines #L50 - L51 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L53-L54

Added lines #L53 - L54 were not covered by tests
Expand All @@ -48,6 +60,7 @@ def initialize(scope, env = nil)
end

@baggage.freeze!
@incoming_trace = true

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

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/propagation_context.rb#L62-L63

Added lines #L62 - L63 were not covered by tests
end
end
end
Expand All @@ -70,7 +83,6 @@ def self.extract_sentry_trace(sentry_trace)
[trace_id, parent_span_id, parent_sampled]
end


# Returns the trace context that can be used to embed in an Event.
# @return [Hash]
def get_trace_context
Expand All @@ -87,7 +99,7 @@ def get_traceparent
"#{trace_id}-#{span_id}"
end

# Returns the Baggage from the propagation context.
# Returns the Baggage from the propagation context or populates as head SDK if empty.
# @return [Baggage, nil]
def get_baggage
populate_head_baggage if @baggage.nil? || @baggage.mutable
Expand Down
5 changes: 1 addition & 4 deletions sentry-ruby/lib/sentry/rack/capture_exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ def capture_exception(exception, env)
end

def start_transaction(env, scope)
sentry_trace = env["HTTP_SENTRY_TRACE"]
baggage = env["HTTP_BAGGAGE"]

options = { name: scope.transaction_name, source: scope.transaction_source, op: transaction_op }
transaction = Sentry::Transaction.from_sentry_trace(sentry_trace, baggage: baggage, **options) if sentry_trace
transaction = Sentry.continue_trace(env, **options)

Check warning on line 66 in sentry-ruby/lib/sentry/rack/capture_exceptions.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/rack/capture_exceptions.rb#L66

Added line #L66 was not covered by tests
Sentry.start_transaction(transaction: transaction, custom_sampling_context: { env: env }, **options)
end

Expand Down
2 changes: 2 additions & 0 deletions sentry-ruby/lib/sentry/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ def initialize(
init_span_recorder
end

# @deprecated use Sentry.continue_trace instead.
#
# Initalizes a Transaction instance with a Sentry trace string from another transaction (usually from an external request).
#
# The original transaction will become the parent of the new Transaction instance. And they will share the same `trace_id`.
Expand Down

0 comments on commit 5e6ffc8

Please sign in to comment.