Skip to content

Commit

Permalink
Implement Baggage class
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 17, 2022
1 parent 2619677 commit 775f94c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require "sentry/hub"
require "sentry/background_worker"
require "sentry/session_flusher"
require "sentry/baggage"

[
"sentry/rake",
Expand Down
66 changes: 66 additions & 0 deletions sentry-ruby/lib/sentry/baggage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require 'cgi'

module Sentry
class Baggage
SENTRY_PREFIX = 'sentry-'.freeze
SENTRY_PREFIX_REGEX = /^sentry-/.freeze

# DynamicSamplingContext
DSC_KEYS = %w(
trace_id
public_key
sample_rate
release
environment
transaction
user_id
user_segment
).freeze

def initialize(sentry_items, third_party_items: '', mutable: true)
@sentry_items = sentry_items
@third_party_items = third_party_items
@mutable = mutable
end

def self.from_incoming_header(header)
return nil if header.nil? || header.empty?

sentry_items = {}
third_party_items = ''
mutable = true

header.split(',').map(&:strip).each do |item|
key, val = item.split('=')
next unless key && val

if key =~ SENTRY_PREFIX_REGEX
baggage_key = CGI.unescape(key.split('-')[1])
sentry_items[baggage_key] = CGI.unescape(val)
mutable = false
else
delim = third_party_items.empty? ? '' : ','
third_party_items += (delim + item)
end
end

new(sentry_items, third_party_items: third_party_items, mutable: mutable)
end

def freeze!
@mutable = false
end

def dynamic_sampling_context
@sentry_items.slice(*DSC_KEYS)
end

def serialize(include_third_party: false)
items = @sentry_items.map { |k, v| "#{SENTRY_PREFIX}#{CGI.escape(k)}=#{CGI.escape(v)}" }
items << @third_party_items if include_third_party
items.join(',')
end
end
end

0 comments on commit 775f94c

Please sign in to comment.