From 58abe249eeae5e0fa493a6db164ec3935bbb9aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20Z=C3=B6llner?= Date: Thu, 26 Jan 2023 11:25:36 +0100 Subject: [PATCH 1/2] Add metricRelabelings and relabelings to serviceMonitor --- templates/prometheus-servicemonitor.yaml | 6 ++++++ values.yaml | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/templates/prometheus-servicemonitor.yaml b/templates/prometheus-servicemonitor.yaml index 323e51fb9..3a69385f1 100644 --- a/templates/prometheus-servicemonitor.yaml +++ b/templates/prometheus-servicemonitor.yaml @@ -38,6 +38,12 @@ spec: - prometheus tlsConfig: insecureSkipVerify: true + {{- if .Values.serverTelemetry.serviceMonitor.metricRelabelings }} + metricRelabelings: {{- toYaml .Values.serverTelemetry.serviceMonitor.metricRelabelings | nindent 4 }} + {{- end }} + {{- if .Values.serverTelemetry.serviceMonitor.relabelings }} + relabelings: {{- toYaml .Values.serverTelemetry.serviceMonitor.relabelings | nindent 4 }} + {{- end }} namespaceSelector: matchNames: - {{ .Release.Namespace }} diff --git a/values.yaml b/values.yaml index 2c3d9e261..e33354ef8 100644 --- a/values.yaml +++ b/values.yaml @@ -1110,6 +1110,24 @@ serverTelemetry: # Timeout for Prometheus scrapes scrapeTimeout: 10s + # MetricRelabelConfigs to apply to samples before ingestion. + metricRelabelings: [] + # Some example metricRelabelings. + #- action: "replace" + # regex: "(.*)" + # sourceLabels: + # - "cluster" + # targetLabel: "vault_cluster" + + # RelabelConfigs to apply to samples before scraping. + relabelings: [] + # Some example relabelings. + #- action: "replace" + # regex: "(.*)" + # sourceLabels: + # - "cluster" + # targetLabel: "vault_cluster" + prometheusRules: # The Prometheus operator *must* be installed before enabling this feature, # if not the chart will fail to install due to missing CustomResourceDefinitions From bfc647f0dc1d3bce5e12050d2b72bf8158527327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20Z=C3=B6llner?= Date: Thu, 26 Jan 2023 11:26:17 +0100 Subject: [PATCH 2/2] Add unit tests for defaults and overrides of metricRelabelings and relabelings --- test/unit/prometheus-servicemonitor.bats | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/unit/prometheus-servicemonitor.bats b/test/unit/prometheus-servicemonitor.bats index 5d92c89d2..6112cc89b 100755 --- a/test/unit/prometheus-servicemonitor.bats +++ b/test/unit/prometheus-servicemonitor.bats @@ -123,3 +123,57 @@ load _helpers [ "$(echo "$output" | yq -r '.spec.endpoints | length')" = "1" ] [ "$(echo "$output" | yq -r '.spec.endpoints[0].port')" = "https" ] } + +@test "prometheus/ServiceMonitor-server: assertMetricRelabelings default" { + cd `chart_dir` + local output=$( (helm template \ + --show-only templates/prometheus-servicemonitor.yaml \ + --set 'serverTelemetry.serviceMonitor.enabled=true' \ + . ) | tee /dev/stderr) + + [ "$(echo "$output" | yq -r '.spec.endpoints | length')" = "1" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0] | has("metricRelabelings")')" = "false" ] +} + +@test "prometheus/ServiceMonitor-server: assertMetricRelabelings override" { + cd `chart_dir` + local output=$( (helm template \ + --show-only templates/prometheus-servicemonitor.yaml \ + --set 'serverTelemetry.serviceMonitor.enabled=true' \ + --set 'serverTelemetry.serviceMonitor.metricRelabelings[0]=foo' \ + --set 'serverTelemetry.serviceMonitor.metricRelabelings[1]=foo' \ + . ) | tee /dev/stderr) + + [ "$(echo "$output" | yq -r '.spec.endpoints | length')" = "1" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0] | has("metricRelabelings")')" = "true" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0].metricRelabelings | length')" = "2" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0].metricRelabelings[0]')" = "foo" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0].metricRelabelings[1]')" = "foo" ] +} + +@test "prometheus/ServiceMonitor-server: assertRelabelings default" { + cd `chart_dir` + local output=$( (helm template \ + --show-only templates/prometheus-servicemonitor.yaml \ + --set 'serverTelemetry.serviceMonitor.enabled=true' \ + . ) | tee /dev/stderr) + + [ "$(echo "$output" | yq -r '.spec.endpoints | length')" = "1" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0] | has("relabelings")')" = "false" ] +} + +@test "prometheus/ServiceMonitor-server: assertRelabelings override" { + cd `chart_dir` + local output=$( (helm template \ + --show-only templates/prometheus-servicemonitor.yaml \ + --set 'serverTelemetry.serviceMonitor.enabled=true' \ + --set 'serverTelemetry.serviceMonitor.relabelings[0]=foo' \ + --set 'serverTelemetry.serviceMonitor.relabelings[1]=foo' \ + . ) | tee /dev/stderr) + + [ "$(echo "$output" | yq -r '.spec.endpoints | length')" = "1" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0] | has("relabelings")')" = "true" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0].relabelings | length')" = "2" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0].relabelings[0]')" = "foo" ] + [ "$(echo "$output" | yq -r '.spec.endpoints[0].relabelings[1]')" = "foo" ] +}