Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace/tracer: report number of instrumentations used as health metric #3021

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
12 changes: 12 additions & 0 deletions ddtrace/tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package tracer
import (
gocontext "context"
"encoding/binary"
"fmt"
"log/slog"
"math"
"os"
Expand Down Expand Up @@ -347,6 +348,17 @@ func newTracer(opts ...StartOption) *tracer {
t.abandonedSpansDebugger = newAbandonedSpansDebugger()
t.abandonedSpansDebugger.Start(t.config.spanTimeout)
}
for name, conf := range c.integrations {
if !conf.Instrumented {
continue
}
v := conf.Version
if v == "" {
v = "unknown"
}
t.statsd.Incr("datadog.tracer.instrumentations", []string{fmt.Sprintf("instrumentation:%s", name), fmt.Sprintf("instrumentation_version:%s", v)}, 1)
hannahkm marked this conversation as resolved.
Show resolved Hide resolved
}

t.wg.Add(1)
go func() {
defer t.wg.Done()
Expand Down
28 changes: 28 additions & 0 deletions ddtrace/tracer/tracer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,34 @@ func TestTracerStart(t *testing.T) {
tr.Stop()
tr.Stop()
})

t.Run("integration_health_metric", func(t *testing.T) {
assert := assert.New(t)
defer clearIntegrationsForTests()
var tg statsdtest.TestStatsdClient

ok := MarkIntegrationImported("github.com/go-chi/chi")
assert.True(ok)
tr, _, _, stop := startTestTracer(t, withStatsdClient(&tg))
defer stop()

tg.Wait(assert, 1, 100*time.Millisecond)

conf, ok := tr.config.integrations["chi"]
assert.True(ok)
assert.True(conf.Instrumented)

counts := tg.Counts()
assert.Equal(int64(1), counts["datadog.tracer.instrumentations"])

calls := tg.IncrCalls()
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")

})
}

func TestTracerLogFile(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions internal/statsdtest/statsdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func (tg *TestStatsdClient) addCount(name string, value int64) {
tg.counts[name] += value
}

func (tc *TestStatsdCall) GetTags() []string {
return tc.tags
}

func (tg *TestStatsdClient) Gauge(name string, value float64, tags []string, rate float64) error {
return tg.addMetric(callTypeGauge, tags, TestStatsdCall{
name: name,
Expand Down Expand Up @@ -214,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()
Expand Down
Loading