Skip to content

Commit

Permalink
Implement traceparent/baggage/DSC for PropagationContext and apply in…
Browse files Browse the repository at this point in the history
… scope
  • Loading branch information
sl0thentr0py committed Aug 28, 2023
1 parent 4a8f404 commit 71b288c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
64 changes: 59 additions & 5 deletions sentry-ruby/lib/sentry/propagation_context.rb
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
3 changes: 2 additions & 1 deletion sentry-ruby/lib/sentry/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def apply_to_event(event, hint = nil)
event.contexts[:trace] ||= span.get_trace_context
else
event.contexts[:trace] ||= propagation_context.get_trace_context
event.dynamic_sampling_context ||= propagation_context.get_dynamic_sampling_context
end

event.fingerprint = fingerprint
Expand Down Expand Up @@ -307,7 +308,7 @@ def set_new_breadcrumb_buffer
end

def generate_propagation_context
@propagation_context = PropagationContext.new
@propagation_context = PropagationContext.new(self)
end

class << self
Expand Down

0 comments on commit 71b288c

Please sign in to comment.