diff --git a/ddtrace/tracer/spancontext.go b/ddtrace/tracer/spancontext.go index 30a791ee8e..11f9b62161 100644 --- a/ddtrace/tracer/spancontext.go +++ b/ddtrace/tracer/spancontext.go @@ -242,6 +242,9 @@ func (c *spanContext) baggageItem(key string) string { } func (c *spanContext) meta(key string) (val string, ok bool) { + if c.span == nil { + return "", false + } c.span.RLock() defer c.span.RUnlock() val, ok = c.span.Meta[key] diff --git a/ddtrace/tracer/sqlcomment_test.go b/ddtrace/tracer/sqlcomment_test.go index 633f7a68b4..4ce4ecc9f4 100644 --- a/ddtrace/tracer/sqlcomment_test.go +++ b/ddtrace/tracer/sqlcomment_test.go @@ -213,6 +213,35 @@ func TestSQLCommentCarrier(t *testing.T) { } } +// https://github.com/DataDog/dd-trace-go/issues/2837 +func TestSQLCommentCarrierInjectNilSpan(t *testing.T) { + tracer := newTracer() + defer tracer.Stop() + + headers := TextMapCarrier(map[string]string{ + DefaultTraceIDHeader: "4", + DefaultParentIDHeader: "1", + originHeader: "synthetics", + b3TraceIDHeader: "0021dc1807524785", + traceparentHeader: "00-00000000000000000000000000000004-2222222222222222-01", + tracestateHeader: "dd=s:2;o:rum;p:0000000000000001;t.tid:1230000000000000~~,othervendor=t61rcWkgMzE", + }) + + spanCtx, err := tracer.Extract(headers) + require.NoError(t, err) + + carrier := SQLCommentCarrier{ + Query: "SELECT * from FOO", + Mode: DBMPropagationModeFull, + DBServiceName: "whiskey-db", + PeerDBHostname: "", + PeerDBName: "", + PeerService: "", + } + err = carrier.Inject(spanCtx) + require.NoError(t, err) +} + func TestExtractOpenTelemetryTraceInformation(t *testing.T) { // open-telemetry supports 128 bit trace ids traceID := "5bd66ef5095369c7b0d1f8f4bd33716a"