Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instrumentations support Java extensions #2761

Merged
merged 8 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/support-extensions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: auto-instrumentation

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Support Java auto-instrumentation extensions.

# One or more tracking issues related to the change
issues: [1785]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
13 changes: 13 additions & 0 deletions apis/v1alpha1/instrumentation_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ type Java struct {
// Resources describes the compute resource requirements.
// +optional
Resources corev1.ResourceRequirements `json:"resources,omitempty"`

// Extensions defines java specific extensions.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@crossoverJie all extensions are copied to a single directory, therefore we should document it because one extension can override another one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added related documents.

// All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.
// +optional
Extensions []Extensions `json:"extensions,omitempty"`
}

type Extensions struct {
// Image is a container image with extensions auto-instrumentation JAR.
Image string `json:"image"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the extension needs to be rebuild for every java agent version?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary; the user just needs to package based on the javaagent version he's using.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some open source extensions, maybe it should provide a compatibility list for javaagent.


// Dir is a directory with extensions auto-instrumentation JAR.
Dir string `json:"dir"`
}

// NodeJS defines NodeJS SDK and instrumentation configuration.
Expand Down
20 changes: 20 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions bundle/manifests/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,25 @@ spec:
- name
type: object
type: array
extensions:
description: |-
Extensions defines java specific extensions.
All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.
items:
properties:
dir:
description: Dir is a directory with extensions auto-instrumentation
JAR.
type: string
image:
description: Image is a container image with extensions
auto-instrumentation JAR.
type: string
required:
- dir
- image
type: object
type: array
image:
description: Image is a container image with javaagent auto-instrumentation
JAR.
Expand Down
19 changes: 19 additions & 0 deletions config/crd/bases/opentelemetry.io_instrumentations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,25 @@ spec:
- name
type: object
type: array
extensions:
description: |-
Extensions defines java specific extensions.
All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.
items:
properties:
dir:
description: Dir is a directory with extensions auto-instrumentation
JAR.
type: string
image:
description: Image is a container image with extensions
auto-instrumentation JAR.
type: string
required:
- dir
- image
type: object
type: array
image:
description: Image is a container image with javaagent auto-instrumentation
JAR.
Expand Down
42 changes: 42 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,14 @@ Java defines configuration for java auto-instrumentation.
Env defines java specific env vars.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b><a href="#instrumentationspecjavaextensionsindex">extensions</a></b></td>
<td>[]object</td>
<td>
Extensions defines java specific extensions.
All extensions are copied to a single directory; if a JAR with the same name exists, it will be overwritten.<br/>
</td>
<td>false</td>
</tr><tr>
<td><b>image</b></td>
<td>string</td>
Expand Down Expand Up @@ -2188,6 +2196,40 @@ TODO: Add other useful fields. apiVersion, kind, uid?<br/>
</table>


### Instrumentation.spec.java.extensions[index]
<sup><sup>[↩ Parent](#instrumentationspecjava)</sup></sup>





<table>
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Description</th>
<th>Required</th>
</tr>
</thead>
<tbody><tr>
<td><b>dir</b></td>
<td>string</td>
<td>
Dir is a directory with extensions auto-instrumentation JAR.<br/>
</td>
<td>true</td>
</tr><tr>
<td><b>image</b></td>
<td>string</td>
<td>
Image is a container image with extensions auto-instrumentation JAR.<br/>
</td>
<td>true</td>
</tr></tbody>
</table>


### Instrumentation.spec.java.resources
<sup><sup>[↩ Parent](#instrumentationspecjava)</sup></sup>

Expand Down
23 changes: 22 additions & 1 deletion pkg/instrumentation/javaagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
package instrumentation

import (
"fmt"

corev1 "k8s.io/api/core/v1"

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

const (
envJavaToolsOptions = "JAVA_TOOL_OPTIONS"
javaJVMArgument = " -javaagent:/otel-auto-instrumentation-java/javaagent.jar"
javaAgent = " -javaagent:/otel-auto-instrumentation-java/javaagent.jar"
javaInitContainerName = initContainerName + "-java"
javaVolumeName = volumeName + "-java"
javaInstrMountPath = "/otel-auto-instrumentation-java"
Expand All @@ -45,6 +47,11 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
}
}

javaJVMArgument := javaAgent
if len(javaSpec.Extensions) > 0 {
javaJVMArgument = javaAgent + fmt.Sprintf(" -Dotel.javaagent.extensions=%s/extensions", javaInstrMountPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://opentelemetry.io/docs/languages/java/automatic/configuration/#extensions

If all extensions are copied to the same directory it can cause overrides.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I'm not sure what the confusion is here.

I think the user should make sure that the jars in the /extensions directory are unique, even if he doesn't use the operator.

}

idx := getIndexOfEnv(container.Env, envJavaToolsOptions)
if idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Expand Down Expand Up @@ -80,6 +87,20 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
MountPath: javaInstrMountPath,
}},
})

for i, extension := range javaSpec.Extensions {
pod.Spec.InitContainers = append(pod.Spec.InitContainers, corev1.Container{
Name: initContainerName + fmt.Sprintf("-extension-%d", i),
Image: extension.Image,
Command: []string{"cp", "-r", extension.Dir + "/.", javaInstrMountPath + "/extensions"},
Resources: javaSpec.Resources,
VolumeMounts: []corev1.VolumeMount{{
Name: javaVolumeName,
MountPath: javaInstrMountPath,
}},
})
}

}
return pod, err
}
78 changes: 76 additions & 2 deletions pkg/instrumentation/javaagent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,81 @@ func TestInjectJavaagent(t *testing.T) {
Env: []corev1.EnvVar{
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
},
},
},
},
},
err: nil,
},
{
name: "add extensions to JAVA_TOOL_OPTIONS",
Java: v1alpha1.Java{Image: "foo/bar:1", Extensions: []v1alpha1.Extensions{
{Image: "ex/ex:0", Dir: "/ex0"},
{Image: "ex/ex:1", Dir: "/ex1"},
}},
pod: corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{},
},
},
},
expected: corev1.Pod{
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "opentelemetry-auto-instrumentation-java",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
SizeLimit: &defaultVolumeLimitSize,
},
},
},
},
InitContainers: []corev1.Container{
{
Name: "opentelemetry-auto-instrumentation-java",
Image: "foo/bar:1",
Command: []string{"cp", "/javaagent.jar", "/otel-auto-instrumentation-java/javaagent.jar"},
VolumeMounts: []corev1.VolumeMount{{
Name: "opentelemetry-auto-instrumentation-java",
MountPath: "/otel-auto-instrumentation-java",
}},
},
{
Name: "opentelemetry-auto-instrumentation-extension-0",
Image: "ex/ex:0",
Command: []string{"cp", "-r", "/ex0/.", "/otel-auto-instrumentation-java/extensions"},
VolumeMounts: []corev1.VolumeMount{{
Name: "opentelemetry-auto-instrumentation-java",
MountPath: "/otel-auto-instrumentation-java",
}},
},
{
Name: "opentelemetry-auto-instrumentation-extension-1",
Image: "ex/ex:1",
Command: []string{"cp", "-r", "/ex1/.", "/otel-auto-instrumentation-java/extensions"},
VolumeMounts: []corev1.VolumeMount{{
Name: "opentelemetry-auto-instrumentation-java",
MountPath: "/otel-auto-instrumentation-java",
}},
},
},
Containers: []corev1.Container{
{
VolumeMounts: []corev1.VolumeMount{
{
Name: "opentelemetry-auto-instrumentation-java",
MountPath: "/otel-auto-instrumentation-java",
},
},
Env: []corev1.EnvVar{
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaAgent + " -Dotel.javaagent.extensions=/otel-auto-instrumentation-java/extensions",
},
},
},
Expand Down Expand Up @@ -137,7 +211,7 @@ func TestInjectJavaagent(t *testing.T) {
Env: []corev1.EnvVar{
{
Name: "JAVA_TOOL_OPTIONS",
Value: "-Dbaz=bar" + javaJVMArgument,
Value: "-Dbaz=bar" + javaAgent,
},
},
},
Expand Down
14 changes: 7 additions & 7 deletions pkg/instrumentation/podmutator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_TRACES_EXPORTER",
Expand Down Expand Up @@ -388,7 +388,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_TRACES_EXPORTER",
Expand Down Expand Up @@ -479,7 +479,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_TRACES_EXPORTER",
Expand Down Expand Up @@ -3821,7 +3821,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_SERVICE_NAME",
Expand Down Expand Up @@ -3884,7 +3884,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_SERVICE_NAME",
Expand Down Expand Up @@ -4605,7 +4605,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_SERVICE_NAME",
Expand Down Expand Up @@ -4668,7 +4668,7 @@ func TestMutatePod(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_SERVICE_NAME",
Expand Down
2 changes: 1 addition & 1 deletion pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func TestInjectJava(t *testing.T) {
},
{
Name: "JAVA_TOOL_OPTIONS",
Value: javaJVMArgument,
Value: javaAgent,
},
{
Name: "OTEL_SERVICE_NAME",
Expand Down
Loading