Skip to content

Commit

Permalink
Add new unified get_trace_propagation_headers and use in net/http
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Aug 28, 2023
1 parent abc0eaa commit 143cf28
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 29 deletions.
9 changes: 9 additions & 0 deletions sentry-ruby/lib/sentry-ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,15 @@ def get_baggage
get_current_hub.get_baggage
end

# Returns the a Hash containing sentry-trace and baggage.
# Can be either from the currently active span or the propagation context.
#
# @return [Hash, nil]
def get_trace_propagation_headers
return nil unless initialized?
get_current_hub.get_trace_propagation_headers
end

##### Helpers #####

# @!visibility private
Expand Down
6 changes: 5 additions & 1 deletion sentry-ruby/lib/sentry/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ def send_event(event, hint = nil)
raise
end

# @deprecated use Sentry.get_traceparent instead.
#
# Generates a Sentry trace for distribted tracing from the given Span.
# Returns `nil` if `config.propagate_traces` is `false`.
# @param span [Span] the span to generate trace from.
Expand All @@ -160,7 +162,9 @@ def generate_sentry_trace(span)
trace
end

# Generates a W3C Baggage header for distribted tracing from the given Span.
# @deprecated Use Sentry.get_baggage instead.
#
# Generates a W3C Baggage header for distributed tracing from the given Span.
# Returns `nil` if `config.propagate_traces` is `false`.
# @param span [Span] the span to generate trace from.
# @return [String, nil]
Expand Down
9 changes: 9 additions & 0 deletions sentry-ruby/lib/sentry/hub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,15 @@ def get_baggage
current_scope.propagation_context&.get_baggage&.serialize
end

def get_trace_propagation_headers
return nil unless configuration.propagate_traces

{
SENTRY_TRACE_HEADER_NAME => get_traceparent,
BAGGAGE_HEADER_NAME => get_baggage
}.compact
end

private

def current_layer
Expand Down
12 changes: 3 additions & 9 deletions sentry-ruby/lib/sentry/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def request(req, body = nil, &block)

Sentry.with_child_span(op: OP_NAME, start_timestamp: Sentry.utc_now.to_f) do |sentry_span|
request_info = extract_request_info(req)
set_sentry_trace_header(req, sentry_span, request_info)
set_propagation_headers(req, request_info)

super.tap do |res|
record_sentry_breadcrumb(request_info, res)
Expand All @@ -49,17 +49,11 @@ def request(req, body = nil, &block)

private

def set_sentry_trace_header(req, sentry_span, request_info)
return unless sentry_span

def set_propagation_headers(req, request_info)
client = Sentry.get_current_client
return unless propagate_trace?(request_info[:url], client.configuration.trace_propagation_targets)

trace = client.generate_sentry_trace(sentry_span)
req[SENTRY_TRACE_HEADER_NAME] = trace if trace

baggage = client.generate_baggage(sentry_span)
req[BAGGAGE_HEADER_NAME] = baggage if baggage && !baggage.empty?
Sentry.get_trace_propagation_headers&.each { |k, v| req[k] = v }
end

def record_sentry_breadcrumb(request_info, res)
Expand Down
2 changes: 1 addition & 1 deletion sentry-ruby/lib/sentry/propagation_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def populate_head_baggage

items = {
"trace_id" => trace_id,
"sample_rate" => configuration.traces_sample_rate,
"sample_rate" => configuration.traces_sample_rate&.to_s,
"environment" => configuration.environment,
"release" => configuration.release,
"public_key" => configuration.dsn&.public_key
Expand Down
19 changes: 1 addition & 18 deletions sentry-ruby/spec/sentry/net/http_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@
response = http.request(request)

expect(response.code).to eq("200")
expect(string_io.string).to match(
/\[Tracing\] Adding sentry-trace header to outgoing request:/
)
end

it "adds baggage header to the request header as head SDK when no incoming trace" do
Expand All @@ -115,10 +112,6 @@
response = http.request(request)

expect(response.code).to eq("200")
expect(string_io.string).to match(
/\[Tracing\] Adding baggage header to outgoing request:/
)

request_span = transaction.span_recorder.spans.last
expect(request["baggage"]).to eq(request_span.to_baggage)
expect(request["baggage"]).to eq(
Expand Down Expand Up @@ -151,10 +144,6 @@
response = http.request(request)

expect(response.code).to eq("200")
expect(string_io.string).to match(
/\[Tracing\] Adding baggage header to outgoing request:/
)

request_span = transaction.span_recorder.spans.last
expect(request["baggage"]).to eq(request_span.to_baggage)
expect(request["baggage"]).to eq(
Expand All @@ -165,7 +154,7 @@
)
end

context "with config.propagate_trace = false" do
context "with config.propagate_traces = false" do
before do
Sentry.configuration.propagate_traces = false
end
Expand All @@ -183,9 +172,6 @@
response = http.request(request)

expect(response.code).to eq("200")
expect(string_io.string).not_to match(
/Adding sentry-trace header to outgoing request:/
)
expect(request.key?("sentry-trace")).to eq(false)
end

Expand All @@ -210,9 +196,6 @@
response = http.request(request)

expect(response.code).to eq("200")
expect(string_io.string).not_to match(
/Adding baggage header to outgoing request:/
)
expect(request.key?("baggage")).to eq(false)
end
end
Expand Down

0 comments on commit 143cf28

Please sign in to comment.