You've already forked opentelemetry-go
							
							
				mirror of
				https://github.com/open-telemetry/opentelemetry-go.git
				synced 2025-10-31 00:07:40 +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:
		| @@ -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. | ||||
|   | ||||
| @@ -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") | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user