From 565a5b825dcacad0a693f5702d0db75c8e2dce9c Mon Sep 17 00:00:00 2001 From: Hannah Kim Date: Tue, 17 Dec 2024 10:14:39 -0500 Subject: [PATCH] use FilterCallsByName to simplify tests --- ddtrace/tracer/tracer_test.go | 8 +++----- internal/statsdtest/statsdtest.go | 14 ++++++++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/ddtrace/tracer/tracer_test.go b/ddtrace/tracer/tracer_test.go index 3d099c826c..c95eb21804 100644 --- a/ddtrace/tracer/tracer_test.go +++ b/ddtrace/tracer/tracer_test.go @@ -276,11 +276,9 @@ func TestTracerStart(t *testing.T) { assert.Equal(int64(1), counts["datadog.tracer.instrumentations"]) calls := tg.IncrCalls() - for _, c := range calls { - if c.GetName() == "datadog.tracer.instrumentations" { - assert.EqualValues(c.GetTags(), []string{"instrumentation:chi", "instrumentation_version:unknown"}) - return - } + for _, c := range statsdtest.FilterCallsByName(calls, "datadog.tracer.instrumentations") { + assert.EqualValues(c.GetTags(), []string{"instrumentation:chi", "instrumentation_version:unknown"}) + return } assert.Fail("expected instrumentation to have appropriate tags") diff --git a/internal/statsdtest/statsdtest.go b/internal/statsdtest/statsdtest.go index 4981d413fc..af41d118fe 100644 --- a/internal/statsdtest/statsdtest.go +++ b/internal/statsdtest/statsdtest.go @@ -59,10 +59,6 @@ func (tc *TestStatsdCall) GetTags() []string { return tc.tags } -func (tc *TestStatsdCall) GetName() string { - return tc.name -} - func (tg *TestStatsdClient) Gauge(name string, value float64, tags []string, rate float64) error { return tg.addMetric(callTypeGauge, tags, TestStatsdCall{ name: name, @@ -222,6 +218,16 @@ func (tg *TestStatsdClient) CallsByName() map[string]int { return counts } +func FilterCallsByName(calls []TestStatsdCall, name string) []TestStatsdCall { + var matches []TestStatsdCall + for _, c := range calls { + if c.name == name { + matches = append(matches, c) + } + } + return matches +} + func (tg *TestStatsdClient) Counts() map[string]int64 { tg.mu.RLock() defer tg.mu.RUnlock()