diff --git a/CHANGELOG.md b/CHANGELOG.md index 35a18a4d6bb..ab09da29b7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +## Changed + +- Skip links with invalid span context. (#2275) + ### Added - Adds `otlptracegrpc.WithGRPCConn` and `otlpmetricgrpc.WithGRPCConn` for reusing existing gRPC connection. (#2002) diff --git a/sdk/trace/span.go b/sdk/trace/span.go index cda1644409b..e4ed7fbc0d9 100644 --- a/sdk/trace/span.go +++ b/sdk/trace/span.go @@ -453,7 +453,7 @@ func (s *recordingSpan) Resource() *resource.Resource { } func (s *recordingSpan) addLink(link trace.Link) { - if !s.IsRecording() { + if !s.IsRecording() || !link.SpanContext.IsValid() { return } s.mu.Lock() diff --git a/sdk/trace/trace_test.go b/sdk/trace/trace_test.go index c4ab8443c2c..36ed7165cc2 100644 --- a/sdk/trace/trace_test.go +++ b/sdk/trace/trace_test.go @@ -670,6 +670,15 @@ func TestLinks(t *testing.T) { if diff := cmpDiff(got, want); diff != "" { t.Errorf("Link: -got +want %s", diff) } + sc1 = trace.NewSpanContext(trace.SpanContextConfig{TraceID: trace.TraceID([16]byte{1, 1}), SpanID: trace.SpanID{3}}) + + span1 := startSpan(tp, "name", trace.WithLinks([]trace.Link{ + {SpanContext: trace.SpanContext{}}, + {SpanContext: sc1}, + }...)) + + sdkspan, _ := span1.(*recordingSpan) + require.Len(t, sdkspan.Links(), 1) } func TestLinksOverLimit(t *testing.T) { diff --git a/trace/config.go b/trace/config.go index b1b74cb722d..8461a15ccd4 100644 --- a/trace/config.go +++ b/trace/config.go @@ -259,7 +259,7 @@ func WithStackTrace(b bool) SpanEndEventOption { } // WithLinks adds links to a Span. The links are added to the existing Span -// links, i.e. this does not overwrite. +// links, i.e. this does not overwrite. Links with invalid span context are ignored. func WithLinks(links ...Link) SpanStartOption { return spanOptionFunc(func(cfg *SpanConfig) { cfg.links = append(cfg.links, links...)