-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JAVA_OPTS and JAVA_TOOL_OPTIONS to envoverwriter (#1133)
Resolves #1130 --------- Co-authored-by: Amir Blum <[email protected]>
- Loading branch information
Showing
18 changed files
with
521 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,150 @@ | ||
package envOverwrite | ||
|
||
import "strings" | ||
import ( | ||
"strings" | ||
|
||
// EnvValues is a map of environment variables odigos uses for various languages and goals. | ||
"github.com/odigos-io/odigos/common" | ||
) | ||
|
||
type envValues struct { | ||
delim string | ||
values map[common.OtelSdk]string | ||
} | ||
// EnvValuesMap is a map of environment variables odigos uses for various languages and goals. | ||
// The key is the environment variable name and the value is the value to be set or appended | ||
// to the environment variable. We need to make sure that in case any of these environment | ||
// variables is already set, we append the value to it instead of overwriting it. | ||
var EnvValues = map[string]struct { | ||
Value string | ||
Delim string | ||
}{ | ||
// | ||
// Note: The values here needs to be in sync with the paths used in the odigos images. | ||
// If the paths are changed in the odigos images, the values here should be updated accordingly. | ||
var EnvValuesMap = map[string]envValues{ | ||
"NODE_OPTIONS": { | ||
Value: "--require /var/odigos/nodejs/autoinstrumentation.js", | ||
Delim: " ", | ||
delim: " ", | ||
values: map[common.OtelSdk]string{ | ||
common.OtelSdkNativeCommunity: "--require /var/odigos/nodejs/autoinstrumentation.js", | ||
common.OtelSdkEbpfEnterprise: "--require /var/odigos/nodejs-ebpf/autoinstrumentation.js", | ||
}, | ||
}, | ||
"PYTHONPATH": { | ||
Value: "/var/odigos/python/opentelemetry/instrumentation/auto_instrumentation:/var/odigos/python", | ||
Delim: ":", | ||
delim: ":", | ||
values: map[common.OtelSdk]string{ | ||
common.OtelSdkNativeCommunity: "/var/odigos/python:/var/odigos/python/opentelemetry/instrumentation/auto_instrumentation", | ||
common.OtelSdkEbpfEnterprise: "/var/odigos/python-ebpf:/var/odigos/python/opentelemetry/instrumentation/auto_instrumentation:/var/odigos/python", | ||
}, | ||
}, | ||
"JAVA_OPTS": { | ||
delim: " ", | ||
values: map[common.OtelSdk]string{ | ||
common.OtelSdkNativeCommunity: "-javaagent:/var/odigos/java/javaagent.jar", | ||
common.OtelSdkEbpfEnterprise: "-javaagent:/var/odigos/java-ebpf/dtrace-injector.jar", | ||
common.OtelSdkNativeEnterprise: "-javaagent:/var/odigos/java-ext-ebpf/javaagent.jar " + | ||
"-Dotel.javaagent.extensions=/var/odigos/java-ext-ebpf/otel_agent_extension.jar", | ||
}, | ||
}, | ||
"JAVA_TOOL_OPTIONS": { | ||
delim: " ", | ||
values: map[common.OtelSdk]string{ | ||
common.OtelSdkNativeCommunity: "-javaagent:/var/odigos/java/javaagent.jar", | ||
common.OtelSdkEbpfEnterprise: "-javaagent:/var/odigos/java-ebpf/dtrace-injector.jar", | ||
common.OtelSdkNativeEnterprise: "-javaagent:/var/odigos/java-ext-ebpf/javaagent.jar " + | ||
"-Dotel.javaagent.extensions=/var/odigos/java-ext-ebpf/otel_agent_extension.jar", | ||
}, | ||
}, | ||
} | ||
|
||
func ShouldPatch(envName string, observedValue string) bool { | ||
odigosEnvValue, ok := EnvValues[envName] | ||
func ShouldPatch(envName string, observedValue string, sdk common.OtelSdk) bool { | ||
env, ok := EnvValuesMap[envName] | ||
if !ok { | ||
// Odigos does not manipulate this environment variable, so ignore it | ||
return false | ||
} | ||
|
||
if odigosEnvValue.Value == observedValue { | ||
// if the observed value is the same as the value odigos sets, | ||
// that means the user does not add any additional values, | ||
// so we should not add it to the deployment manifest | ||
desiredValue, ok := env.values[sdk] | ||
if !ok { | ||
// No specific overwrite is required for this SDK | ||
return false | ||
} | ||
|
||
if strings.Contains(observedValue, desiredValue) { | ||
// if the observed value contains the value odigos sets for this SDK, | ||
// that means that either: | ||
// 1. the user does not add any additional values and we see here our own value only, | ||
// 2. we already patched the value in the deployment manifest and we see here the patched value, | ||
// so we should not patch it | ||
return false | ||
} | ||
|
||
// if we are moving from one SDK to another, avoid patching the value | ||
// it there is no user defined value (observedValue is equal to the value odigos sets for the previous SDK) | ||
for _, v := range env.values { | ||
if v == observedValue { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func ShouldRevert(envName string, value string) bool { | ||
valToAppend, ok := EnvValues[envName] | ||
func ShouldRevert(envName string, observedValue string) bool { | ||
env, ok := EnvValuesMap[envName] | ||
if !ok { | ||
// We don't care about this environment variable | ||
return false | ||
} | ||
|
||
if !strings.Contains(value, valToAppend.Value) { | ||
// The environment variable is not patched | ||
return false | ||
// If we find any of the values odigos sets for any SDK in the observed value, | ||
// that means that the environment variable is patched by odigos and we need to revert it | ||
for _, val := range env.values { | ||
if strings.Contains(observedValue, val) && observedValue != val { | ||
return true | ||
} | ||
} | ||
return true | ||
|
||
return false | ||
} | ||
|
||
func Patch(envName string, currentVal string) string { | ||
env, exists := EnvValues[envName] | ||
func Patch(envName string, currentVal string, sdk common.OtelSdk) string { | ||
env, exists := EnvValuesMap[envName] | ||
if !exists { | ||
return "" | ||
} | ||
|
||
valToAppend, ok := env.values[sdk] | ||
if !ok { | ||
return "" | ||
} | ||
|
||
if currentVal == "" { | ||
return env.Value | ||
return valToAppend | ||
} | ||
|
||
if strings.Contains(currentVal, env.Value) { | ||
// The environment variable is already patched | ||
if strings.Contains(currentVal, valToAppend) { | ||
// The environment variable is already patched with the correct value for this SDK | ||
return currentVal | ||
} | ||
|
||
return currentVal + env.Delim + env.Value | ||
for sdkKey, val := range env.values { | ||
if sdkKey != sdk && strings.Contains(currentVal, val){ | ||
// The environment variable is patched by another SDK | ||
// but we need to append our value to it, so replace the | ||
// value of the other SDK with ours | ||
return strings.ReplaceAll(currentVal, val, valToAppend) | ||
} | ||
} | ||
|
||
return currentVal + env.delim + valToAppend | ||
} | ||
|
||
func ValToAppend(envName string, sdk common.OtelSdk) (string, bool) { | ||
env, exists := EnvValuesMap[envName] | ||
if !exists { | ||
return "", false | ||
} | ||
|
||
valToAppend, ok := env.values[sdk] | ||
if !ok { | ||
return "", false | ||
} | ||
|
||
return valToAppend, true | ||
} |
Oops, something went wrong.