Skip to content

Commit

Permalink
gofmt
Browse files Browse the repository at this point in the history
Signed-off-by: ChrsMark <[email protected]>
  • Loading branch information
ChrsMark committed Nov 23, 2024
1 parent 9f4ebcb commit 145ad1b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 68 deletions.
10 changes: 5 additions & 5 deletions receiver/receivercreator/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
140 changes: 79 additions & 61 deletions receiver/receivercreator/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -53,8 +53,10 @@ password: "changeme"`
otelMetricsHints + "/enabled": "true",
otelMetricsHints + "/scraper": "redis",
otelMetricsHints + "/config": config,
}},
Port: 6379},
},
},
Port: 6379,
},
},
expectedReceiver: receiverTemplate{
receiverConfig: receiverConfig{
Expand All @@ -78,8 +80,10 @@ password: "changeme"`
otelMetricsHints + "/enabled": "true",
otelMetricsHints + "/scraper": "redis",
otelMetricsHints + "/config": config,
}},
Port: 6379},
},
},
Port: 6379,
},
},
expectedReceiver: receiverTemplate{},
wantError: false,
Expand All @@ -97,8 +101,10 @@ password: "changeme"`
Annotations: map[string]string{
otelMetricsHints + "/enabled": "true",
otelMetricsHints + "/scraper": "redis",
}},
Port: 6379},
},
},
Port: 6379,
},
},
expectedReceiver: receiverTemplate{
receiverConfig: receiverConfig{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -173,7 +183,9 @@ password: "changeme"`
otelMetricsHints + "/enabled": "true",
otelMetricsHints + "/scraper": "redis",
otelMetricsHints + "/config": config,
}}},
},
},
},
},
expectedReceiver: receiverTemplate{},
wantError: true,
Expand Down Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 4 additions & 2 deletions receiver/receivercreator/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 145ad1b

Please sign in to comment.