From a8f097d458720d6fdd00e6753f3a6d070e83a1d5 Mon Sep 17 00:00:00 2001 From: Hannah Kim Date: Wed, 11 Dec 2024 15:05:03 -0500 Subject: [PATCH] ddtrace/tracer: test for different values of source --- ddtrace/tracer/metrics_test.go | 57 +++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/ddtrace/tracer/metrics_test.go b/ddtrace/tracer/metrics_test.go index 436856257d..0279fec7ce 100644 --- a/ddtrace/tracer/metrics_test.go +++ b/ddtrace/tracer/metrics_test.go @@ -6,9 +6,11 @@ package tracer import ( + "slices" "testing" "time" + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" globalinternal "gopkg.in/DataDog/dd-trace-go.v1/internal" "github.com/stretchr/testify/assert" @@ -64,24 +66,49 @@ func TestReportHealthMetrics(t *testing.T) { } func TestSpansStartedTags(t *testing.T) { - assert := assert.New(t) - var tg statsdtest.TestStatsdClient defer func(old time.Duration) { statsInterval = old }(statsInterval) - statsInterval = time.Nanosecond - - tracer, _, _, stop := startTestTracer(t, withStatsdClient(&tg)) - defer stop() - - tracer.StartSpan("operation").Finish() - - tg.Wait(assert, 1, 10*time.Second) - - counts := tg.Counts() - assert.Equal(int64(1), counts["datadog.tracer.spans_started"]) - calls := tg.CountCalls() - assert.Contains(calls[0].Tags, "source:manual") + statsInterval = time.Millisecond + + t.Run("default", func(t *testing.T) { + assert := assert.New(t) + tracer, _, _, stop := startTestTracer(t, withStatsdClient(&tg)) + defer stop() + + tracer.StartSpan("operation").Finish() + + tg.Wait(assert, 1, 100*time.Millisecond) + + counts := tg.Counts() + assert.Equal(int64(1), counts["datadog.tracer.spans_started"]) + for _, c := range tg.CountCalls() { + if slices.Equal(c.Tags, []string{"source:manual"}) { + return + } + } + assert.Fail("expected source:manual tag in spans_started") + }) + + t.Run("other_source", func(t *testing.T) { + tg.Reset() + assert := assert.New(t) + tracer, _, _, stop := startTestTracer(t, withStatsdClient(&tg)) + defer stop() + + tracer.StartSpan("operation", Tag(ext.Component, "contrib")).Finish() + tg.Wait(assert, 1, 100*time.Millisecond) + + counts := tg.Counts() + assert.Equal(int64(1), counts["datadog.tracer.spans_started"]) + for _, c := range tg.CountCalls() { + if slices.Equal(c.Tags, []string{"source:contrib"}) { + return + } + } + assert.Fail("expected source:contrib tag in spans_started") + + }) } func TestTracerMetrics(t *testing.T) {