Skip to content

Commit

Permalink
make enablement hint mandatory
Browse files Browse the repository at this point in the history
Signed-off-by: ChrsMark <chrismarkou92@gmail.com>
  • Loading branch information
ChrsMark committed Nov 20, 2024
1 parent fea0e37 commit 20f501e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 31 deletions.
6 changes: 3 additions & 3 deletions receiver/receivercreator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down
30 changes: 15 additions & 15 deletions receiver/receivercreator/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ func (builder *k8sHintsBuilder) createReceiverTemplateFromHints(env observer.End
if err != nil {
return nil, fmt.Errorf("could not extract endpoint's pod: %v", zap.Any("endpointPod", pod))
}
if len(pod.Annotations) == 0 {
return nil, nil
}
} else {
return nil, nil
}
Expand All @@ -105,8 +102,8 @@ func (builder *k8sHintsBuilder) createScraper(
return nil, nil
}

subreceiverKey := getHintAnnotation(annotations, otelMetricsHints, scraperHint, fmt.Sprint(port))
if subreceiverKey == "" {
subreceiverKey, found := getHintAnnotation(annotations, otelMetricsHints, scraperHint, fmt.Sprint(port))
if !found || subreceiverKey == "" {
// no scraper hint detected
return nil, nil
}
Expand All @@ -132,8 +129,8 @@ func getScraperConfFromAnnotations(
conf := userConfigMap{}
conf[endpointConfigKey] = defaultEndpoint

configStr := getHintAnnotation(annotations, otelMetricsHints, configHint, scopeSuffix)
if configStr == "" {
configStr, found := getHintAnnotation(annotations, otelMetricsHints, configHint, scopeSuffix)
if !found || configStr == "" {
return conf
}
if err := yaml.Unmarshal([]byte(configStr), &conf); err != nil {
Expand All @@ -155,22 +152,25 @@ func getScraperConfFromAnnotations(
return conf
}

func getHintAnnotation(annotations map[string]string, hintBase string, hintKey string, suffix string) string {
func getHintAnnotation(annotations map[string]string, hintBase string, hintKey string, suffix string) (string, bool) {
// try to scope the hint more on container level by suffixing
// with .<port> in case of Port event or # TODO: .<container_name> in case of Pod Container event
containerLevelHint := annotations[fmt.Sprintf("%s.%s/%s", hintBase, suffix, hintKey)]
if containerLevelHint != "" {
return containerLevelHint
containerLevelHint, ok := annotations[fmt.Sprintf("%s.%s/%s", hintBase, suffix, hintKey)]
if ok {
return containerLevelHint, ok
}

// if there is no container level hint defined try to use the Pod level hint
podHintKey := fmt.Sprintf("%s/%s", hintBase, hintKey)
return annotations[podHintKey]
podLevelHint, ok := annotations[fmt.Sprintf("%s/%s", hintBase, hintKey)]
return podLevelHint, ok
}

func discoveryMetricsEnabled(annotations map[string]string, hintBase string, scopeSuffix string) bool {
hintVal := getHintAnnotation(annotations, hintBase, discoveryEnabledHint, scopeSuffix)
return hintVal == "true" || hintVal == ""
enabledHint, found := getHintAnnotation(annotations, hintBase, discoveryEnabledHint, scopeSuffix)
if !found {
return false
}
return enabledHint == "true"
}

func getStringEnv(env observer.EndpointEnv, key string) string {
Expand Down
30 changes: 17 additions & 13 deletions receiver/receivercreator/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}},
Expand All @@ -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,
}},
Expand All @@ -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},
Expand All @@ -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,
}},
Expand All @@ -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,
Expand All @@ -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,
}}},
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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: "",
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions receiver/receivercreator/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}},
Expand Down

0 comments on commit 20f501e

Please sign in to comment.