Skip to content

Commit 71b6d7f

Browse files
TommyCppMrAlias
andauthored
[propagator] Set sample bitmask when sampling decision is debug for B3 Propagator. (#369)
According to the spec. Debug sampling decision implies accept(sampled) decision. But we didn't set sample bitmask. So when trace state is debug, `IsSampled` method will still return false, which is different from spec. Now we make sure when debug bitmask is set, sample bitmask will also be set. Co-authored-by: Tyler Yahn <[email protected]>
1 parent 9dc5e0c commit 71b6d7f

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

propagators/b3/b3_data_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ var extractHeaders = []extractTest{
144144
wantSc: trace.SpanContext{
145145
TraceID: traceID,
146146
SpanID: spanID,
147-
TraceFlags: trace.FlagsDeferred | trace.FlagsDebug,
147+
TraceFlags: trace.FlagsSampled | trace.FlagsDebug,
148148
},
149149
},
150150
{
@@ -163,8 +163,7 @@ var extractHeaders = []extractTest{
163163
},
164164
{
165165
// spec explicitly states "Debug implies an accept decision, so don't
166-
// also send the X-B3-Sampled header", make sure sampling is
167-
// deferred.
166+
// also send the X-B3-Sampled header", make sure sampling is set in this case.
168167
name: "multiple: debug flag set and sampling state is deny",
169168
headers: map[string]string{
170169
b3TraceID: traceIDStr,
@@ -175,7 +174,7 @@ var extractHeaders = []extractTest{
175174
wantSc: trace.SpanContext{
176175
TraceID: traceID,
177176
SpanID: spanID,
178-
TraceFlags: trace.FlagsDebug,
177+
TraceFlags: trace.FlagsDebug | trace.FlagsSampled,
179178
},
180179
},
181180
{
@@ -251,7 +250,7 @@ var extractHeaders = []extractTest{
251250
wantSc: trace.SpanContext{
252251
TraceID: traceID,
253252
SpanID: spanID,
254-
TraceFlags: trace.FlagsDebug,
253+
TraceFlags: trace.FlagsDebug | trace.FlagsSampled,
255254
},
256255
},
257256
{

propagators/b3/b3_propagator.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,14 @@ func extractMultiple(traceID, spanID, parentSpanID, sampled, flags string) (trac
208208
return empty, errInvalidSampledHeader
209209
}
210210

211-
// The only accepted value for Flags is "1". This will set Debug to
212-
// true. All other values and omission of header will be ignored.
211+
// The only accepted value for Flags is "1". This will set Debug bitmask and
212+
// sampled bitmask to 1 since debug implicitly means sampled. All other
213+
// values and omission of header will be ignored. According to the spec. User
214+
// shouldn't send X-B3-Sampled header along with X-B3-Flags header. Thus we will
215+
// ignore X-B3-Sampled header when X-B3-Flags header is sent and valid.
213216
if flags == "1" {
214-
sc.TraceFlags |= trace.FlagsDebug
217+
sc.TraceFlags |= trace.FlagsDebug | trace.FlagsSampled
218+
sc.TraceFlags &= ^trace.FlagsDeferred
215219
}
216220

217221
if traceID != "" {
@@ -331,7 +335,7 @@ func extractSingle(contextHeader string) (trace.SpanContext, error) {
331335
case "":
332336
sc.TraceFlags = trace.FlagsDeferred
333337
case "d":
334-
sc.TraceFlags = trace.FlagsDebug
338+
sc.TraceFlags = trace.FlagsDebug | trace.FlagsSampled
335339
case "1":
336340
sc.TraceFlags = trace.FlagsSampled
337341
case "0":

propagators/b3/b3_propagator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ func TestExtractMultiple(t *testing.T) {
5656
},
5757
{
5858
"", "", "", "", "1",
59-
trace.SpanContext{TraceFlags: trace.FlagsDeferred | trace.FlagsDebug},
59+
trace.SpanContext{TraceFlags: trace.FlagsSampled | trace.FlagsDebug},
6060
nil,
6161
},
6262
{
6363
"", "", "", "0", "1",
64-
trace.SpanContext{TraceFlags: trace.FlagsDebug},
64+
trace.SpanContext{TraceFlags: trace.FlagsDebug | trace.FlagsSampled},
6565
nil,
6666
},
6767
{
@@ -214,7 +214,7 @@ func TestExtractSingle(t *testing.T) {
214214
}{
215215
{"0", trace.SpanContext{}, nil},
216216
{"1", trace.SpanContext{TraceFlags: trace.FlagsSampled}, nil},
217-
{"d", trace.SpanContext{TraceFlags: trace.FlagsDebug}, nil},
217+
{"d", trace.SpanContext{TraceFlags: trace.FlagsDebug | trace.FlagsSampled}, nil},
218218
{"a", empty, errInvalidSampledByte},
219219
{"3", empty, errInvalidSampledByte},
220220
{"000000000000007b", empty, errInvalidScope},

0 commit comments

Comments
 (0)