Skip to content

Commit

Permalink
Move extract_sentry_trace to PropagationContext
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 10, 2023
1 parent 70ed112 commit 6150e0e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
26 changes: 24 additions & 2 deletions sentry-ruby/lib/sentry/propagation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

module Sentry
class PropagationContext
SENTRY_TRACE_REGEXP = Regexp.new(
"^[ \t]*" + # whitespace
"([0-9a-f]{32})?" + # trace_id
"-?([0-9a-f]{16})?" + # span_id
"-?([01])?" + # sampled
"[ \t]*$" # whitespace
)

# An uuid that can be used to identify a trace.
# @return [String]
Expand All @@ -24,6 +31,21 @@ def initialize(scope)
@baggage = nil
end

# Extract the trace_id, parent_span_id and parent_sampled values from a sentry-trace header.
#
# @param sentry_trace [String] the sentry-trace header value from the previous transaction.
# @return [Array, nil]
def self.extract_sentry_trace(sentry_trace)
match = SENTRY_TRACE_REGEXP.match(sentry_trace)
return nil if match.nil?

trace_id, parent_span_id, sampled_flag = match[1..3]
parent_sampled = sampled_flag.nil? ? nil : sampled_flag != "0"

[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 @@ -40,8 +62,8 @@ def get_traceparent
"#{trace_id}-#{span_id}"
end

# Returns the W3C baggage header from the propagation context.
# @return [String, nil]
# Returns the Baggage from the propagation context.
# @return [Baggage, nil]
def get_baggage
populate_head_baggage if @baggage.nil? || @baggage.mutable
@baggage
Expand Down
23 changes: 6 additions & 17 deletions sentry-ruby/lib/sentry/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

require "sentry/baggage"
require "sentry/profiler"
require "sentry/propagation_context"

module Sentry
class Transaction < Span
SENTRY_TRACE_REGEXP = Regexp.new(
"^[ \t]*" + # whitespace
"([0-9a-f]{32})?" + # trace_id
"-?([0-9a-f]{16})?" + # span_id
"-?([01])?" + # sampled
"[ \t]*$" # whitespace
)
# @deprecated Use Sentry::PropagationContext::SENTRY_TRACE_REGEXP instead.
SENTRY_TRACE_REGEXP = PropagationContext::SENTRY_TRACE_REGEXP

UNLABELD_NAME = "<unlabeled transaction>".freeze
MESSAGE_PREFIX = "[Tracing]"

Expand Down Expand Up @@ -132,18 +129,10 @@ def self.from_sentry_trace(sentry_trace, baggage: nil, hub: Sentry.get_current_h
)
end

# Extract the trace_id, parent_span_id and parent_sampled values from a sentry-trace header.
#
# @param sentry_trace [String] the sentry-trace header value from the previous transaction.
# @deprecated Use Sentry::PropagationContext.extract_sentry_trace instead.
# @return [Array, nil]
def self.extract_sentry_trace(sentry_trace)
match = SENTRY_TRACE_REGEXP.match(sentry_trace)
return nil if match.nil?

trace_id, parent_span_id, sampled_flag = match[1..3]
parent_sampled = sampled_flag.nil? ? nil : sampled_flag != "0"

[trace_id, parent_span_id, parent_sampled]
PropagationContext.extract_sentry_trace(sentry_trace)
end

# @return [Hash]
Expand Down
4 changes: 2 additions & 2 deletions sentry-ruby/spec/sentry/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
sentry_trace = subject.to_sentry_trace

expect(sentry_trace).to eq("#{subject.trace_id}-#{subject.span_id}-1")
expect(sentry_trace).to match(Sentry::Transaction::SENTRY_TRACE_REGEXP)
expect(sentry_trace).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP)
end

context "without sampled value" do
Expand All @@ -87,7 +87,7 @@
sentry_trace = subject.to_sentry_trace

expect(sentry_trace).to eq("#{subject.trace_id}-#{subject.span_id}-")
expect(sentry_trace).to match(Sentry::Transaction::SENTRY_TRACE_REGEXP)
expect(sentry_trace).to match(Sentry::PropagationContext::SENTRY_TRACE_REGEXP)
end
end
end
Expand Down

0 comments on commit 6150e0e

Please sign in to comment.