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 16, 2023
1 parent cf81291 commit 4332930
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 @@ -516,6 +516,15 @@ def get_trace_propagation_headers
get_current_hub.get_trace_propagation_headers
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)
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 @@ -257,6 +257,24 @@ def get_trace_propagation_headers
headers
end

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

return nil unless configuration.tracing_enabled?

propagation_context = current_scope.propagation_context
return nil unless propagation_context.incoming_trace

Transaction.new(
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"]

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

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

@baggage = if baggage_header && !baggage_header.empty?
Baggage.from_incoming_header(baggage_header)
Expand All @@ -48,6 +60,7 @@ def initialize(scope, env = nil)
end

@baggage.freeze!
@incoming_trace = true
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)
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 4332930

Please sign in to comment.