Skip to content

Commit ebb05d6

Browse files
fix: Do not overwrite baggage header contents if it already exists (#2896)
1 parent 84e50c5 commit ebb05d6

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Bug Fixes 🐛
1010

11+
- Do not overwrite baggage header contents if it already exists by @jakubsomonday in [#2894](https://github.com/getsentry/sentry-ruby/pull/2894)
1112
- (rails) Set mechanism.handled based on error handling status by @solnic in [#2892](https://github.com/getsentry/sentry-ruby/pull/2892)
1213
- Copy event processors on Scope#dup by @sl0thentr0py in [#2893](https://github.com/getsentry/sentry-ruby/pull/2893)
1314
- Map `trilogy` database adapter to `mysql` for Query Insights compatibility by @krismichalski in [#2656](https://github.com/getsentry/sentry-ruby/pull/2656)

sentry-ruby/lib/sentry/utils/http_tracing.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ def set_span_info(sentry_span, request_info, response_status)
1212
end
1313

1414
def set_propagation_headers(req)
15-
Sentry.get_trace_propagation_headers&.each { |k, v| req[k] = v }
15+
Sentry.get_trace_propagation_headers&.each do |k, v|
16+
if k == BAGGAGE_HEADER_NAME && req[k]
17+
req[k] = "#{v},#{req[k]}"
18+
else
19+
req[k] = v
20+
end
21+
end
1622
end
1723

1824
def record_sentry_breadcrumb(request_info, response_status)

sentry-ruby/spec/sentry/net/http_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,45 @@
192192
)
193193
end
194194

195+
it "merges baggage header with pre-existing baggage on the request" do
196+
stub_normal_response
197+
198+
uri = URI("http://example.com/path")
199+
http = Net::HTTP.new(uri.host, uri.port)
200+
request = Net::HTTP::Get.new(uri.request_uri)
201+
request["baggage"] = "routingKey=myvalue,tenantId=123"
202+
203+
transaction = Sentry.start_transaction
204+
Sentry.get_current_scope.set_span(transaction)
205+
206+
response = http.request(request)
207+
208+
expect(response.code).to eq("200")
209+
request_span = transaction.span_recorder.spans.last
210+
sentry_baggage = request_span.to_baggage
211+
212+
expect(request["baggage"]).to include(sentry_baggage)
213+
expect(request["baggage"]).to include("routingKey=myvalue,tenantId=123")
214+
expect(request["baggage"]).to eq("#{sentry_baggage},routingKey=myvalue,tenantId=123")
215+
end
216+
217+
it "sets baggage header normally when no pre-existing baggage on the request" do
218+
stub_normal_response
219+
220+
uri = URI("http://example.com/path")
221+
http = Net::HTTP.new(uri.host, uri.port)
222+
request = Net::HTTP::Get.new(uri.request_uri)
223+
224+
transaction = Sentry.start_transaction
225+
Sentry.get_current_scope.set_span(transaction)
226+
227+
response = http.request(request)
228+
229+
expect(response.code).to eq("200")
230+
request_span = transaction.span_recorder.spans.last
231+
expect(request["baggage"]).to eq(request_span.to_baggage)
232+
end
233+
195234
context "with config.propagate_traces = false" do
196235
before do
197236
Sentry.configuration.propagate_traces = false

0 commit comments

Comments
 (0)