diff --git a/internal/metrics/listener.go b/internal/metrics/listener.go index 96a328b..297d08d 100644 --- a/internal/metrics/listener.go +++ b/internal/metrics/listener.go @@ -61,6 +61,12 @@ type ( } ) +var ( + // runtimeTag is the metrics tag representing the go runtime version. Use + // this rather than calling getRuntimeTag directly. + runtimeTag = getRuntimeTag(runtime.Version()) +) + // MakeListener initializes a new metrics lambda listener func MakeListener(config Config, extensionManager *extension.ExtensionManager) Listener { @@ -173,7 +179,7 @@ func (l *Listener) HandlerFinished(ctx context.Context, err error) { func (l *Listener) AddDistributionMetric(metric string, value float64, timestamp time.Time, forceLogForwarder bool, tags ...string) { // We add our own runtime tag to the metric for version tracking - tags = append(tags, getRuntimeTag()) + tags = append(tags, runtimeTag) if l.isAgentRunning { err := l.statsdClient.Distribution(metric, value, tags, 1) @@ -217,8 +223,12 @@ func (l *Listener) AddDistributionMetric(metric string, value float64, timestamp l.processor.AddMetric(&m) } -func getRuntimeTag() string { - v := runtime.Version() +// getRuntimeTag returns the runtime tag to be used when creating distribution +// metrics. It should not be called directly, instead use the global +// runtimeTag var. +func getRuntimeTag(v string) string { + v = strings.ReplaceAll(v, " ", "-") + v = strings.ReplaceAll(v, ",", "-") return fmt.Sprintf("dd_lambda_layer:datadog-%s", v) } diff --git a/internal/metrics/listener_test.go b/internal/metrics/listener_test.go index 87fe99a..a6ad6d7 100644 --- a/internal/metrics/listener_test.go +++ b/internal/metrics/listener_test.go @@ -110,9 +110,9 @@ func TestAddDistributionMetricWithFIPSMode(t *testing.T) { // Create a listener with FIPS mode enabled listener := MakeListener(Config{ - APIKey: "12345", - Site: server.URL, - FIPSMode: true, + APIKey: "12345", + Site: server.URL, + FIPSMode: true, }, &extension.ExtensionManager{}) // Verify the API client wasn't created @@ -205,6 +205,7 @@ func TestSubmitEnhancedMetrics(t *testing.T) { assert.False(t, called) expected := "{\"m\":\"aws.lambda.enhanced.invocations\",\"v\":1," assert.True(t, strings.Contains(output, expected)) + assert.True(t, strings.Contains(output, "dd_lambda_layer:datadog-go1.")) } func TestDoNotSubmitEnhancedMetrics(t *testing.T) { @@ -290,3 +291,23 @@ func TestListenerHandlerFinishedFlushes(t *testing.T) { }) } } + +func TestGetRuntimeTag(t *testing.T) { + testcases := []struct { + runtimeVersion string + expect string + }{ + {"", "dd_lambda_layer:datadog-"}, + {"go1.25.1", "dd_lambda_layer:datadog-go1.25.1"}, + // runtime.Version() will include any values from the GOEXPERIMENT env var + {"go1.25.1 X:jsonv2", "dd_lambda_layer:datadog-go1.25.1-X:jsonv2"}, + {"go1.25.1 X:fieldtrace,jsonv2", "dd_lambda_layer:datadog-go1.25.1-X:fieldtrace-jsonv2"}, + } + + for _, tc := range testcases { + t.Run(tc.runtimeVersion, func(t *testing.T) { + actual := getRuntimeTag(tc.runtimeVersion) + assert.Equal(t, actual, tc.expect) + }) + } +}