-
Notifications
You must be signed in to change notification settings - Fork 91
/
instrumentation_test.go
289 lines (247 loc) · 9.56 KB
/
instrumentation_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package auto
import (
"context"
"fmt"
"log/slog"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe/sampling"
)
func TestWithServiceName(t *testing.T) {
ctx := context.Background()
testServiceName := "test_serviceName"
// Use WithServiceName to config the service name
c, err := newInstConfig(ctx, []InstrumentationOption{WithServiceName((testServiceName))})
require.NoError(t, err)
assert.Equal(t, testServiceName, c.serviceName)
// No service name provided - check for default value
c, err = newInstConfig(ctx, []InstrumentationOption{})
require.NoError(t, err)
assert.Equal(t, c.defaultServiceName(), c.serviceName)
}
func TestWithPID(t *testing.T) {
ctx := context.Background()
c, err := newInstConfig(ctx, []InstrumentationOption{WithPID(1)})
require.NoError(t, err)
assert.Equal(t, 1, c.target.Pid)
const exe = "./test/path/program/run.go"
// PID should override valid target exe
c, err = newInstConfig(ctx, []InstrumentationOption{WithTarget(exe), WithPID(1)})
require.NoError(t, err)
assert.Equal(t, 1, c.target.Pid)
assert.Equal(t, "", c.target.ExePath)
}
func TestWithEnv(t *testing.T) {
t.Run("OTEL_GO_AUTO_TARGET_EXE", func(t *testing.T) {
const path = "./test/path/program/run.go"
mockEnv(t, map[string]string{"OTEL_GO_AUTO_TARGET_EXE": path})
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, path, c.target.ExePath)
assert.Equal(t, 0, c.target.Pid)
})
t.Run("OTEL_SERVICE_NAME", func(t *testing.T) {
const name = "test_service"
mockEnv(t, map[string]string{"OTEL_SERVICE_NAME": name})
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, name, c.serviceName)
})
t.Run("OTEL_RESOURCE_ATTRIBUTES", func(t *testing.T) {
const name = "test_service"
val := fmt.Sprintf("a=b,fubar,%s=%s,foo=bar", semconv.ServiceNameKey, name)
mockEnv(t, map[string]string{"OTEL_RESOURCE_ATTRIBUTES": val})
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, name, c.serviceName)
})
t.Run("OTEL_LOG_LEVEL", func(t *testing.T) {
orig := newLogger
var got slog.Leveler
newLogger = func(level slog.Leveler) *slog.Logger {
got = level
return newLoggerFunc(level)
}
t.Cleanup(func() { newLogger = orig })
t.Setenv(envLogLevelKey, "debug")
ctx, opts := context.Background(), []InstrumentationOption{WithEnv()}
_, err := newInstConfig(ctx, opts)
require.NoError(t, err)
assert.Equal(t, slog.LevelDebug, got)
t.Setenv(envLogLevelKey, "invalid")
_, err = newInstConfig(ctx, opts)
require.ErrorContains(t, err, `parse log level "invalid"`)
})
}
func TestOptionPrecedence(t *testing.T) {
const (
path = "./test/path/program/run.go"
name = "test_service"
)
t.Run("Env", func(t *testing.T) {
mockEnv(t, map[string]string{
"OTEL_GO_AUTO_TARGET_EXE": path,
"OTEL_SERVICE_NAME": name,
})
// WithEnv passed last, it should have precedence.
opts := []InstrumentationOption{
WithPID(1),
WithServiceName("wrong"),
WithEnv(),
}
c, err := newInstConfig(context.Background(), opts)
require.NoError(t, err)
assert.Equal(t, path, c.target.ExePath)
assert.Equal(t, 0, c.target.Pid)
assert.Equal(t, name, c.serviceName)
})
t.Run("Options", func(t *testing.T) {
mockEnv(t, map[string]string{
"OTEL_GO_AUTO_TARGET_EXE": path,
"OTEL_SERVICE_NAME": "wrong",
})
// WithEnv passed first, it should be overridden.
opts := []InstrumentationOption{
WithEnv(),
WithPID(1),
WithServiceName(name),
}
c, err := newInstConfig(context.Background(), opts)
require.NoError(t, err)
assert.Equal(t, "", c.target.ExePath)
assert.Equal(t, 1, c.target.Pid)
assert.Equal(t, name, c.serviceName)
})
}
func TestWithResourceAttributes(t *testing.T) {
t.Run("By Code", func(t *testing.T) {
attr1 := semconv.K8SContainerName("test_container_name")
attr2 := semconv.K8SPodName("test_pod_name")
attr3 := semconv.K8SNamespaceName("test_namespace_name")
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithResourceAttributes(attr1, attr2), WithResourceAttributes(attr3)})
require.NoError(t, err)
assert.Equal(t, []attribute.KeyValue{attr1, attr2, attr3}, c.additionalResAttrs)
})
t.Run("By Env", func(t *testing.T) {
nameAttr := semconv.ServiceName("test_service")
attr2 := semconv.K8SPodName("test_pod_name")
attr3 := semconv.K8SNamespaceName("test_namespace_name")
mockEnv(t, map[string]string{
"OTEL_RESOURCE_ATTRIBUTES": fmt.Sprintf("%s=%s,%s=%s,%s=%s", nameAttr.Key, nameAttr.Value.AsString(), attr2.Key, attr2.Value.AsString(), attr3.Key, attr3.Value.AsString()),
})
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, nameAttr.Value.AsString(), c.serviceName)
assert.Equal(t, []attribute.KeyValue{attr2, attr3}, c.additionalResAttrs)
})
t.Run("By Code and Env", func(t *testing.T) {
nameAttr := semconv.ServiceName("test_service")
attr2 := semconv.K8SPodName("test_pod_name")
attr3 := semconv.K8SNamespaceName("test_namespace_name")
mockEnv(t, map[string]string{
"OTEL_RESOURCE_ATTRIBUTES": fmt.Sprintf("%s=%s,%s=%s", nameAttr.Key, nameAttr.Value.AsString(), attr2.Key, attr2.Value.AsString()),
})
// Use WithResourceAttributes to config the additional resource attributes
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv(), WithResourceAttributes(attr3)})
require.NoError(t, err)
assert.Equal(t, nameAttr.Value.AsString(), c.serviceName)
assert.Equal(t, []attribute.KeyValue{attr2, attr3}, c.additionalResAttrs)
})
}
func TestWithLogger(t *testing.T) {
l := slog.New(slog.Default().Handler())
opts := []InstrumentationOption{WithLogger(l)}
c, err := newInstConfig(context.Background(), opts)
require.NoError(t, err)
assert.Same(t, l, c.logger)
}
func TestWithSampler(t *testing.T) {
t.Run("Default sampler", func(t *testing.T) {
c, err := newInstConfig(context.Background(), []InstrumentationOption{})
require.NoError(t, err)
sc, err := convertSamplerToConfig(c.sampler)
assert.NoError(t, err)
assert.Equal(t, sc.Samplers, sampling.DefaultConfig().Samplers)
assert.Equal(t, sc.ActiveSampler, sampling.ParentBasedID)
conf, ok := sc.Samplers[sampling.ParentBasedID]
assert.True(t, ok)
assert.Equal(t, conf.SamplerType, sampling.SamplerParentBased)
pbConfig, ok := conf.Config.(sampling.ParentBasedConfig)
assert.True(t, ok)
assert.Equal(t, pbConfig, sampling.DefaultParentBasedSampler())
})
t.Run("Env config", func(t *testing.T) {
mockEnv(t, map[string]string{
tracesSamplerKey: samplerNameParentBasedTraceIDRatio,
tracesSamplerArgKey: "0.42",
})
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
sc, err := convertSamplerToConfig(c.sampler)
assert.NoError(t, err)
assert.Equal(t, sc.ActiveSampler, sampling.ParentBasedID)
parentBasedConfig, ok := sc.Samplers[sampling.ParentBasedID]
assert.True(t, ok)
assert.Equal(t, parentBasedConfig.SamplerType, sampling.SamplerParentBased)
pbConfig, ok := parentBasedConfig.Config.(sampling.ParentBasedConfig)
assert.True(t, ok)
assert.Equal(t, pbConfig.Root, sampling.TraceIDRatioID)
tidRatio, ok := sc.Samplers[sampling.TraceIDRatioID]
assert.True(t, ok)
assert.Equal(t, tidRatio.SamplerType, sampling.SamplerTraceIDRatio)
config, ok := tidRatio.Config.(sampling.TraceIDRatioConfig)
assert.True(t, ok)
expected, _ := sampling.NewTraceIDRatioConfig(0.42)
assert.Equal(t, expected, config)
})
t.Run("Invalid Env config", func(t *testing.T) {
mockEnv(t, map[string]string{
tracesSamplerKey: "invalid",
tracesSamplerArgKey: "0.42",
})
_, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.Error(t, err)
require.Contains(t, err.Error(), "unknown sampler name")
})
t.Run("WithSampler", func(t *testing.T) {
c, err := newInstConfig(context.Background(), []InstrumentationOption{
WithSampler(ParentBasedSampler{
Root: TraceIDRatioSampler{Fraction: 0.42},
}),
})
require.NoError(t, err)
sc, err := convertSamplerToConfig(c.sampler)
assert.NoError(t, err)
assert.Equal(t, sc.ActiveSampler, sampling.ParentBasedID)
parentBasedConfig, ok := sc.Samplers[sampling.ParentBasedID]
assert.True(t, ok)
assert.Equal(t, parentBasedConfig.SamplerType, sampling.SamplerParentBased)
pbConfig, ok := parentBasedConfig.Config.(sampling.ParentBasedConfig)
assert.True(t, ok)
assert.Equal(t, pbConfig.Root, sampling.TraceIDRatioID)
assert.Equal(t, pbConfig.RemoteSampled, sampling.AlwaysOnID)
assert.Equal(t, pbConfig.RemoteNotSampled, sampling.AlwaysOffID)
assert.Equal(t, pbConfig.LocalSampled, sampling.AlwaysOnID)
assert.Equal(t, pbConfig.LocalNotSampled, sampling.AlwaysOffID)
tidRatio, ok := sc.Samplers[sampling.TraceIDRatioID]
assert.True(t, ok)
assert.Equal(t, tidRatio.SamplerType, sampling.SamplerTraceIDRatio)
config, ok := tidRatio.Config.(sampling.TraceIDRatioConfig)
assert.True(t, ok)
expected, _ := sampling.NewTraceIDRatioConfig(0.42)
assert.Equal(t, expected, config)
})
}
func mockEnv(t *testing.T, env map[string]string) {
orig := lookupEnv
t.Cleanup(func() { lookupEnv = orig })
lookupEnv = func(key string) (string, bool) {
v, ok := env[key]
return v, ok
}
}