Class: Sentry::Baggage

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

Overview

A W3C Baggage Header implementation.

Constant Summary collapse

SENTRY_PREFIX =
'sentry-'
SENTRY_PREFIX_REGEX =
/^sentry-/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, mutable: true) ⇒ Baggage

Returns a new instance of Baggage.



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

def initialize(items, mutable: true)
  @items = items
  @mutable = mutable
end

Instance Attribute Details

#itemsHash (readonly)

Returns:

  • (Hash)


12
13
14
# File 'lib/sentry/baggage.rb', line 12

def items
  @items
end

#mutableBoolean (readonly)

Returns:

  • (Boolean)


15
16
17
# File 'lib/sentry/baggage.rb', line 15

def mutable
  @mutable
end

Class Method Details

.from_incoming_header(header) ⇒ Baggage?

Creates a Baggage object from an incoming W3C Baggage header string.

Sentry items are identified with the ‘sentry-’ prefix and stored in a hash. The presence of a Sentry item makes the baggage object immutable.

Parameters:

  • header (String)

    The incoming Baggage header string.

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sentry/baggage.rb', line 29

def self.from_incoming_header(header)
  items = {}
  mutable = true

  header.split(',').each do |item|
    item = item.strip
    key, val = item.split('=')

    next unless key && val
    next unless key =~ SENTRY_PREFIX_REGEX

    baggage_key = key.split('-')[1]
    next unless baggage_key

    items[CGI.unescape(baggage_key)] = CGI.unescape(val)
    mutable = false
  end

  new(items, mutable: mutable)
end

Instance Method Details

#dynamic_sampling_contextHash

A Dynamic Sampling Context hash to be used in the trace envelope header.

Returns:

  • (Hash)


59
60
61
# File 'lib/sentry/baggage.rb', line 59

def dynamic_sampling_context
  @items
end

#freeze!void

This method returns an undefined value.

Make the Baggage immutable.



52
53
54
# File 'lib/sentry/baggage.rb', line 52

def freeze!
  @mutable = false
end

#serializeString

Serialize the Baggage object back to a string.

Returns:

  • (String)


65
66
67
68
# File 'lib/sentry/baggage.rb', line 65

def serialize
  items = @items.map { |k, v| "#{SENTRY_PREFIX}#{CGI.escape(k)}=#{CGI.escape(v)}" }
  items.join(',')
end