-
-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2619677
commit 775f94c
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |