1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-12 10:04:29 +02:00

Update OTLP SpanData transform (#614)

* Update OTLP SpanData transform

The ParentSpanId needs to be empty for root spans according to the OTLP
[docs](6c2a86ed2f/gen/go/trace/v1/trace.pb.go (L284-L286)).
This updates the SpanData transform function to not add the ParentSpanID
if it is not a valid span ID (which includes if it is the nil span ID
used for an unset ID).

Additionally, this adds a test to prevent regression.

* Simplify test to just check parent span ID transform

Co-authored-by: Rahul Patel <rahulpa@google.com>
This commit is contained in:
Tyler Yahn 2020-04-07 15:14:23 -07:00 committed by GitHub
parent 8e97011ea8
commit 856aad92ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -65,10 +65,10 @@ func span(sd *export.SpanData) *tracepb.Span {
if sd == nil {
return nil
}
return &tracepb.Span{
s := &tracepb.Span{
TraceId: sd.SpanContext.TraceID[:],
SpanId: sd.SpanContext.SpanID[:],
ParentSpanId: sd.ParentSpanID[:],
Status: status(sd.StatusCode, sd.StatusMessage),
StartTimeUnixNano: uint64(sd.StartTime.UnixNano()),
EndTimeUnixNano: uint64(sd.EndTime.UnixNano()),
@ -82,6 +82,12 @@ func span(sd *export.SpanData) *tracepb.Span {
DroppedEventsCount: uint32(sd.DroppedMessageEventCount),
DroppedLinksCount: uint32(sd.DroppedLinkCount),
}
if sd.ParentSpanID.IsValid() {
s.ParentSpanId = sd.ParentSpanID[:]
}
return s
}
// status transform a span code and message into an OTLP span status.

View File

@ -360,3 +360,12 @@ func TestSpanData(t *testing.T) {
t.Fatalf("transformed span differs %v\n", diff)
}
}
// Empty parent span ID should be treated as root span.
func TestRootSpanData(t *testing.T) {
rs := SpanData([]*export.SpanData{{}})[0]
got := rs.GetInstrumentationLibrarySpans()[0].GetSpans()[0].GetParentSpanId()
// Empty means root span.
assert.Nil(t, got, "incorrect transform of root parent span ID")
}