diff --git a/receiver/receivercreator/discovery.go b/receiver/receivercreator/discovery.go index e7684043594f..69441aeef751 100644 --- a/receiver/receivercreator/discovery.go +++ b/receiver/receivercreator/discovery.go @@ -84,7 +84,8 @@ func (builder *k8sHintsBuilder) createReceiverTemplateFromHints(env observer.End func (builder *k8sHintsBuilder) createScraper( annotations map[string]string, - env observer.EndpointEnv) (*receiverTemplate, error) { + env observer.EndpointEnv, +) (*receiverTemplate, error) { var port uint16 var p observer.Port err := mapstructure.Decode(env, &p) @@ -127,7 +128,8 @@ func (builder *k8sHintsBuilder) createScraper( func getScraperConfFromAnnotations( annotations map[string]string, defaultEndpoint, scopeSuffix string, - logger *zap.Logger) (userConfigMap, error) { + logger *zap.Logger, +) (userConfigMap, error) { conf := userConfigMap{} conf[endpointConfigKey] = defaultEndpoint @@ -150,9 +152,7 @@ func getScraperConfFromAnnotations( if err != nil { logger.Debug("configured endpoint is not valid", zap.Error(err)) return userConfigMap{}, fmt.Errorf("configured endpoint is not valid: %v", zap.Error(err)) - } - return conf, nil } @@ -190,7 +190,7 @@ func getStringEnv(env observer.EndpointEnv, key string) string { func validateEndpoint(endpoint, defaultEndpoint string) error { // replace temporarily the dynamic reference to ease the url parsing - endpoint = strings.Replace(endpoint, "`endpoint`", defaultEndpoint, -1) + endpoint = strings.ReplaceAll(endpoint, "`endpoint`", defaultEndpoint) uri, _ := url.Parse(endpoint) // target endpoint can come in form ip:port. In that case we fix the uri diff --git a/receiver/receivercreator/discovery_test.go b/receiver/receivercreator/discovery_test.go index 5a74c12b1600..a0c254be4a0f 100644 --- a/receiver/receivercreator/discovery_test.go +++ b/receiver/receivercreator/discovery_test.go @@ -22,12 +22,12 @@ func TestK8sHintsBuilderMetrics(t *testing.T) { err := id.UnmarshalText([]byte("redis/pod-2-UID_6379")) assert.NoError(t, err) - var config = ` + config := ` collection_interval: "20s" timeout: "30s" username: "username" password: "changeme"` - var configRedis = ` + configRedis := ` collection_interval: "20s" timeout: "130s" username: "username" @@ -53,8 +53,10 @@ password: "changeme"` otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", otelMetricsHints + "/config": config, - }}, - Port: 6379}, + }, + }, + Port: 6379, + }, }, expectedReceiver: receiverTemplate{ receiverConfig: receiverConfig{ @@ -78,8 +80,10 @@ password: "changeme"` otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", otelMetricsHints + "/config": config, - }}, - Port: 6379}, + }, + }, + Port: 6379, + }, }, expectedReceiver: receiverTemplate{}, wantError: false, @@ -97,8 +101,10 @@ password: "changeme"` Annotations: map[string]string{ otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", - }}, - Port: 6379}, + }, + }, + Port: 6379, + }, }, expectedReceiver: receiverTemplate{ receiverConfig: receiverConfig{ @@ -122,8 +128,10 @@ password: "changeme"` otelMetricsHints + ".6379/enabled": "true", otelMetricsHints + ".6379/scraper": "redis", otelMetricsHints + ".6379/config": config, - }}, - Port: 6379}, + }, + }, + Port: 6379, + }, }, expectedReceiver: receiverTemplate{ receiverConfig: receiverConfig{ @@ -148,8 +156,10 @@ password: "changeme"` otelMetricsHints + ".6379/scraper": "redis", otelMetricsHints + "/config": config, otelMetricsHints + ".6379/config": configRedis, - }}, - Port: 6379}, + }, + }, + Port: 6379, + }, }, expectedReceiver: receiverTemplate{ receiverConfig: receiverConfig{ @@ -173,7 +183,9 @@ password: "changeme"` otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "redis", otelMetricsHints + "/config": config, - }}}, + }, + }, + }, }, expectedReceiver: receiverTemplate{}, wantError: true, @@ -204,14 +216,14 @@ password: "changeme"` } func TestGetConfFromAnnotations(t *testing.T) { - var config = ` + config := ` endpoint: "0.0.0.0:8080" collection_interval: "20s" initial_delay: "20s" read_buffer_size: "10" nested_example: foo: bar` - var configNoEndpoint = ` + configNoEndpoint := ` collection_interval: "20s" initial_delay: "20s" read_buffer_size: "10" @@ -223,51 +235,52 @@ nested_example: defaultEndpoint string scopeSuffix string expectError bool - }{"simple_annotation_case": { - hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/enabled": "true", - "io.opentelemetry.discovery.metrics/config": config, - }, expectedConf: userConfigMap{ - "collection_interval": "20s", - "endpoint": "0.0.0.0:8080", - "initial_delay": "20s", - "read_buffer_size": "10", - "nested_example": userConfigMap{"foo": "bar"}, - }, defaultEndpoint: "0.0.0.0:8080", - scopeSuffix: "", - }, "simple_annotation_case_default_endpoint": { - hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/enabled": "true", - "io.opentelemetry.discovery.metrics/config": configNoEndpoint, - }, expectedConf: userConfigMap{ - "collection_interval": "20s", - "endpoint": "1.1.1.1:8080", - "initial_delay": "20s", - "read_buffer_size": "10", - "nested_example": userConfigMap{"foo": "bar"}, - }, defaultEndpoint: "1.1.1.1:8080", - scopeSuffix: "", - }, "simple_annotation_case_scoped": { - hintsAnn: map[string]string{ - "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", - "initial_delay": "20s", - "read_buffer_size": "10", - "nested_example": userConfigMap{"foo": "bar"}, - }, defaultEndpoint: "0.0.0.0:8080", - scopeSuffix: "8080", - }, "simple_annotation_case_with_invalid_endpoint": { - hintsAnn: map[string]string{ - "io.opentelemetry.discovery.metrics/enabled": "true", - "io.opentelemetry.discovery.metrics/config": config, - }, expectedConf: userConfigMap{}, - defaultEndpoint: "1.2.3.4:8080", - scopeSuffix: "", - expectError: true, - }, + }{ + "simple_annotation_case": { + hintsAnn: map[string]string{ + "io.opentelemetry.discovery.metrics/enabled": "true", + "io.opentelemetry.discovery.metrics/config": config, + }, expectedConf: userConfigMap{ + "collection_interval": "20s", + "endpoint": "0.0.0.0:8080", + "initial_delay": "20s", + "read_buffer_size": "10", + "nested_example": userConfigMap{"foo": "bar"}, + }, defaultEndpoint: "0.0.0.0:8080", + scopeSuffix: "", + }, "simple_annotation_case_default_endpoint": { + hintsAnn: map[string]string{ + "io.opentelemetry.discovery.metrics/enabled": "true", + "io.opentelemetry.discovery.metrics/config": configNoEndpoint, + }, expectedConf: userConfigMap{ + "collection_interval": "20s", + "endpoint": "1.1.1.1:8080", + "initial_delay": "20s", + "read_buffer_size": "10", + "nested_example": userConfigMap{"foo": "bar"}, + }, defaultEndpoint: "1.1.1.1:8080", + scopeSuffix: "", + }, "simple_annotation_case_scoped": { + hintsAnn: map[string]string{ + "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", + "initial_delay": "20s", + "read_buffer_size": "10", + "nested_example": userConfigMap{"foo": "bar"}, + }, defaultEndpoint: "0.0.0.0:8080", + scopeSuffix: "8080", + }, "simple_annotation_case_with_invalid_endpoint": { + hintsAnn: map[string]string{ + "io.opentelemetry.discovery.metrics/enabled": "true", + "io.opentelemetry.discovery.metrics/config": config, + }, expectedConf: userConfigMap{}, + defaultEndpoint: "1.2.3.4:8080", + scopeSuffix: "", + expectError: true, + }, } for name, test := range tests { @@ -287,7 +300,7 @@ nested_example: } func TestDiscoveryMetricsEnabled(t *testing.T) { - var config = ` + config := ` endpoint: "0.0.0.0:8080"` tests := map[string]struct { hintsAnn map[string]string @@ -346,6 +359,11 @@ func TestValidateEndpoint(t *testing.T) { defaultEndpoint: "1.2.3.4:8080", expectError: false, }, + "test_invalid": { + endpoint: "http://0.0.0.0:8080/some?foo=1.2.3.4:8080", + defaultEndpoint: "1.2.3.4:8080", + expectError: true, + }, "test_valid_no_scheme": { endpoint: "1.2.3.4:8080/stats", defaultEndpoint: "1.2.3.4:8080", diff --git a/receiver/receivercreator/fixtures_test.go b/receiver/receivercreator/fixtures_test.go index b17c60604ed2..7abffe5d986e 100644 --- a/receiver/receivercreator/fixtures_test.go +++ b/receiver/receivercreator/fixtures_test.go @@ -72,8 +72,10 @@ var portEndpointWithHints = observer.Endpoint{ otelMetricsHints + "/enabled": "true", otelMetricsHints + "/scraper": "with_endpoint", otelMetricsHints + "/config": config, - }}, - Port: 6379}, + }, + }, + Port: 6379, + }, } var hostportEndpoint = observer.Endpoint{