From 8365b6d9176221c99e67d327e404133ab8155ae2 Mon Sep 17 00:00:00 2001 From: dmathieu Date: Tue, 16 May 2023 15:23:26 +0200 Subject: [PATCH] set the proper trace ID and span ID in trace context --- module/apmotel/tracer.go | 8 ++++++++ module/apmotel/tracer_test.go | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/module/apmotel/tracer.go b/module/apmotel/tracer.go index 37ca3421b..3f638b41c 100644 --- a/module/apmotel/tracer.go +++ b/module/apmotel/tracer.go @@ -87,6 +87,10 @@ func (t *tracer) Start(ctx context.Context, spanName string, opts ...trace.SpanS Start: startTime, }) s.tx = parent.tx + s.spanContext = trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID(s.span.TraceContext().Trace), + SpanID: trace.SpanID(s.span.TraceContext().Span), + }) return trace.ContextWithSpan(ctx, s), s } } @@ -99,6 +103,10 @@ func (t *tracer) Start(ctx context.Context, spanName string, opts ...trace.SpanS } } s.tx = t.provider.apmTracer.StartTransactionOptions(spanName, "", tranOpts) + s.spanContext = trace.NewSpanContext(trace.SpanContextConfig{ + TraceID: trace.TraceID(s.tx.TraceContext().Trace), + SpanID: trace.SpanID(s.tx.TraceContext().Span), + }) return trace.ContextWithSpan(ctx, s), s } diff --git a/module/apmotel/tracer_test.go b/module/apmotel/tracer_test.go index e42c20b54..b2d16df40 100644 --- a/module/apmotel/tracer_test.go +++ b/module/apmotel/tracer_test.go @@ -53,6 +53,8 @@ func TestTracerStartTransaction(t *testing.T) { assert.NotNil(t, s.(*span).tx) assert.Nil(t, s.(*span).span) + + assert.True(t, trace.SpanContextFromContext(ctx).IsValid()) } func TestTracerStartTransactionWithParentContext(t *testing.T) { @@ -71,6 +73,8 @@ func TestTracerStartTransactionWithParentContext(t *testing.T) { assert.NotNil(t, s.(*span).tx) assert.Nil(t, s.(*span).span) + + assert.True(t, trace.SpanContextFromContext(ctx).IsValid()) } func TestTracerStartChildSpan(t *testing.T) {