From 13adc90a68e001a7c51ea6783a345c60d943a92c Mon Sep 17 00:00:00 2001 From: ChrsMark Date: Tue, 19 Nov 2024 23:40:17 +0200 Subject: [PATCH] make enablement hint mandatory Signed-off-by: ChrsMark --- receiver/receivercreator/README.md | 6 ++--- receiver/receivercreator/discovery.go | 3 +-- receiver/receivercreator/discovery_test.go | 30 ++++++++++++---------- receiver/receivercreator/fixtures_test.go | 1 + 4 files changed, 22 insertions(+), 18 deletions(-) diff --git a/receiver/receivercreator/README.md b/receiver/receivercreator/README.md index b6c01d4cca28a..ac836eb98e027 100644 --- a/receiver/receivercreator/README.md +++ b/receiver/receivercreator/README.md @@ -464,9 +464,7 @@ Find bellow the supported annotations that user can define to automatically enab #### Enable/disable discovery -`io.opentelemetry.discovery.metrics/enabled` (example: `"true"`) - -By default `"true"`. +`io.opentelemetry.discovery.metrics/enabled` (Required. `"true"` or `"false"`) #### Define scraper @@ -589,12 +587,14 @@ spec: app: redis annotations: # redis container port metrics hints + io.opentelemetry.discovery.metrics.6379/enabled: "true" io.opentelemetry.discovery.metrics.6379/scraper: redis io.opentelemetry.discovery.metrics.6379/config: | collection_interval: "20s" timeout: "10s" # nginx container port metrics hints + io.opentelemetry.discovery.metrics.80/enabled: "true" io.opentelemetry.discovery.metrics.80/scraper: nginx io.opentelemetry.discovery.metrics.80/config: | endpoint: "http://`endpoint`/nginx_status" diff --git a/receiver/receivercreator/discovery.go b/receiver/receivercreator/discovery.go index 956f9f089ba5c..0411f532aec5d 100644 --- a/receiver/receivercreator/discovery.go +++ b/receiver/receivercreator/discovery.go @@ -169,8 +169,7 @@ func getHintAnnotation(annotations map[string]string, hintBase string, hintKey s } func discoveryMetricsEnabled(annotations map[string]string, hintBase string, scopeSuffix string) bool { - hintVal := getHintAnnotation(annotations, hintBase, discoveryEnabledHint, scopeSuffix) - return hintVal == "true" || hintVal == "" + return getHintAnnotation(annotations, hintBase, discoveryEnabledHint, scopeSuffix) == "true" } func getStringEnv(env observer.EndpointEnv, key string) string { diff --git a/receiver/receivercreator/discovery_test.go b/receiver/receivercreator/discovery_test.go index 79129df82e6d8..dc908988b65eb 100644 --- a/receiver/receivercreator/discovery_test.go +++ b/receiver/receivercreator/discovery_test.go @@ -50,6 +50,7 @@ password: "changeme"` UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", otelMetricsHints + "/config": config, }}, @@ -74,6 +75,7 @@ password: "changeme"` UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", otelMetricsHints + "/config": config, }}, @@ -93,6 +95,7 @@ password: "changeme"` UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", }}, Port: 6379}, @@ -116,6 +119,7 @@ password: "changeme"` UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + ".6379/enabled": "true", otelMetricsHints + ".6379/scraper": "redis", otelMetricsHints + ".6379/config": config, }}, @@ -140,6 +144,7 @@ password: "changeme"` UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + ".6379/enabled": "true", otelMetricsHints + ".6379/scraper": "redis", otelMetricsHints + "/config": config, otelMetricsHints + ".6379/config": configRedis, @@ -165,6 +170,7 @@ password: "changeme"` UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", otelMetricsHints + "/config": config, }}}, @@ -218,7 +224,8 @@ nested_example: scopeSuffix string }{"simple_annotation_case": { hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/config": config, + "io.opentelemetry.discovery.metrics/enabled": "true", + "io.opentelemetry.discovery.metrics/config": config, }, expectedConf: userConfigMap{ "collection_interval": "20s", "endpoint": "0.0.0.0:8080", @@ -229,7 +236,8 @@ nested_example: scopeSuffix: "", }, "simple_annotation_case_default_endpoint": { hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/config": configNoEndpoint, + "io.opentelemetry.discovery.metrics/enabled": "true", + "io.opentelemetry.discovery.metrics/config": configNoEndpoint, }, expectedConf: userConfigMap{ "collection_interval": "20s", "endpoint": "1.1.1.1:8080", @@ -240,7 +248,8 @@ nested_example: scopeSuffix: "", }, "simple_annotation_case_scoped": { hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics.8080/config": config, + "io.opentelemetry.discovery.metrics.8080/enabled": "true", + "io.opentelemetry.discovery.metrics.8080/config": config, }, expectedConf: userConfigMap{ "collection_interval": "20s", "endpoint": "0.0.0.0:8080", @@ -251,7 +260,8 @@ nested_example: scopeSuffix: "8080", }, "simple_annotation_case_with_invalid_endpoint": { hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/config": config, + "io.opentelemetry.discovery.metrics/enabled": "true", + "io.opentelemetry.discovery.metrics/config": config, }, expectedConf: userConfigMap{}, defaultEndpoint: "1.2.3.4:8080", scopeSuffix: "", @@ -291,19 +301,13 @@ endpoint: "0.0.0.0:8080"` }, expected: false, scopeSuffix: "", - }, "test_default": { - hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/config": config, - }, - expected: true, - scopeSuffix: "", }, "test_enabled_scope": { hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/config": config, - "io.opentelemetry.discovery.metrics8080/enabled": "true", + "io.opentelemetry.discovery.metrics/config": config, + "io.opentelemetry.discovery.metrics.8080/enabled": "true", }, expected: true, - scopeSuffix: "some", + scopeSuffix: "8080", }, "test_disabled_scoped": { hintsAnn: map[string]string{ "io.opentelemetry.discovery.metrics/config": config, diff --git a/receiver/receivercreator/fixtures_test.go b/receiver/receivercreator/fixtures_test.go index f0dda32ebbaf4..b17c60604ed29 100644 --- a/receiver/receivercreator/fixtures_test.go +++ b/receiver/receivercreator/fixtures_test.go @@ -69,6 +69,7 @@ var portEndpointWithHints = observer.Endpoint{ UID: "pod-2-UID", Labels: map[string]string{"env": "prod"}, Annotations: map[string]string{ + otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "with_endpoint", otelMetricsHints + "/config": config, }},