Skip to content

Commit 4ca1b3f

Browse files
committed
test(auto-instrumentation): add E2E and unit tests for spec.env propagation fix
Signed-off-by: Praful Khanduri <[email protected]>
1 parent 1ef9998 commit 4ca1b3f

File tree

13 files changed

+332
-110
lines changed

13 files changed

+332
-110
lines changed

internal/instrumentation/podmutator_test.go

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,10 +1372,6 @@ func TestMutatePod(t *testing.T) {
13721372
Name: "PYTHONPATH",
13731373
Value: fmt.Sprintf("%s:%s", pythonPathPrefix, pythonPathSuffix),
13741374
},
1375-
{
1376-
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1377-
Value: "http/protobuf",
1378-
},
13791375
{
13801376
Name: "OTEL_EXPORTER_OTLP_TIMEOUT",
13811377
Value: "20",
@@ -1416,6 +1412,11 @@ func TestMutatePod(t *testing.T) {
14161412
Name: "OTEL_RESOURCE_ATTRIBUTES",
14171413
Value: "k8s.container.name=app,k8s.namespace.name=python,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),service.instance.id=python.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).app,service.namespace=python",
14181414
},
1415+
1416+
{
1417+
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1418+
Value: "http/protobuf",
1419+
},
14191420
},
14201421
VolumeMounts: []corev1.VolumeMount{
14211422
{
@@ -1583,10 +1584,6 @@ func TestMutatePod(t *testing.T) {
15831584
Name: "PYTHONPATH",
15841585
Value: fmt.Sprintf("%s:%s", pythonPathPrefix, pythonPathSuffix),
15851586
},
1586-
{
1587-
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1588-
Value: "http/protobuf",
1589-
},
15901587
{
15911588
Name: "OTEL_EXPORTER_OTLP_TIMEOUT",
15921589
Value: "20",
@@ -1627,6 +1624,10 @@ func TestMutatePod(t *testing.T) {
16271624
Name: "OTEL_RESOURCE_ATTRIBUTES",
16281625
Value: "k8s.container.name=app1,k8s.namespace.name=python-multiple-containers,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),service.instance.id=python-multiple-containers.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).app1,service.namespace=python-multiple-containers",
16291626
},
1627+
{
1628+
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1629+
Value: "http/protobuf",
1630+
},
16301631
},
16311632
VolumeMounts: []corev1.VolumeMount{
16321633
{
@@ -1678,10 +1679,6 @@ func TestMutatePod(t *testing.T) {
16781679
Name: "PYTHONPATH",
16791680
Value: fmt.Sprintf("%s:%s", pythonPathPrefix, pythonPathSuffix),
16801681
},
1681-
{
1682-
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1683-
Value: "http/protobuf",
1684-
},
16851682
{
16861683
Name: "OTEL_EXPORTER_OTLP_TIMEOUT",
16871684
Value: "20",
@@ -1722,6 +1719,10 @@ func TestMutatePod(t *testing.T) {
17221719
Name: "OTEL_RESOURCE_ATTRIBUTES",
17231720
Value: "k8s.container.name=app2,k8s.namespace.name=python-multiple-containers,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),service.instance.id=python-multiple-containers.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).app2,service.namespace=python-multiple-containers",
17241721
},
1722+
{
1723+
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1724+
Value: "http/protobuf",
1725+
},
17251726
},
17261727
VolumeMounts: []corev1.VolumeMount{
17271728
{
@@ -4159,22 +4160,6 @@ func TestMutatePod(t *testing.T) {
41594160
Name: "PYTHONPATH",
41604161
Value: fmt.Sprintf("%s:%s", pythonPathPrefix, pythonPathSuffix),
41614162
},
4162-
{
4163-
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
4164-
Value: "http/protobuf",
4165-
},
4166-
{
4167-
Name: "OTEL_TRACES_EXPORTER",
4168-
Value: "otlp",
4169-
},
4170-
{
4171-
Name: "OTEL_METRICS_EXPORTER",
4172-
Value: "otlp",
4173-
},
4174-
{
4175-
Name: "OTEL_LOGS_EXPORTER",
4176-
Value: "otlp",
4177-
},
41784163
{
41794164
Name: "OTEL_SERVICE_NAME",
41804165
Value: "python1",
@@ -4203,6 +4188,22 @@ func TestMutatePod(t *testing.T) {
42034188
Name: "OTEL_RESOURCE_ATTRIBUTES",
42044189
Value: "k8s.container.name=python1,k8s.namespace.name=multi-instrumentation-multi-containers,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),service.instance.id=multi-instrumentation-multi-containers.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).python1,service.namespace=multi-instrumentation-multi-containers",
42054190
},
4191+
{
4192+
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
4193+
Value: "http/protobuf",
4194+
},
4195+
{
4196+
Name: "OTEL_TRACES_EXPORTER",
4197+
Value: "otlp",
4198+
},
4199+
{
4200+
Name: "OTEL_METRICS_EXPORTER",
4201+
Value: "otlp",
4202+
},
4203+
{
4204+
Name: "OTEL_LOGS_EXPORTER",
4205+
Value: "otlp",
4206+
},
42064207
},
42074208
VolumeMounts: []corev1.VolumeMount{
42084209
{
@@ -4238,22 +4239,6 @@ func TestMutatePod(t *testing.T) {
42384239
Name: "PYTHONPATH",
42394240
Value: fmt.Sprintf("%s:%s", pythonPathPrefix, pythonPathSuffix),
42404241
},
4241-
{
4242-
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
4243-
Value: "http/protobuf",
4244-
},
4245-
{
4246-
Name: "OTEL_TRACES_EXPORTER",
4247-
Value: "otlp",
4248-
},
4249-
{
4250-
Name: "OTEL_METRICS_EXPORTER",
4251-
Value: "otlp",
4252-
},
4253-
{
4254-
Name: "OTEL_LOGS_EXPORTER",
4255-
Value: "otlp",
4256-
},
42574242
{
42584243
Name: "OTEL_SERVICE_NAME",
42594244
Value: "python2",
@@ -4282,6 +4267,22 @@ func TestMutatePod(t *testing.T) {
42824267
Name: "OTEL_RESOURCE_ATTRIBUTES",
42834268
Value: "k8s.container.name=python2,k8s.namespace.name=multi-instrumentation-multi-containers,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),service.instance.id=multi-instrumentation-multi-containers.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).python2,service.namespace=multi-instrumentation-multi-containers",
42844269
},
4270+
{
4271+
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
4272+
Value: "http/protobuf",
4273+
},
4274+
{
4275+
Name: "OTEL_TRACES_EXPORTER",
4276+
Value: "otlp",
4277+
},
4278+
{
4279+
Name: "OTEL_METRICS_EXPORTER",
4280+
Value: "otlp",
4281+
},
4282+
{
4283+
Name: "OTEL_LOGS_EXPORTER",
4284+
Value: "otlp",
4285+
},
42854286
},
42864287
VolumeMounts: []corev1.VolumeMount{
42874288
{

internal/instrumentation/python_test.go

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ func TestInjectPythonSDK(t *testing.T) {
1717
tests := []struct {
1818
name string
1919
v1alpha1.Python
20-
pod corev1.Pod
21-
platform string
22-
expected corev1.Pod
23-
err error
20+
pod corev1.Pod
21+
platform string
22+
expected corev1.Pod
23+
err error
24+
inst v1alpha1.Instrumentation
25+
simulateDefaults bool
2426
}{
2527
{
2628
name: "PYTHONPATH not defined",
@@ -92,6 +94,108 @@ func TestInjectPythonSDK(t *testing.T) {
9294
},
9395
err: nil,
9496
},
97+
{
98+
name: "spec.env overrides defaults",
99+
Python: v1alpha1.Python{Image: "foo/bar:1"},
100+
pod: corev1.Pod{
101+
Spec: corev1.PodSpec{
102+
Containers: []corev1.Container{{}},
103+
},
104+
},
105+
platform: "glibc",
106+
inst: v1alpha1.Instrumentation{Spec: v1alpha1.InstrumentationSpec{Env: []corev1.EnvVar{{Name: "OTEL_METRICS_EXPORTER", Value: "none"}}}},
107+
simulateDefaults: true,
108+
expected: corev1.Pod{
109+
Spec: corev1.PodSpec{
110+
Volumes: []corev1.Volume{{
111+
Name: pythonVolumeName,
112+
VolumeSource: corev1.VolumeSource{
113+
EmptyDir: &corev1.EmptyDirVolumeSource{SizeLimit: &defaultVolumeLimitSize},
114+
},
115+
}},
116+
InitContainers: []corev1.Container{{
117+
Name: "opentelemetry-auto-instrumentation-python",
118+
Image: "foo/bar:1",
119+
Command: []string{"cp", "-r", "/autoinstrumentation/.", "/otel-auto-instrumentation-python"},
120+
VolumeMounts: []corev1.VolumeMount{{Name: pythonVolumeName, MountPath: "/otel-auto-instrumentation-python"}},
121+
}},
122+
Containers: []corev1.Container{{
123+
VolumeMounts: []corev1.VolumeMount{{Name: pythonVolumeName, MountPath: "/otel-auto-instrumentation-python"}},
124+
Env: []corev1.EnvVar{
125+
{
126+
Name: "OTEL_NODE_IP",
127+
ValueFrom: &corev1.EnvVarSource{
128+
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "status.hostIP"},
129+
},
130+
},
131+
{
132+
Name: "OTEL_POD_IP",
133+
ValueFrom: &corev1.EnvVarSource{
134+
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "status.podIP"},
135+
},
136+
},
137+
{Name: "PYTHONPATH", Value: fmt.Sprintf("%s:%s", "/otel-auto-instrumentation-python/opentelemetry/instrumentation/auto_instrumentation", "/otel-auto-instrumentation-python")},
138+
{Name: "OTEL_METRICS_EXPORTER", Value: "none"},
139+
{Name: "OTEL_EXPORTER_OTLP_PROTOCOL", Value: "http/protobuf"},
140+
{Name: "OTEL_TRACES_EXPORTER", Value: "otlp"},
141+
{Name: "OTEL_LOGS_EXPORTER", Value: "otlp"},
142+
},
143+
}},
144+
},
145+
},
146+
err: nil,
147+
},
148+
{
149+
name: "defaults applied when no spec.env",
150+
Python: v1alpha1.Python{Image: "foo/bar:1"},
151+
pod: corev1.Pod{
152+
Spec: corev1.PodSpec{
153+
Containers: []corev1.Container{{}},
154+
},
155+
},
156+
platform: "glibc",
157+
inst: v1alpha1.Instrumentation{},
158+
simulateDefaults: true,
159+
expected: corev1.Pod{
160+
Spec: corev1.PodSpec{
161+
Volumes: []corev1.Volume{{
162+
Name: pythonVolumeName,
163+
VolumeSource: corev1.VolumeSource{
164+
EmptyDir: &corev1.EmptyDirVolumeSource{SizeLimit: &defaultVolumeLimitSize},
165+
},
166+
}},
167+
InitContainers: []corev1.Container{{
168+
Name: "opentelemetry-auto-instrumentation-python",
169+
Image: "foo/bar:1",
170+
Command: []string{"cp", "-r", "/autoinstrumentation/.", "/otel-auto-instrumentation-python"},
171+
VolumeMounts: []corev1.VolumeMount{{Name: pythonVolumeName, MountPath: "/otel-auto-instrumentation-python"}},
172+
}},
173+
Containers: []corev1.Container{{
174+
VolumeMounts: []corev1.VolumeMount{{Name: pythonVolumeName, MountPath: "/otel-auto-instrumentation-python"}},
175+
Env: []corev1.EnvVar{
176+
{
177+
Name: "OTEL_NODE_IP",
178+
ValueFrom: &corev1.EnvVarSource{
179+
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "status.hostIP"},
180+
},
181+
},
182+
{
183+
Name: "OTEL_POD_IP",
184+
ValueFrom: &corev1.EnvVarSource{
185+
FieldRef: &corev1.ObjectFieldSelector{FieldPath: "status.podIP"},
186+
},
187+
},
188+
{Name: "PYTHONPATH", Value: fmt.Sprintf("%s:%s", "/otel-auto-instrumentation-python/opentelemetry/instrumentation/auto_instrumentation", "/otel-auto-instrumentation-python")},
189+
{Name: "OTEL_EXPORTER_OTLP_PROTOCOL", Value: "http/protobuf"},
190+
{Name: "OTEL_TRACES_EXPORTER", Value: "otlp"},
191+
{Name: "OTEL_METRICS_EXPORTER", Value: "otlp"},
192+
{Name: "OTEL_LOGS_EXPORTER", Value: "otlp"},
193+
},
194+
}},
195+
},
196+
},
197+
err: nil,
198+
},
95199
{
96200
name: "PYTHONPATH defined",
97201
Python: v1alpha1.Python{Image: "foo/bar:1", Resources: testResourceRequirements},
@@ -673,9 +777,22 @@ func TestInjectPythonSDK(t *testing.T) {
673777
},
674778
}
675779

780+
injector := sdkInjector{}
676781
for _, test := range tests {
677782
t.Run(test.name, func(t *testing.T) {
678783
pod, err := injectPythonSDK(test.Python, test.pod, 0, test.platform, v1alpha1.InstrumentationSpec{})
784+
if err != nil {
785+
assert.Equal(t, test.expected, pod)
786+
assert.Equal(t, test.err, err)
787+
return
788+
}
789+
790+
if test.simulateDefaults {
791+
pod = injector.injectCommonEnvVar(test.inst, pod, 0)
792+
}
793+
794+
pod = injector.injectDefaultEnvVars(pod, 0, getDefaultPythonEnvVars()...)
795+
679796
assert.Equal(t, test.expected, pod)
680797
assert.Equal(t, test.err, err)
681798
})

internal/instrumentation/sdk_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,22 +1226,6 @@ func TestInjectPython(t *testing.T) {
12261226
Name: "PYTHONPATH",
12271227
Value: fmt.Sprintf("%s:%s", pythonPathPrefix, pythonPathSuffix),
12281228
},
1229-
{
1230-
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1231-
Value: "http/protobuf",
1232-
},
1233-
{
1234-
Name: "OTEL_TRACES_EXPORTER",
1235-
Value: "otlp",
1236-
},
1237-
{
1238-
Name: "OTEL_METRICS_EXPORTER",
1239-
Value: "otlp",
1240-
},
1241-
{
1242-
Name: "OTEL_LOGS_EXPORTER",
1243-
Value: "otlp",
1244-
},
12451229
{
12461230
Name: "OTEL_SERVICE_NAME",
12471231
Value: "app",
@@ -1270,6 +1254,22 @@ func TestInjectPython(t *testing.T) {
12701254
Name: "OTEL_RESOURCE_ATTRIBUTES",
12711255
Value: "k8s.container.name=app,k8s.namespace.name=ns,k8s.node.name=$(OTEL_RESOURCE_ATTRIBUTES_NODE_NAME),k8s.pod.name=$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME),service.instance.id=ns.$(OTEL_RESOURCE_ATTRIBUTES_POD_NAME).app,service.namespace=ns,service.version=latest",
12721256
},
1257+
{
1258+
Name: "OTEL_EXPORTER_OTLP_PROTOCOL",
1259+
Value: "http/protobuf",
1260+
},
1261+
{
1262+
Name: "OTEL_TRACES_EXPORTER",
1263+
Value: "otlp",
1264+
},
1265+
{
1266+
Name: "OTEL_METRICS_EXPORTER",
1267+
Value: "otlp",
1268+
},
1269+
{
1270+
Name: "OTEL_LOGS_EXPORTER",
1271+
Value: "otlp",
1272+
},
12731273
},
12741274
},
12751275
},

0 commit comments

Comments
 (0)