Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add span flags to OTLP exported data #5194

Merged
merged 8 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- Add `Recorder` in `go.opentelemetry.io/otel/log/logtest` to facilitate testing the log bridge implementations. (#5134)
- Add span flags to OTLP spans and links exported by `go.opentelemetry.io/otel/exporters/otlp/otlptrace`. (#5194)

### Changed

Expand Down
13 changes: 13 additions & 0 deletions exporters/otlp/otlptrace/internal/tracetransform/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func span(sd tracesdk.ReadOnlySpan) *tracepb.Span {
if psid := sd.Parent().SpanID(); psid.IsValid() {
s.ParentSpanId = psid[:]
}
s.Flags = buildSpanFlags(sd.Parent())

return s
}
Expand Down Expand Up @@ -146,16 +147,28 @@ func links(links []tracesdk.Link) []*tracepb.Span_Link {
tid := otLink.SpanContext.TraceID()
sid := otLink.SpanContext.SpanID()

flags := buildSpanFlags(otLink.SpanContext)

sl = append(sl, &tracepb.Span_Link{
TraceId: tid[:],
SpanId: sid[:],
Attributes: KeyValues(otLink.Attributes),
DroppedAttributesCount: uint32(otLink.DroppedAttributeCount),
Flags: flags,
})
}
return sl
}

func buildSpanFlags(sc trace.SpanContext) uint32 {
flags := tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_HAS_IS_REMOTE_MASK
if sc.IsRemote() {
flags = flags | tracepb.SpanFlags_SPAN_FLAGS_CONTEXT_IS_REMOTE_MASK
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
}

return uint32(flags)
}

// spanEvents transforms span Events to an OTLP span events.
func spanEvents(es []tracesdk.Event) []*tracepb.Span_Event {
if len(es) == 0 {
Expand Down
26 changes: 26 additions & 0 deletions exporters/otlp/otlptrace/internal/tracetransform/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func TestLinks(t *testing.T) {
TraceId: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
SpanId: []uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0},
DroppedAttributesCount: 3,
Flags: 0x100,
}
assert.Equal(t, expected, got[0])

Expand Down Expand Up @@ -166,6 +167,30 @@ func TestStatus(t *testing.T) {
}
}

func TestBuildSpanFlags(t *testing.T) {
for _, tt := range []struct {
name string
spanContext trace.SpanContext
wantFlags uint32
}{
{
name: "with an empty span context",
wantFlags: 0x100,
},
{
name: "with a remote span context",
spanContext: trace.NewSpanContext(trace.SpanContextConfig{
Remote: true,
}),
wantFlags: 0x300,
},
} {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.wantFlags, buildSpanFlags(tt.spanContext))
})
}
}

func TestNilSpan(t *testing.T) {
assert.Nil(t, span(nil))
}
Expand Down Expand Up @@ -270,6 +295,7 @@ func TestSpanData(t *testing.T) {
SpanId: []byte{0xFF, 0xFE, 0xFD, 0xFC, 0xFB, 0xFA, 0xF9, 0xF8},
ParentSpanId: []byte{0xEF, 0xEE, 0xED, 0xEC, 0xEB, 0xEA, 0xE9, 0xE8},
TraceState: "key1=val1,key2=val2",
Flags: 0x300,
Name: spanData.Name,
Kind: tracepb.Span_SPAN_KIND_SERVER,
StartTimeUnixNano: uint64(startTime.UnixNano()),
Expand Down