Skip to content

Commit a3919b9

Browse files
authored
Instrumentations support Java extensions (#2761)
* Support extensions * fix with cr * fix document * fix lint
1 parent 17abca5 commit a3919b9

10 files changed

+235
-11
lines changed

.chloggen/support-extensions.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
5+
component: auto-instrumentation
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Support Java auto-instrumentation extensions.
9+
10+
# One or more tracking issues related to the change
11+
issues: [1785]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

apis/v1alpha1/instrumentation_types.go

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ type Java struct {
133133
// Resources describes the compute resource requirements.
134134
// +optional
135135
Resources corev1.ResourceRequirements `json:"resources,omitempty"`
136+
137+
// Extensions defines java specific extensions.
138+
// All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.
139+
// +optional
140+
Extensions []Extensions `json:"extensions,omitempty"`
141+
}
142+
143+
type Extensions struct {
144+
// Image is a container image with extensions auto-instrumentation JAR.
145+
Image string `json:"image"`
146+
147+
// Dir is a directory with extensions auto-instrumentation JAR.
148+
Dir string `json:"dir"`
136149
}
137150

138151
// NodeJS defines NodeJS SDK and instrumentation configuration.

apis/v1alpha1/zz_generated.deepcopy.go

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundle/manifests/opentelemetry.io_instrumentations.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,25 @@ spec:
919919
- name
920920
type: object
921921
type: array
922+
extensions:
923+
description: |-
924+
Extensions defines java specific extensions.
925+
All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.
926+
items:
927+
properties:
928+
dir:
929+
description: Dir is a directory with extensions auto-instrumentation
930+
JAR.
931+
type: string
932+
image:
933+
description: Image is a container image with extensions
934+
auto-instrumentation JAR.
935+
type: string
936+
required:
937+
- dir
938+
- image
939+
type: object
940+
type: array
922941
image:
923942
description: Image is a container image with javaagent auto-instrumentation
924943
JAR.

config/crd/bases/opentelemetry.io_instrumentations.yaml

+19
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,25 @@ spec:
917917
- name
918918
type: object
919919
type: array
920+
extensions:
921+
description: |-
922+
Extensions defines java specific extensions.
923+
All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.
924+
items:
925+
properties:
926+
dir:
927+
description: Dir is a directory with extensions auto-instrumentation
928+
JAR.
929+
type: string
930+
image:
931+
description: Image is a container image with extensions
932+
auto-instrumentation JAR.
933+
type: string
934+
required:
935+
- dir
936+
- image
937+
type: object
938+
type: array
920939
image:
921940
description: Image is a container image with javaagent auto-instrumentation
922941
JAR.

docs/api.md

+42
Original file line numberDiff line numberDiff line change
@@ -1970,6 +1970,14 @@ the precedence order is: `original container env vars` > `language specific env
19701970
If the former var had been defined, then the other vars would be ignored.<br/>
19711971
</td>
19721972
<td>false</td>
1973+
</tr><tr>
1974+
<td><b><a href="#instrumentationspecjavaextensionsindex">extensions</a></b></td>
1975+
<td>[]object</td>
1976+
<td>
1977+
Extensions defines java specific extensions.
1978+
All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.<br/>
1979+
</td>
1980+
<td>false</td>
19731981
</tr><tr>
19741982
<td><b>image</b></td>
19751983
<td>string</td>
@@ -2258,6 +2266,40 @@ TODO: Add other useful fields. apiVersion, kind, uid?<br/>
22582266
</table>
22592267

22602268

2269+
### Instrumentation.spec.java.extensions[index]
2270+
<sup><sup>[↩ Parent](#instrumentationspecjava)</sup></sup>
2271+
2272+
2273+
2274+
2275+
2276+
<table>
2277+
<thead>
2278+
<tr>
2279+
<th>Name</th>
2280+
<th>Type</th>
2281+
<th>Description</th>
2282+
<th>Required</th>
2283+
</tr>
2284+
</thead>
2285+
<tbody><tr>
2286+
<td><b>dir</b></td>
2287+
<td>string</td>
2288+
<td>
2289+
Dir is a directory with extensions auto-instrumentation JAR.<br/>
2290+
</td>
2291+
<td>true</td>
2292+
</tr><tr>
2293+
<td><b>image</b></td>
2294+
<td>string</td>
2295+
<td>
2296+
Image is a container image with extensions auto-instrumentation JAR.<br/>
2297+
</td>
2298+
<td>true</td>
2299+
</tr></tbody>
2300+
</table>
2301+
2302+
22612303
### Instrumentation.spec.java.resources
22622304
<sup><sup>[↩ Parent](#instrumentationspecjava)</sup></sup>
22632305

pkg/instrumentation/javaagent.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
package instrumentation
1616

1717
import (
18+
"fmt"
19+
1820
corev1 "k8s.io/api/core/v1"
1921

2022
"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
2123
)
2224

2325
const (
2426
envJavaToolsOptions = "JAVA_TOOL_OPTIONS"
25-
javaJVMArgument = " -javaagent:/otel-auto-instrumentation-java/javaagent.jar"
27+
javaAgent = " -javaagent:/otel-auto-instrumentation-java/javaagent.jar"
2628
javaInitContainerName = initContainerName + "-java"
2729
javaVolumeName = volumeName + "-java"
2830
javaInstrMountPath = "/otel-auto-instrumentation-java"
@@ -45,6 +47,11 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
4547
}
4648
}
4749

50+
javaJVMArgument := javaAgent
51+
if len(javaSpec.Extensions) > 0 {
52+
javaJVMArgument = javaAgent + fmt.Sprintf(" -Dotel.javaagent.extensions=%s/extensions", javaInstrMountPath)
53+
}
54+
4855
idx := getIndexOfEnv(container.Env, envJavaToolsOptions)
4956
if idx == -1 {
5057
container.Env = append(container.Env, corev1.EnvVar{
@@ -80,6 +87,20 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
8087
MountPath: javaInstrMountPath,
8188
}},
8289
})
90+
91+
for i, extension := range javaSpec.Extensions {
92+
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
93+
Name: initContainerName + fmt.Sprintf("-extension-%d", i),
94+
Image: extension.Image,
95+
Command: []string{"cp", "-r", extension.Dir + "/.", javaInstrMountPath + "/extensions"},
96+
Resources: javaSpec.Resources,
97+
VolumeMounts: []corev1.VolumeMount{{
98+
Name: javaVolumeName,
99+
MountPath: javaInstrMountPath,
100+
}},
101+
})
102+
}
103+
83104
}
84105
return pod, err
85106
}

pkg/instrumentation/javaagent_test.go

+76-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,81 @@ func TestInjectJavaagent(t *testing.T) {
7676
Env: []corev1.EnvVar{
7777
{
7878
Name: "JAVA_TOOL_OPTIONS",
79-
Value: javaJVMArgument,
79+
Value: javaAgent,
80+
},
81+
},
82+
},
83+
},
84+
},
85+
},
86+
err: nil,
87+
},
88+
{
89+
name: "add extensions to JAVA_TOOL_OPTIONS",
90+
Java: v1alpha1.Java{Image: "foo/bar:1", Extensions: []v1alpha1.Extensions{
91+
{Image: "ex/ex:0", Dir: "/ex0"},
92+
{Image: "ex/ex:1", Dir: "/ex1"},
93+
}},
94+
pod: corev1.Pod{
95+
Spec: corev1.PodSpec{
96+
Containers: []corev1.Container{
97+
{},
98+
},
99+
},
100+
},
101+
expected: corev1.Pod{
102+
Spec: corev1.PodSpec{
103+
Volumes: []corev1.Volume{
104+
{
105+
Name: "opentelemetry-auto-instrumentation-java",
106+
VolumeSource: corev1.VolumeSource{
107+
EmptyDir: &corev1.EmptyDirVolumeSource{
108+
SizeLimit: &defaultVolumeLimitSize,
109+
},
110+
},
111+
},
112+
},
113+
InitContainers: []corev1.Container{
114+
{
115+
Name: "opentelemetry-auto-instrumentation-java",
116+
Image: "foo/bar:1",
117+
Command: []string{"cp", "/javaagent.jar", "/otel-auto-instrumentation-java/javaagent.jar"},
118+
VolumeMounts: []corev1.VolumeMount{{
119+
Name: "opentelemetry-auto-instrumentation-java",
120+
MountPath: "/otel-auto-instrumentation-java",
121+
}},
122+
},
123+
{
124+
Name: "opentelemetry-auto-instrumentation-extension-0",
125+
Image: "ex/ex:0",
126+
Command: []string{"cp", "-r", "/ex0/.", "/otel-auto-instrumentation-java/extensions"},
127+
VolumeMounts: []corev1.VolumeMount{{
128+
Name: "opentelemetry-auto-instrumentation-java",
129+
MountPath: "/otel-auto-instrumentation-java",
130+
}},
131+
},
132+
{
133+
Name: "opentelemetry-auto-instrumentation-extension-1",
134+
Image: "ex/ex:1",
135+
Command: []string{"cp", "-r", "/ex1/.", "/otel-auto-instrumentation-java/extensions"},
136+
VolumeMounts: []corev1.VolumeMount{{
137+
Name: "opentelemetry-auto-instrumentation-java",
138+
MountPath: "/otel-auto-instrumentation-java",
139+
}},
140+
},
141+
},
142+
Containers: []corev1.Container{
143+
{
144+
VolumeMounts: []corev1.VolumeMount{
145+
{
146+
Name: "opentelemetry-auto-instrumentation-java",
147+
MountPath: "/otel-auto-instrumentation-java",
148+
},
149+
},
150+
Env: []corev1.EnvVar{
151+
{
152+
Name: "JAVA_TOOL_OPTIONS",
153+
Value: javaAgent + " -Dotel.javaagent.extensions=/otel-auto-instrumentation-java/extensions",
80154
},
81155
},
82156
},
@@ -137,7 +211,7 @@ func TestInjectJavaagent(t *testing.T) {
137211
Env: []corev1.EnvVar{
138212
{
139213
Name: "JAVA_TOOL_OPTIONS",
140-
Value: "-Dbaz=bar" + javaJVMArgument,
214+
Value: "-Dbaz=bar" + javaAgent,
141215
},
142216
},
143217
},

pkg/instrumentation/podmutator_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestMutatePod(t *testing.T) {
184184
},
185185
{
186186
Name: "JAVA_TOOL_OPTIONS",
187-
Value: javaJVMArgument,
187+
Value: javaAgent,
188188
},
189189
{
190190
Name: "OTEL_TRACES_EXPORTER",
@@ -388,7 +388,7 @@ func TestMutatePod(t *testing.T) {
388388
},
389389
{
390390
Name: "JAVA_TOOL_OPTIONS",
391-
Value: javaJVMArgument,
391+
Value: javaAgent,
392392
},
393393
{
394394
Name: "OTEL_TRACES_EXPORTER",
@@ -479,7 +479,7 @@ func TestMutatePod(t *testing.T) {
479479
},
480480
{
481481
Name: "JAVA_TOOL_OPTIONS",
482-
Value: javaJVMArgument,
482+
Value: javaAgent,
483483
},
484484
{
485485
Name: "OTEL_TRACES_EXPORTER",
@@ -3821,7 +3821,7 @@ func TestMutatePod(t *testing.T) {
38213821
},
38223822
{
38233823
Name: "JAVA_TOOL_OPTIONS",
3824-
Value: javaJVMArgument,
3824+
Value: javaAgent,
38253825
},
38263826
{
38273827
Name: "OTEL_SERVICE_NAME",
@@ -3884,7 +3884,7 @@ func TestMutatePod(t *testing.T) {
38843884
},
38853885
{
38863886
Name: "JAVA_TOOL_OPTIONS",
3887-
Value: javaJVMArgument,
3887+
Value: javaAgent,
38883888
},
38893889
{
38903890
Name: "OTEL_SERVICE_NAME",
@@ -4605,7 +4605,7 @@ func TestMutatePod(t *testing.T) {
46054605
},
46064606
{
46074607
Name: "JAVA_TOOL_OPTIONS",
4608-
Value: javaJVMArgument,
4608+
Value: javaAgent,
46094609
},
46104610
{
46114611
Name: "OTEL_SERVICE_NAME",
@@ -4668,7 +4668,7 @@ func TestMutatePod(t *testing.T) {
46684668
},
46694669
{
46704670
Name: "JAVA_TOOL_OPTIONS",
4671-
Value: javaJVMArgument,
4671+
Value: javaAgent,
46724672
},
46734673
{
46744674
Name: "OTEL_SERVICE_NAME",

pkg/instrumentation/sdk_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ func TestInjectJava(t *testing.T) {
577577
},
578578
{
579579
Name: "JAVA_TOOL_OPTIONS",
580-
Value: javaJVMArgument,
580+
Value: javaAgent,
581581
},
582582
{
583583
Name: "OTEL_SERVICE_NAME",

0 commit comments

Comments
 (0)