Skip to content

Conversation

@maciejburda
Copy link
Member

What and why?

Unifies the behaviour of HTTP Header Writers to always inject headers with sampling decision information if TracingContextInjection is configured to all.

Review checklist

  • Feature or bugfix MUST have appropriate tests (unit, integration)
  • Make sure each commit and the PR mention the Issue number or JIRA reference
  • Add CHANGELOG entry for user facing changes
  • Add Objective-C interface for public APIs (see our guidelines (internal) and run make api-surface)

@maciejburda maciejburda requested review from a team as code owners September 16, 2025 12:39
public var sampled: Bool? {
if let single = httpHeaderFields[B3HTTPHeaders.Single.b3Field] {
return single != B3HTTPHeaders.Constants.unsampledValue
return single.components(separatedBy: B3HTTPHeaders.Constants.b3Separator)[safe: 2] != B3HTTPHeaders.Constants.unsampledValue
Copy link
Member Author

@maciejburda maciejburda Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should read when B3 when it is just "0" as well.

I think that was the main source of confusion, because in public documentation:
https://github.com/openzipkin/b3-propagation
Both b3=0 and b3={TraceId}-{SpanId}-{SamplingState}-{ParentSpanId} are valid.

I think that in our case we always want to use the latter, or not inject header at all.
But maybe if sampled = false and TraceContextInjection = sampled we should inject b3=0?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend SDK engineer here 👋🏼

I think we should be flexible on what we extract and accept both b3=0 and b3={TraceId}-{SpanId}-{SamplingState}-{ParentSpanId} styles, but only inject in the b3={TraceId}-{SpanId}-{SamplingState}-{ParentSpanId} style

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, if the input is b3=0, then we don't have a trace-id or span-id at all. For reference in our C# library, we don't accept a value of b3=0 so if a new span is created then we get a brand new tracecontext. I wonder what the other libraries do...let me get back to you, but I think the injection behavior you have encoded is accurate so let's not block on this

Comment on lines -98 to -110
case .single:
if sampled {
traceHeaderFields[B3HTTPHeaders.Single.b3Field] = [
String(traceContext.traceID, representation: .hexadecimal32Chars),
String(traceContext.spanID, representation: .hexadecimal16Chars),
sampled ? Constants.sampledValue : Constants.unsampledValue,
traceContext.parentSpanID.map { String($0, representation: .hexadecimal16Chars) }
]
.compactMap { $0 }
.joined(separator: Constants.b3Separator)
} else {
traceHeaderFields[B3HTTPHeaders.Single.b3Field] = Constants.unsampledValue
}
Copy link
Member Author

@maciejburda maciejburda Sep 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the issue. Take a look at the dead code in line 103 - dead code because of if statement before.

Comment on lines -52 to -53
switch (traceContextInjection, traceContext.isKept) {
case (.all, _), (.sampled, true):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually correct - but decided to rework the code to match b3 implementation.

I think it's slightly better to read now.

}

switch (traceContextInjection, sampled) {
case (.all, _), (.sampled, true):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Behaviour was correct according to tests.

I hope it's a bit less cryptic now.


let headers = writer.traceHeaderFields
XCTAssertEqual(headers[B3HTTPHeaders.Single.b3Field], "0")
XCTAssertEqual(headers[B3HTTPHeaders.Single.b3Field], "00000000000004d200000000000004d2-0000000000000929-0-000000000000162e")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the key difference in the behaviour in this PR.

@maciejburda maciejburda requested a review from a team as a code owner September 16, 2025 12:50
@datadog-datadog-prod-us1

This comment has been minimized.

urseberry
urseberry previously approved these changes Sep 16, 2025
Copy link

@zacharycmontoya zacharycmontoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I can follow up on the case where b3=0 is extracted

simaoseica-dd
simaoseica-dd previously approved these changes Sep 18, 2025
Copy link
Contributor

@simaoseica-dd simaoseica-dd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for spotting it. Well done. 👌

@maciejburda maciejburda force-pushed the maciey/fix-tracing-header-writers branch from 2820e15 to 81e6bf7 Compare September 19, 2025 09:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants