From af443783ca839d44bbd537cd5853411cf44a73fd Mon Sep 17 00:00:00 2001 From: Musa Date: Mon, 9 Sep 2024 15:15:28 -0400 Subject: [PATCH] Support configurable resources for NodeJS. (#225) --- .../validate_annotations_deployment_test.go | 2 +- main.go | 6 +-- pkg/instrumentation/defaultinstrumentation.go | 5 ++ .../defaultinstrumentation_test.go | 48 +++++++++++++++++++ 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/integration-tests/manifests/annotations/validate_annotations_deployment_test.go b/integration-tests/manifests/annotations/validate_annotations_deployment_test.go index 0fdee266..27697e61 100644 --- a/integration-tests/manifests/annotations/validate_annotations_deployment_test.go +++ b/integration-tests/manifests/annotations/validate_annotations_deployment_test.go @@ -95,7 +95,7 @@ func TestJavaOnlyDeployment(t *testing.T) { updateTheOperator(t, clientSet, string(jsonStr)) if err := checkResourceAnnotations(t, clientSet, "deployment", uniqueNamespace, deploymentName, sampleDeploymentYamlNameRelPath, startTime, []string{injectJavaAnnotation, autoAnnotateJavaAnnotation}, false); err != nil { - t.Fatalf("Failed annotation check: %s", err.Error()) + t.Fatalf("Failed annotation check: %s", err.Error()) } } diff --git a/main.go b/main.go index e21f5e70..1205201c 100644 --- a/main.go +++ b/main.go @@ -143,7 +143,7 @@ func main() { pflag.Parse() // set instrumentation cpu and memory limits in environment variables to be used for default instrumentation; default values received from https://github.com/open-telemetry/opentelemetry-operator/blob/main/apis/v1alpha1/instrumentation_webhook.go - autoInstrumentationConfig := map[string]map[string]map[string]string{"java": {"limits": {"cpu": "500m", "memory": "64Mi"}, "requests": {"cpu": "50m", "memory": "64Mi"}}, "python": {"limits": {"cpu": "500m", "memory": "32Mi"}, "requests": {"cpu": "50m", "memory": "32Mi"}}, "dotnet": {"limits": {"cpu": "500m", "memory": "128Mi"}, "requests": {"cpu": "50m", "memory": "128Mi"}}} + autoInstrumentationConfig := map[string]map[string]map[string]string{"java": {"limits": {"cpu": "500m", "memory": "64Mi"}, "requests": {"cpu": "50m", "memory": "64Mi"}}, "python": {"limits": {"cpu": "500m", "memory": "32Mi"}, "requests": {"cpu": "50m", "memory": "32Mi"}}, "dotnet": {"limits": {"cpu": "500m", "memory": "128Mi"}, "requests": {"cpu": "50m", "memory": "128Mi"}}, "nodejs": {"limits": {"cpu": "500m", "memory": "128Mi"}, "requests": {"cpu": "50m", "memory": "128Mi"}}} err := json.Unmarshal([]byte(autoInstrumentationConfigStr), &autoInstrumentationConfig) if err != nil { setupLog.Info(fmt.Sprintf("Using default values: %v", autoInstrumentationConfig)) @@ -157,8 +157,8 @@ func main() { if dotNetVar, ok := autoInstrumentationConfig["dotnet"]; ok { setLangEnvVars("DOTNET", dotNetVar) } - if dotNetVar, ok := autoInstrumentationConfig["nodejs"]; ok { - setLangEnvVars("NODEJS", dotNetVar) + if nodeJSVar, ok := autoInstrumentationConfig["nodejs"]; ok { + setLangEnvVars("NODEJS", nodeJSVar) } // set supported language instrumentation images in environment variable to be used for default instrumentation diff --git a/pkg/instrumentation/defaultinstrumentation.go b/pkg/instrumentation/defaultinstrumentation.go index 2f6fa918..a1685728 100644 --- a/pkg/instrumentation/defaultinstrumentation.go +++ b/pkg/instrumentation/defaultinstrumentation.go @@ -29,6 +29,7 @@ const ( java = "JAVA" python = "PYTHON" dotNet = "DOTNET" + nodeJS = "NODEJS" limit = "LIMIT" request = "REQUEST" ) @@ -173,6 +174,10 @@ func getDefaultInstrumentation(agentConfig *adapters.CwaConfig, isWindowsPod boo {Name: "OTEL_METRICS_EXPORTER", Value: "none"}, {Name: "OTEL_LOGS_EXPORTER", Value: "none"}, }, + Resources: corev1.ResourceRequirements{ + Limits: getInstrumentationConfigForResource(nodeJS, limit), + Requests: getInstrumentationConfigForResource(nodeJS, request), + }, }, }, }, nil diff --git a/pkg/instrumentation/defaultinstrumentation_test.go b/pkg/instrumentation/defaultinstrumentation_test.go index d1611f8d..bb1d920f 100644 --- a/pkg/instrumentation/defaultinstrumentation_test.go +++ b/pkg/instrumentation/defaultinstrumentation_test.go @@ -34,6 +34,10 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) { os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_LIMIT", "128Mi") os.Setenv("AUTO_INSTRUMENTATION_DOTNET_CPU_REQUEST", "50m") os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_REQUEST", "128Mi") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_CPU_LIMIT", "500m") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_MEM_LIMIT", "128Mi") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_CPU_REQUEST", "50m") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_MEM_REQUEST", "128Mi") httpInst := &v1alpha1.Instrumentation{ Status: v1alpha1.InstrumentationStatus{}, @@ -143,6 +147,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) { {Name: "OTEL_METRICS_EXPORTER", Value: "none"}, {Name: "OTEL_LOGS_EXPORTER", Value: "none"}, }, + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("500m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("50m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + }, }, }, } @@ -254,6 +268,16 @@ func Test_getDefaultInstrumentationLinux(t *testing.T) { {Name: "OTEL_METRICS_EXPORTER", Value: "none"}, {Name: "OTEL_LOGS_EXPORTER", Value: "none"}, }, + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("500m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("50m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + }, }, }, } @@ -333,6 +357,10 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) { os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_LIMIT", "128Mi") os.Setenv("AUTO_INSTRUMENTATION_DOTNET_CPU_REQUEST", "50m") os.Setenv("AUTO_INSTRUMENTATION_DOTNET_MEM_REQUEST", "128Mi") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_CPU_LIMIT", "500m") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_MEM_LIMIT", "128Mi") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_CPU_REQUEST", "50m") + os.Setenv("AUTO_INSTRUMENTATION_NODEJS_MEM_REQUEST", "128Mi") httpInst := &v1alpha1.Instrumentation{ Status: v1alpha1.InstrumentationStatus{}, @@ -442,6 +470,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) { {Name: "OTEL_METRICS_EXPORTER", Value: "none"}, {Name: "OTEL_LOGS_EXPORTER", Value: "none"}, }, + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("500m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("50m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + }, }, }, } @@ -553,6 +591,16 @@ func Test_getDefaultInstrumentationWindows(t *testing.T) { {Name: "OTEL_METRICS_EXPORTER", Value: "none"}, {Name: "OTEL_LOGS_EXPORTER", Value: "none"}, }, + Resources: corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("500m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + Requests: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("50m"), + corev1.ResourceMemory: resource.MustParse("128Mi"), + }, + }, }, }, }