-
Notifications
You must be signed in to change notification settings - Fork 87
/
instrumentation_test.go
209 lines (169 loc) · 6.5 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package auto
import (
"context"
"fmt"
"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"
)
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) {
const name = "debug"
mockEnv(t, map[string]string{"OTEL_LOG_LEVEL": name})
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.NoError(t, err)
assert.Equal(t, LogLevelDebug, c.logLevel)
const wrong = "invalid"
mockEnv(t, map[string]string{"OTEL_LOG_LEVEL": wrong})
_, err = newInstConfig(context.Background(), []InstrumentationOption{WithEnv()})
require.Error(t, err)
})
}
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 TestWithLogLevel(t *testing.T) {
t.Run("With Valid Input", func(t *testing.T) {
c, err := newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel("error")})
require.NoError(t, err)
assert.Equal(t, LogLevelError, c.logLevel)
c, err = newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel(LogLevelInfo)})
require.NoError(t, err)
assert.Equal(t, LogLevelInfo, c.logLevel)
})
t.Run("Will Validate Input", func(t *testing.T) {
_, err := newInstConfig(context.Background(), []InstrumentationOption{WithLogLevel("invalid")})
require.Error(t, err)
})
}
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
}
}