Skip to content

Commit

Permalink
Add JAVA_OPTS and JAVA_TOOL_OPTIONS to envoverwriter (#1133)
Browse files Browse the repository at this point in the history
Resolves #1130

---------

Co-authored-by: Amir Blum <amirgiraffe@gmail.com>
  • Loading branch information
RonFed and blumamir committed May 26, 2024
1 parent 7a2aa4d commit f8b3686
Show file tree
Hide file tree
Showing 18 changed files with 521 additions and 178 deletions.
102 changes: 32 additions & 70 deletions cli/cmd/resources/odigosconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,93 +16,55 @@ const (
)

func otelSdkConfigCommunity() (map[common.ProgrammingLanguage]common.OtelSdk, map[common.ProgrammingLanguage][]common.OtelSdk) {

nativeCommunity := common.OtelSdk{
SdkType: common.NativeOtelSdkType,
SdkTier: common.CommunityOtelSdkTier,
}

eBPFCommunity := common.OtelSdk{
SdkType: common.EbpfOtelSdkType,
SdkTier: common.CommunityOtelSdkTier,
}

return map[common.ProgrammingLanguage]common.OtelSdk{
common.JavaProgrammingLanguage: nativeCommunity,
common.PythonProgrammingLanguage: nativeCommunity,
common.GoProgrammingLanguage: eBPFCommunity,
common.DotNetProgrammingLanguage: nativeCommunity,
common.JavascriptProgrammingLanguage: nativeCommunity,
common.JavaProgrammingLanguage: common.OtelSdkNativeCommunity,
common.PythonProgrammingLanguage: common.OtelSdkNativeCommunity,
common.GoProgrammingLanguage: common.OtelSdkEbpfCommunity,
common.DotNetProgrammingLanguage: common.OtelSdkNativeCommunity,
common.JavascriptProgrammingLanguage: common.OtelSdkNativeCommunity,
},
map[common.ProgrammingLanguage][]common.OtelSdk{
common.JavaProgrammingLanguage: {nativeCommunity},
common.PythonProgrammingLanguage: {nativeCommunity},
common.GoProgrammingLanguage: {eBPFCommunity},
common.DotNetProgrammingLanguage: {nativeCommunity},
common.JavascriptProgrammingLanguage: {nativeCommunity},
common.JavaProgrammingLanguage: {common.OtelSdkNativeCommunity},
common.PythonProgrammingLanguage: {common.OtelSdkNativeCommunity},
common.GoProgrammingLanguage: {common.OtelSdkEbpfCommunity},
common.DotNetProgrammingLanguage: {common.OtelSdkNativeCommunity},
common.JavascriptProgrammingLanguage: {common.OtelSdkNativeCommunity},
}
}

func otelSdkConfigCloud() (map[common.ProgrammingLanguage]common.OtelSdk, map[common.ProgrammingLanguage][]common.OtelSdk) {

nativeCommunity := common.OtelSdk{
SdkType: common.NativeOtelSdkType,
SdkTier: common.CommunityOtelSdkTier,
}

eBPFEnterprise := common.OtelSdk{
SdkType: common.EbpfOtelSdkType,
SdkTier: common.EnterpriseOtelSdkTier,
}

return map[common.ProgrammingLanguage]common.OtelSdk{
common.JavaProgrammingLanguage: nativeCommunity,
common.PythonProgrammingLanguage: nativeCommunity,
common.GoProgrammingLanguage: eBPFEnterprise,
common.DotNetProgrammingLanguage: nativeCommunity,
common.JavascriptProgrammingLanguage: nativeCommunity,
common.JavaProgrammingLanguage: common.OtelSdkNativeCommunity,
common.PythonProgrammingLanguage: common.OtelSdkNativeCommunity,
common.GoProgrammingLanguage: common.OtelSdkEbpfEnterprise,
common.DotNetProgrammingLanguage: common.OtelSdkNativeCommunity,
common.JavascriptProgrammingLanguage: common.OtelSdkNativeCommunity,
},
map[common.ProgrammingLanguage][]common.OtelSdk{
common.JavaProgrammingLanguage: {nativeCommunity, eBPFEnterprise},
common.PythonProgrammingLanguage: {nativeCommunity, eBPFEnterprise},
common.GoProgrammingLanguage: {eBPFEnterprise},
common.DotNetProgrammingLanguage: {nativeCommunity},
common.JavascriptProgrammingLanguage: {nativeCommunity, eBPFEnterprise},
common.JavaProgrammingLanguage: {common.OtelSdkNativeCommunity, common.OtelSdkEbpfEnterprise},
common.PythonProgrammingLanguage: {common.OtelSdkNativeCommunity, common.OtelSdkEbpfEnterprise},
common.GoProgrammingLanguage: {common.OtelSdkEbpfEnterprise},
common.DotNetProgrammingLanguage: {common.OtelSdkNativeCommunity},
common.JavascriptProgrammingLanguage: {common.OtelSdkNativeCommunity, common.OtelSdkEbpfEnterprise},
}
}

func otelSdkConfigOnPrem() (map[common.ProgrammingLanguage]common.OtelSdk, map[common.ProgrammingLanguage][]common.OtelSdk) {

nativeCommunity := common.OtelSdk{
SdkType: common.NativeOtelSdkType,
SdkTier: common.CommunityOtelSdkTier,
}

eBPFEnterprise := common.OtelSdk{
SdkType: common.EbpfOtelSdkType,
SdkTier: common.EnterpriseOtelSdkTier,
}

nativeEnterprise := common.OtelSdk{
SdkType: common.NativeOtelSdkType,
SdkTier: common.EnterpriseOtelSdkTier,
}

return map[common.ProgrammingLanguage]common.OtelSdk{
common.JavaProgrammingLanguage: eBPFEnterprise, // Notice - for onprem, the default for java is eBPF
common.PythonProgrammingLanguage: eBPFEnterprise, // Also Python
common.GoProgrammingLanguage: eBPFEnterprise,
common.DotNetProgrammingLanguage: nativeCommunity,
common.JavascriptProgrammingLanguage: eBPFEnterprise, // Also Javascript
common.MySQLProgrammingLanguage: eBPFEnterprise,
common.JavaProgrammingLanguage: common.OtelSdkEbpfEnterprise, // Notice - for onprem, the default for java is eBPF
common.PythonProgrammingLanguage: common.OtelSdkEbpfEnterprise, // Also Python
common.GoProgrammingLanguage: common.OtelSdkEbpfEnterprise,
common.DotNetProgrammingLanguage: common.OtelSdkNativeCommunity,
common.JavascriptProgrammingLanguage: common.OtelSdkEbpfEnterprise, // Also Javascript
common.MySQLProgrammingLanguage: common.OtelSdkEbpfEnterprise,
},
map[common.ProgrammingLanguage][]common.OtelSdk{
common.JavaProgrammingLanguage: {nativeCommunity, eBPFEnterprise, nativeEnterprise},
common.PythonProgrammingLanguage: {nativeCommunity, eBPFEnterprise},
common.GoProgrammingLanguage: {eBPFEnterprise},
common.DotNetProgrammingLanguage: {nativeCommunity},
common.JavascriptProgrammingLanguage: {nativeCommunity, eBPFEnterprise},
common.MySQLProgrammingLanguage: {eBPFEnterprise},
common.JavaProgrammingLanguage: {common.OtelSdkNativeCommunity, common.OtelSdkEbpfEnterprise, common.OtelSdkNativeEnterprise},
common.PythonProgrammingLanguage: {common.OtelSdkNativeCommunity, common.OtelSdkEbpfEnterprise},
common.GoProgrammingLanguage: {common.OtelSdkEbpfEnterprise},
common.DotNetProgrammingLanguage: {common.OtelSdkNativeCommunity},
common.JavascriptProgrammingLanguage: {common.OtelSdkNativeCommunity, common.OtelSdkEbpfEnterprise},
common.MySQLProgrammingLanguage: {common.OtelSdkEbpfEnterprise},
}
}

Expand Down
135 changes: 107 additions & 28 deletions common/envOverwrite/overwriter.go
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
}
Loading

0 comments on commit f8b3686

Please sign in to comment.