Skip to content

Commit 78e9740

Browse files
AugustyniakRafal Augustyniak
andauthored
Replace existing traceparent header values (#69)
Co-authored-by: Rafal Augustyniak <rafal.augustyniak@airbnb.com>
1 parent 44e0e57 commit 78e9740

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

Sources/NautilusTelemetry/Tracing/Span+URLSession.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension Span {
2525
return "\(method) \(target)"
2626
}
2727

28-
/// Add `traceparent` header to a URLRequest if we're sampling
28+
/// Sets the `traceparent` header on a URLRequest if we're sampling.
2929
/// - Parameter isSampling: whether we are sampling, defaults to InstrumentationSystem.tracer.isSampling
3030
/// - Parameter urlRequest: urlRequest to modify
3131
public func addTraceHeadersIfSampling(
@@ -34,19 +34,19 @@ extension Span {
3434
) {
3535
if isSampling {
3636
let value = traceParentHeaderValue(sampled: true)
37-
urlRequest.addValue(value.1, forHTTPHeaderField: value.0)
37+
urlRequest.setValue(value.1, forHTTPHeaderField: value.0)
3838
}
3939
}
4040

41-
/// Add `traceparent` header to a URLRequest regardless of sampling state
41+
/// Sets the `traceparent` header on a URLRequest regardless of sampling state.
4242
/// Sampled flag determined from InstrumentationSystem.tracer.isSampling
4343
/// - Parameter urlRequest: urlRequest to modify
4444
public func addTraceHeadersUnconditionally(
4545
_ urlRequest: inout URLRequest,
4646
isSampling: Bool = InstrumentationSystem.tracer.isSampling
4747
) {
4848
let value = traceParentHeaderValue(sampled: isSampling)
49-
urlRequest.addValue(value.1, forHTTPHeaderField: value.0)
49+
urlRequest.setValue(value.1, forHTTPHeaderField: value.0)
5050
}
5151

5252
/// Annotates the span with attributes from URLSessionTaskMetrics.

Tests/NautilusTelemetryTests/Tracing/SpanTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,28 @@ struct SpanTests {
140140
}
141141
}
142142

143+
@Test
144+
func traceparentHeaderReplacesExistingValue() throws {
145+
let url = try TestUtils.makeURL("https://api.example.com/")
146+
let span = tracer.startSpan(name: "test")
147+
148+
var sampledRequest = URLRequest(url: url)
149+
sampledRequest.setValue("existing", forHTTPHeaderField: "traceparent")
150+
span.addTraceHeadersIfSampling(&sampledRequest, isSampling: true)
151+
#expect(
152+
sampledRequest.value(forHTTPHeaderField: "traceparent")
153+
== span.traceParentHeaderValue(sampled: true).1
154+
)
155+
156+
var unsampledRequest = URLRequest(url: url)
157+
unsampledRequest.setValue("existing", forHTTPHeaderField: "traceparent")
158+
span.addTraceHeadersUnconditionally(&unsampledRequest, isSampling: false)
159+
#expect(
160+
unsampledRequest.value(forHTTPHeaderField: "traceparent")
161+
== span.traceParentHeaderValue(sampled: false).1
162+
)
163+
}
164+
143165
@Test
144166
func throwingSpan() throws {
145167
#expect(throws: TestError.self) {

0 commit comments

Comments
 (0)