Class: Sentry::PropagationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/propagation_context.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, env = nil) ⇒ PropagationContext

Returns a new instance of PropagationContext.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sentry/propagation_context.rb', line 36

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"] || env[SENTRY_TRACE_HEADER_NAME]
    baggage_header = env["HTTP_BAGGAGE"] || env[BAGGAGE_HEADER_NAME]

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

      if 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)
          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!
        @incoming_trace = true
      end
    end
  end

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

Instance Attribute Details

#baggageBaggage? (readonly)

This is only for accessing the current baggage variable. Please use the #get_baggage method for interfacing outside this class.

Returns:



34
35
36
# File 'lib/sentry/propagation_context.rb', line 34

def baggage
  @baggage
end

#incoming_traceBoolean (readonly)

Is there an incoming trace or not?

Returns:

  • (Boolean)


30
31
32
# File 'lib/sentry/propagation_context.rb', line 30

def incoming_trace
  @incoming_trace
end

#parent_sampledBoolean? (readonly)

The sampling decision of the parent transaction.

Returns:

  • (Boolean, nil)


27
28
29
# File 'lib/sentry/propagation_context.rb', line 27

def parent_sampled
  @parent_sampled
end

#parent_span_idString? (readonly)

Span parent’s span_id.

Returns:

  • (String, nil)


24
25
26
# File 'lib/sentry/propagation_context.rb', line 24

def parent_span_id
  @parent_span_id
end

#span_idString (readonly)

An uuid that can be used to identify the span.

Returns:

  • (String)


21
22
23
# File 'lib/sentry/propagation_context.rb', line 21

def span_id
  @span_id
end

#trace_idString (readonly)

An uuid that can be used to identify a trace.

Returns:

  • (String)


18
19
20
# File 'lib/sentry/propagation_context.rb', line 18

def trace_id
  @trace_id
end

Class Method Details

.extract_sentry_trace(sentry_trace) ⇒ Array?

Extract the trace_id, parent_span_id and parent_sampled values from a sentry-trace header.

Parameters:

  • sentry_trace (String)

    the sentry-trace header value from the previous transaction.

Returns:

  • (Array, nil)


77
78
79
80
81
82
83
84
85
# File 'lib/sentry/propagation_context.rb', line 77

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

Instance Method Details

#get_baggageBaggage?

Returns the Baggage from the propagation context or populates as head SDK if empty.

Returns:



105
106
107
108
# File 'lib/sentry/propagation_context.rb', line 105

def get_baggage
  populate_head_baggage if @baggage.nil? || @baggage.mutable
  @baggage
end

#get_dynamic_sampling_contextString?

Returns the Dynamic Sampling Context from the baggage.

Returns:

  • (String, nil)


112
113
114
# File 'lib/sentry/propagation_context.rb', line 112

def get_dynamic_sampling_context
  get_baggage&.dynamic_sampling_context
end

#get_trace_contextHash

Returns the trace context that can be used to embed in an Event.

Returns:

  • (Hash)


89
90
91
92
93
94
95
# File 'lib/sentry/propagation_context.rb', line 89

def get_trace_context
  {
    trace_id: trace_id,
    span_id: span_id,
    parent_span_id: parent_span_id
  }
end

#get_traceparentString

Returns the sentry-trace header from the propagation context.

Returns:

  • (String)


99
100
101
# File 'lib/sentry/propagation_context.rb', line 99

def get_traceparent
  "#{trace_id}-#{span_id}"
end