Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions internal/metrics/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
27 changes: 24 additions & 3 deletions internal/metrics/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
})
}
}
Loading