Skip to content

Commit f562fb5

Browse files
authored
APM: Reduce number of allocs from sampler metrics (#33417)
1 parent b9eb4a2 commit f562fb5

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

pkg/trace/sampler/metrics.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,33 @@ type metrics struct {
2424
value map[metricsKey]metricsValue
2525
}
2626

27-
type metricsKey [3]string
27+
type metricsKey struct {
28+
targetService string
29+
targetEnv string
30+
samplingPriority string
31+
}
2832

2933
func newMetricsKey(service, env string, samplingPriority *SamplingPriority) metricsKey {
30-
var key metricsKey
31-
if service != "" {
32-
key[0] = "target_service:" + service
33-
}
34-
if env != "" {
35-
key[1] = "target_env:" + env
34+
mk := metricsKey{
35+
targetService: service,
36+
targetEnv: env,
3637
}
3738
if samplingPriority != nil {
38-
key[2] = samplingPriority.tag()
39+
mk.samplingPriority = samplingPriority.tagValue()
3940
}
40-
return key
41+
return mk
4142
}
4243

4344
func (k metricsKey) tags() []string {
44-
tags := make([]string, 0, len(k))
45-
for _, v := range k {
46-
if v != "" {
47-
tags = append(tags, v)
48-
}
45+
tags := make([]string, 0, 3) // Pre-allocate number of fields for efficiency
46+
if k.targetService != "" {
47+
tags = append(tags, "target_service:"+k.targetService)
48+
}
49+
if k.targetEnv != "" {
50+
tags = append(tags, "target_env:"+k.targetEnv)
51+
}
52+
if k.samplingPriority != "" {
53+
tags = append(tags, "sampling_priority:"+k.samplingPriority)
4954
}
5055
return tags
5156
}

pkg/trace/sampler/prioritysampler_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import (
1010
"testing"
1111
"time"
1212

13+
"github.com/golang/mock/gomock"
14+
"github.com/stretchr/testify/assert"
15+
"go.uber.org/atomic"
16+
1317
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
1418
"github.com/DataDog/datadog-agent/pkg/trace/config"
1519
"github.com/DataDog/datadog-go/v5/statsd"
1620
mockStatsd "github.com/DataDog/datadog-go/v5/statsd/mocks"
17-
"github.com/golang/mock/gomock"
18-
"github.com/stretchr/testify/assert"
19-
"go.uber.org/atomic"
2021
)
2122

2223
func randomTraceID() uint64 {
@@ -89,7 +90,7 @@ func TestPrioritySample(t *testing.T) {
8990
},
9091
}
9192
for _, tt := range tests {
92-
t.Run(tt.priority.tag(), func(t *testing.T) {
93+
t.Run(tt.priority.tagValue(), func(t *testing.T) {
9394
ctrl := gomock.NewController(t)
9495
defer ctrl.Finish()
9596
statsdClient := mockStatsd.NewMockClientInterface(ctrl)
@@ -107,7 +108,7 @@ func TestPrioritySample(t *testing.T) {
107108
if tt.priority == PriorityNone {
108109
expectedTagsA = append(expectedTagsA, "sampling_priority:auto_drop")
109110
} else {
110-
expectedTagsA = append(expectedTagsA, tt.priority.tag())
111+
expectedTagsA = append(expectedTagsA, "sampling_priority:"+tt.priority.tagValue())
111112
}
112113
chunkA, rootA := getTestTraceWithService("service-a", s)
113114
chunkA.Priority = int32(tt.priority)
@@ -121,7 +122,7 @@ func TestPrioritySample(t *testing.T) {
121122
if tt.priority == PriorityNone {
122123
expectedTagsB = append(expectedTagsB, "sampling_priority:auto_drop")
123124
} else {
124-
expectedTagsB = append(expectedTagsB, tt.priority.tag())
125+
expectedTagsB = append(expectedTagsB, "sampling_priority:"+tt.priority.tagValue())
125126
}
126127
chunkB, rootB := getTestTraceWithService("service-b", s)
127128
chunkB.Priority = int32(tt.priority)

pkg/trace/sampler/sampler.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,19 @@ const (
6868
samplerHasher = uint64(1111111111111111111)
6969
)
7070

71-
func (s SamplingPriority) tag() string {
72-
var v string
71+
func (s SamplingPriority) tagValue() string {
7372
switch s {
7473
case PriorityUserDrop:
75-
v = "manual_drop"
74+
return "manual_drop"
7675
case PriorityAutoDrop:
77-
v = "auto_drop"
76+
return "auto_drop"
7877
case PriorityAutoKeep:
79-
v = "auto_keep"
78+
return "auto_keep"
8079
case PriorityUserKeep:
81-
v = "manual_keep"
80+
return "manual_keep"
8281
default:
83-
v = "none"
82+
return "none"
8483
}
85-
return "sampling_priority:" + v
8684
}
8785

8886
// SampleByRate returns whether to keep a trace, based on its ID and a sampling rate.

0 commit comments

Comments
 (0)