Skip to content

Commit ed5ef5d

Browse files
authored
fix(span): skip zero parent span id from empty traceparent (#23) (#25)
When --tp-ignore-env is set, LoadTraceparent returns a traceparent with Initialized=true but zeroed SpanId. The code unconditionally copied this into ParentSpanId, producing a span with an all-zeros parent that looks like it has a parent when it doesn't. Now checks bytes.Equal against GetEmptySpanId() before setting parent. Fixes #23 🤖 Claude <claude@anthropic.com>
1 parent b7dbb42 commit ed5ef5d

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

data_for_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,32 @@ var suites = []FixtureSuite{
12431243
},
12441244
},
12451245
},
1246+
// #23: --tp-ignore-env should not produce a zero-valued parent span id
1247+
{
1248+
{
1249+
Name: "#23 --tp-ignore-env should not send zero parent span id",
1250+
Config: FixtureConfig{
1251+
CliArgs: []string{
1252+
"span",
1253+
"--endpoint", "{{endpoint}}",
1254+
"--tp-ignore-env",
1255+
"--name", "test-tp-ignore",
1256+
},
1257+
Env: map[string]string{
1258+
"TRACEPARENT": "00-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-bbbbbbbbbbbbbbbb-01",
1259+
},
1260+
},
1261+
Expect: Results{
1262+
Config: otelcli.DefaultConfig(),
1263+
SpanData: map[string]string{
1264+
"trace_id": "*",
1265+
"span_id": "*",
1266+
"parent_span_id": "",
1267+
},
1268+
SpanCount: 1,
1269+
},
1270+
},
1271+
},
12461272
// full-system test --otlp-headers makes it to grpc/http servers
12471273
{
12481274
{

otelcli/config_span.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package otelcli
22

33
import (
4+
"bytes"
45
"encoding/hex"
56
"fmt"
67
"io"
@@ -42,7 +43,10 @@ func (c Config) NewProtobufSpan() *tracepb.Span {
4243
tp := c.LoadTraceparent()
4344
if tp.Initialized {
4445
span.TraceId = tp.TraceId
45-
span.ParentSpanId = tp.SpanId
46+
// only set parent span id when the traceparent has a real (non-zero) span id (#23)
47+
if !bytes.Equal(tp.SpanId, otlpclient.GetEmptySpanId()) {
48+
span.ParentSpanId = tp.SpanId
49+
}
4650
}
4751
} else {
4852
span.TraceId = otlpclient.GetEmptyTraceId()

0 commit comments

Comments
 (0)