-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement traceparent/baggage/DSC for PropagationContext and apply in…
… scope
- Loading branch information
1 parent
4a8f404
commit 71b288c
Showing
2 changed files
with
61 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,78 @@ | ||
# frozen_string_literal: true | ||
|
||
require "securerandom" | ||
require "sentry/baggage" | ||
|
||
module Sentry | ||
class PropagationContext | ||
def initialize | ||
|
||
# An uuid that can be used to identify a trace. | ||
# @return [String] | ||
attr_reader :trace_id | ||
# An uuid that can be used to identify the span. | ||
# @return [String] | ||
attr_reader :span_id | ||
# Span parent's span_id. | ||
# @return [String] | ||
attr_reader :parent_span_id | ||
|
||
def initialize(scope) | ||
@scope = scope | ||
@trace_id = SecureRandom.uuid.delete("-") | ||
@span_id = SecureRandom.uuid.delete("-").slice(0, 16) | ||
@parent_span_id = nil | ||
@dynamic_sampling_context = nil | ||
@baggage = nil | ||
end | ||
|
||
# Returns the trace context that can be used to embed in an Event. | ||
# @return [Hash] | ||
def get_trace_context | ||
{ | ||
trace_id: @trace_id, | ||
span_id: @span_id, | ||
parent_span_id: @parent_span_id | ||
trace_id: trace_id, | ||
span_id: span_id, | ||
parent_span_id: parent_span_id | ||
} | ||
end | ||
|
||
# Returns the sentry-trace header from the propagation context. | ||
# @return [String] | ||
def get_traceparent | ||
"#{trace_id}-#{span_id}" | ||
end | ||
|
||
# Returns the W3C baggage header from the propagation context. | ||
# @return [String, nil] | ||
def get_baggage | ||
populate_head_baggage if @baggage.nil? || @baggage.mutable | ||
@baggage | ||
end | ||
|
||
# Returns the Dynamic Sampling Context from the baggage. | ||
# @return [String, nil] | ||
def get_dynamic_sampling_context | ||
get_baggage&.dynamic_sampling_context | ||
end | ||
|
||
private | ||
|
||
def populate_head_baggage | ||
return unless Sentry.initialized? | ||
|
||
configuration = Sentry.configuration | ||
|
||
items = { | ||
"trace_id" => trace_id, | ||
"sample_rate" => configuration.traces_sample_rate, | ||
"environment" => configuration.environment, | ||
"release" => configuration.release, | ||
"public_key" => configuration.dsn&.public_key | ||
} | ||
|
||
user = @scope&.user | ||
items["user_segment"] = user["segment"] if user && user["segment"] | ||
|
||
items.compact! | ||
@baggage = Baggage.new(items, mutable: false) | ||
end | ||
end | ||
end |
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