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 16, 2023
1 parent 70ed112 commit 05c5f27
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 27 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

Check warning on line 507 in sentry-ruby/lib/sentry-ruby.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L506-L507

Added lines #L506 - L507 were not covered by tests
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

Check warning on line 516 in sentry-ruby/lib/sentry-ruby.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry-ruby.rb#L515-L516

Added lines #L515 - L516 were not covered by tests
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

Check warning on line 247 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L247

Added line #L247 was not covered by tests

{
SENTRY_TRACE_HEADER_NAME => get_traceparent,

Check warning on line 250 in sentry-ruby/lib/sentry/hub.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/hub.rb#L250

Added line #L250 was not covered by tests
BAGGAGE_HEADER_NAME => get_baggage
}.compact
end

private

def current_layer
Expand Down
8 changes: 1 addition & 7 deletions sentry-ruby/lib/sentry/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,10 @@ def request(req, body = nil, &block)
private

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

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 }

Check warning on line 56 in sentry-ruby/lib/sentry/net/http.rb

View check run for this annotation

Codecov / codecov/patch

sentry-ruby/lib/sentry/net/http.rb#L56

Added line #L56 was not covered by tests
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 @@ -150,10 +143,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 @@ -164,7 +153,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 @@ -182,9 +171,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 @@ -209,9 +195,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 05c5f27

Please sign in to comment.