Skip to content

[chore] simplify code by using a utility method #3668

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

Merged
merged 2 commits into from
Feb 19, 2025
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
7 changes: 1 addition & 6 deletions pkg/instrumentation/apachehttpd.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,7 @@ func injectApacheHttpdagent(_ logr.Logger, apacheSpec v1alpha1.ApacheHttpd, pod
container := &pod.Spec.Containers[index]

// inject env vars
for _, env := range apacheSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
container.Env = appendIfNotSet(container.Env, apacheSpec.Env...)

// First make a clone of the instrumented container to take the existing Apache configuration from
// and create init container from it
Expand Down
7 changes: 1 addition & 6 deletions pkg/instrumentation/dotnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,7 @@ func injectDotNetSDK(dotNetSpec v1alpha1.DotNet, pod corev1.Pod, index int, runt
}

// inject .NET instrumentation spec env vars.
for _, env := range dotNetSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
container.Env = appendIfNotSet(container.Env, dotNetSpec.Env...)

const (
doNotConcatEnvValues = false
Expand Down
40 changes: 16 additions & 24 deletions pkg/instrumentation/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ import (

func configureExporter(exporter v1alpha1.Exporter, pod *corev1.Pod, container *corev1.Container) {
if exporter.Endpoint != "" {
if getIndexOfEnv(container.Env, constants.EnvOTELExporterOTLPEndpoint) == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterOTLPEndpoint,
Value: exporter.Endpoint,
})
}
container.Env = appendIfNotSet(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterOTLPEndpoint,
Value: exporter.Endpoint,
})
}
if exporter.TLS == nil {
return
Expand All @@ -52,36 +50,30 @@ func configureExporter(exporter v1alpha1.Exporter, pod *corev1.Pod, container *c
if filepath.IsAbs(exporter.TLS.CA) {
envVarVal = exporter.TLS.CA
}
if getIndexOfEnv(container.Env, constants.EnvOTELExporterCertificate) == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterCertificate,
Value: envVarVal,
})
}
container.Env = appendIfNotSet(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterCertificate,
Value: envVarVal,
})
}
if exporter.TLS.Cert != "" {
envVarVal := fmt.Sprintf("%s/%s", secretMountPath, exporter.TLS.Cert)
if filepath.IsAbs(exporter.TLS.Cert) {
envVarVal = exporter.TLS.Cert
}
if getIndexOfEnv(container.Env, constants.EnvOTELExporterClientCertificate) == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterClientCertificate,
Value: envVarVal,
})
}
container.Env = appendIfNotSet(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterClientCertificate,
Value: envVarVal,
})
}
if exporter.TLS.Key != "" {
envVarVar := fmt.Sprintf("%s/%s", secretMountPath, exporter.TLS.Key)
if filepath.IsAbs(exporter.TLS.Key) {
envVarVar = exporter.TLS.Key
}
if getIndexOfEnv(container.Env, constants.EnvOTELExporterClientKey) == -1 {
container.Env = append(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterClientKey,
Value: envVarVar,
})
}
container.Env = appendIfNotSet(container.Env, corev1.EnvVar{
Name: constants.EnvOTELExporterClientKey,
Value: envVarVar,
})
}

if exporter.TLS.SecretName != "" {
Expand Down
7 changes: 1 addition & 6 deletions pkg/instrumentation/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,7 @@ func injectGoSDK(goSpec v1alpha1.Go, pod corev1.Pod, cfg config.Config) (corev1.

// Inject Go instrumentation spec env vars.
// For Go, env vars must be added to the agent contain
for _, env := range goSpec.Env {
idx := getIndexOfEnv(goAgent.Env, env.Name)
if idx == -1 {
goAgent.Env = append(goAgent.Env, env)
}
}
goAgent.Env = appendIfNotSet(goAgent.Env, goSpec.Env...)

pod.Spec.Containers = append(pod.Spec.Containers, goAgent)
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
Expand Down
7 changes: 1 addition & 6 deletions pkg/instrumentation/javaagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ func injectJavaagent(javaSpec v1alpha1.Java, pod corev1.Pod, index int) (corev1.
}

// inject Java instrumentation spec env vars.
for _, env := range javaSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
container.Env = appendIfNotSet(container.Env, javaSpec.Env...)

javaJVMArgument := javaAgent
if len(javaSpec.Extensions) > 0 {
Expand Down
7 changes: 1 addition & 6 deletions pkg/instrumentation/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,7 @@ func injectNginxSDK(_ logr.Logger, nginxSpec v1alpha1.Nginx, pod corev1.Pod, use
container := &pod.Spec.Containers[index]

// inject env vars
for _, env := range nginxSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
container.Env = appendIfNotSet(container.Env, nginxSpec.Env...)

// First make a clone of the instrumented container to take the existing Nginx configuration from
// and create init container from it
Expand Down
7 changes: 1 addition & 6 deletions pkg/instrumentation/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,7 @@ func injectNodeJSSDK(nodeJSSpec v1alpha1.NodeJS, pod corev1.Pod, index int) (cor
}

// inject NodeJS instrumentation spec env vars.
for _, env := range nodeJSSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
container.Env = appendIfNotSet(container.Env, nodeJSSpec.Env...)

idx := getIndexOfEnv(container.Env, envNodeOptions)
if idx == -1 {
Expand Down
48 changes: 15 additions & 33 deletions pkg/instrumentation/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ func injectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int, plat
}

// inject Python instrumentation spec env vars.
for _, env := range pythonSpec.Env {
idx := getIndexOfEnv(container.Env, env.Name)
if idx == -1 {
container.Env = append(container.Env, env)
}
}
container.Env = appendIfNotSet(container.Env, pythonSpec.Env...)

idx := getIndexOfEnv(container.Env, envPythonPath)
if idx == -1 {
Expand All @@ -78,41 +73,28 @@ func injectPythonSDK(pythonSpec v1alpha1.Python, pod corev1.Pod, index int, plat
container.Env[idx].Value = fmt.Sprintf("%s:%s:%s", pythonPathPrefix, container.Env[idx].Value, pythonPathSuffix)
}

// Set OTEL_EXPORTER_OTLP_PROTOCOL to http/protobuf if not set by user because it is what our autoinstrumentation supports.
idx = getIndexOfEnv(container.Env, envOtelExporterOTLPProtocol)
if idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
container.Env = appendIfNotSet(container.Env,
// Set OTEL_EXPORTER_OTLP_PROTOCOL to http/protobuf if not set by user because it is what our autoinstrumentation supports.
corev1.EnvVar{
Name: envOtelExporterOTLPProtocol,
Value: "http/protobuf",
})
}

// Set OTEL_TRACES_EXPORTER to otlp exporter if not set by user because it is what our autoinstrumentation supports.
idx = getIndexOfEnv(container.Env, envOtelTracesExporter)
if idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
},
// Set OTEL_TRACES_EXPORTER to otlp exporter if not set by user because it is what our autoinstrumentation supports.
corev1.EnvVar{
Name: envOtelTracesExporter,
Value: "otlp",
})
}

// Set OTEL_METRICS_EXPORTER to otlp exporter if not set by user because it is what our autoinstrumentation supports.
idx = getIndexOfEnv(container.Env, envOtelMetricsExporter)
if idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
},
// Set OTEL_METRICS_EXPORTER to otlp exporter if not set by user because it is what our autoinstrumentation supports.
corev1.EnvVar{
Name: envOtelMetricsExporter,
Value: "otlp",
})
}

// Set OTEL_LOGS_EXPORTER to otlp exporter if not set by user because it is what our autoinstrumentation supports.
idx = getIndexOfEnv(container.Env, envOtelLogsExporter)
if idx == -1 {
container.Env = append(container.Env, corev1.EnvVar{
},
// Set OTEL_LOGS_EXPORTER to otlp exporter if not set by user because it is what our autoinstrumentation supports.
corev1.EnvVar{
Name: envOtelLogsExporter,
Value: "otlp",
})
}
},
)

container.VolumeMounts = append(container.VolumeMounts, corev1.VolumeMount{
Name: volume.Name,
Expand Down
14 changes: 14 additions & 0 deletions pkg/instrumentation/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,20 @@ func getIndexOfEnv(envs []corev1.EnvVar, name string) int {
return -1
}

func appendIfNotSet(envs []corev1.EnvVar, newEnvVars ...corev1.EnvVar) []corev1.EnvVar {
keys := make(map[string]struct{}, len(envs))
for _, e := range envs {
keys[e.Name] = struct{}{}
}
for _, envVar := range newEnvVars {
if _, ok := keys[envVar.Name]; !ok {
envs = append(envs, envVar)
keys[envVar.Name] = struct{}{}
}
}
return envs
}

func moveEnvToListEnd(envs []corev1.EnvVar, idx int) []corev1.EnvVar {
if idx >= 0 && idx < len(envs) {
envToMove := envs[idx]
Expand Down
82 changes: 82 additions & 0 deletions pkg/instrumentation/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2563,3 +2563,85 @@ func TestChooseServiceName(t *testing.T) {
})
}
}

func TestAppendIfNotSet(t *testing.T) {
tests := []struct {
name string
envVars []corev1.EnvVar
newVars []corev1.EnvVar
result []corev1.EnvVar
}{
{
name: "add to empty",
newVars: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
result: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
},
{
name: "do not update if set",
envVars: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
newVars: []corev1.EnvVar{
{
Name: "foo",
Value: "foobar",
},
},
result: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
},
{
name: "mixed use",
envVars: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
},
newVars: []corev1.EnvVar{
{
Name: "foo",
Value: "foobar",
},
{
Name: "alpha",
Value: "beta",
},
},
result: []corev1.EnvVar{
{
Name: "foo",
Value: "bar",
},
{
Name: "alpha",
Value: "beta",
},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := appendIfNotSet(test.envVars, test.newVars...)
assert.Equal(t, test.result, result)
})
}
}
Loading