Method: Sentry::PropagationContext#initialize

Defined in:
lib/sentry/propagation_context.rb

#initialize(scope, env = nil) ⇒ PropagationContext



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sentry/propagation_context.rb', line 83

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

  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

        @sample_rand = self.class.extract_sample_rand_from_baggage(@baggage, @trace_id)

        @baggage.freeze!
        @incoming_trace = true
      end
    end
  end

  @trace_id ||= Utils.uuid
  @span_id = Utils.uuid.slice(0, 16)
  @sample_rand ||= self.class.generate_sample_rand(@baggage, @trace_id, @parent_sampled)
end