From f828f547380ce33ea5bd7d7b483bb573a8df1a70 Mon Sep 17 00:00:00 2001 From: Baptiste Roux <32563450+BapRx@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:30:43 +0100 Subject: [PATCH] [prometheus-mongodb-exporter] Support multi-target exporter (#4875) * feat(mongodb): support multi-target exporter Signed-off-by: Baptiste Roux * feat(prometheus-mongodb-exporter): apply suggestions Signed-off-by: Baptiste Roux * chore(prometheus-mongodb-exporter): bump chart version Signed-off-by: Baptiste Roux * chore(prometheus-mongodb-exporter): add CI multi-target test Signed-off-by: Baptiste Roux * fix(prometheus-mongodb-exporter): rename CI multi-target test file Signed-off-by: Baptiste Roux --------- Signed-off-by: Baptiste Roux --- charts/prometheus-mongodb-exporter/Chart.yaml | 2 +- .../servicemonitor-multi-target-values.yaml | 18 ++++++ .../templates/_helpers.tpl | 11 ++++ .../templates/deployment.yaml | 16 +++++ .../templates/secret.yaml | 2 +- .../templates/servicemonitor.yaml | 59 ++++++++++++++----- .../prometheus-mongodb-exporter/values.yaml | 24 ++++++++ 7 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 charts/prometheus-mongodb-exporter/ci/servicemonitor-multi-target-values.yaml diff --git a/charts/prometheus-mongodb-exporter/Chart.yaml b/charts/prometheus-mongodb-exporter/Chart.yaml index ec7452919cf5..ce35ec51c725 100644 --- a/charts/prometheus-mongodb-exporter/Chart.yaml +++ b/charts/prometheus-mongodb-exporter/Chart.yaml @@ -15,4 +15,4 @@ maintainers: name: prometheus-mongodb-exporter sources: - https://github.com/percona/mongodb_exporter -version: 3.8.0 +version: 3.9.0 diff --git a/charts/prometheus-mongodb-exporter/ci/servicemonitor-multi-target-values.yaml b/charts/prometheus-mongodb-exporter/ci/servicemonitor-multi-target-values.yaml new file mode 100644 index 000000000000..6b9118c8faac --- /dev/null +++ b/charts/prometheus-mongodb-exporter/ci/servicemonitor-multi-target-values.yaml @@ -0,0 +1,18 @@ +serviceMonitor: + enabled: true + multiTarget: + enabled: true + username: test + password: + value: test + targets: + - uri: "mongodb://mongodb-0.staging.internal:27017/admin" + name: "mongodb-0" + - uri: "mongodb://mongodb-1.staging.internaltld:27017/admin" + name: "mongodb-1" + scrapeTimeout: 30s + interval: 60s + metricRelabelings: + - targetLabel: environment + replacement: staging + action: replace diff --git a/charts/prometheus-mongodb-exporter/templates/_helpers.tpl b/charts/prometheus-mongodb-exporter/templates/_helpers.tpl index 42518bcd0281..6776aa5de7ee 100644 --- a/charts/prometheus-mongodb-exporter/templates/_helpers.tpl +++ b/charts/prometheus-mongodb-exporter/templates/_helpers.tpl @@ -74,3 +74,14 @@ Determine secret name, can either be the self-created of an existing one {{ include "prometheus-mongodb-exporter.fullname" . }} {{- end -}} {{- end -}} + +{{/* +Generate the MongoDB URI from the serviceMonitor multi-target list +*/}} +{{- define "prometheus-mongodb-exporter.mongodbUri" -}} +{{- $uriList := list }} +{{- range .Values.serviceMonitor.multiTarget.targets }} +{{- $uriList = append $uriList (printf "%s" .uri ) }} +{{- end }} +{{- join "," $uriList }} +{{- end }} diff --git a/charts/prometheus-mongodb-exporter/templates/deployment.yaml b/charts/prometheus-mongodb-exporter/templates/deployment.yaml index 2655881b55a9..513e6f6da2d1 100644 --- a/charts/prometheus-mongodb-exporter/templates/deployment.yaml +++ b/charts/prometheus-mongodb-exporter/templates/deployment.yaml @@ -28,11 +28,27 @@ spec: containers: - name: mongodb-exporter env: + {{- if .Values.serviceMonitor.multiTarget.enabled }} + - name: MONGODB_URI + value: {{ include "prometheus-mongodb-exporter.mongodbUri" . }} + - name: MONGODB_USER + value: {{ required "The MongoDB username must be set" .Values.serviceMonitor.multiTarget.username }} + - name: MONGODB_PASSWORD + {{- if and .Values.serviceMonitor.multiTarget.password.existingSecret.name .Values.serviceMonitor.multiTarget.password.existingSecret.key }} + valueFrom: + secretKeyRef: + name: {{ .Values.serviceMonitor.multiTarget.password.existingSecret.name }} + key: {{ .Values.serviceMonitor.multiTarget.password.existingSecret.key }} + {{- else }} + value: {{ .Values.serviceMonitor.multiTarget.password.value }} + {{- end }} + {{- else }} - name: MONGODB_URI valueFrom: secretKeyRef: name: {{ include "prometheus-mongodb-exporter.secretName" . }} key: {{ .Values.existingSecret.key }} + {{- end }} {{- if .Values.env }} {{- range $key, $value := .Values.env }} - name: "{{ $key }}" diff --git a/charts/prometheus-mongodb-exporter/templates/secret.yaml b/charts/prometheus-mongodb-exporter/templates/secret.yaml index 0ab309121bb0..b339c9135354 100644 --- a/charts/prometheus-mongodb-exporter/templates/secret.yaml +++ b/charts/prometheus-mongodb-exporter/templates/secret.yaml @@ -1,4 +1,4 @@ -{{- if not .Values.existingSecret.name -}} +{{- if and (not .Values.existingSecret.name) (not .Values.serviceMonitor.multiTarget.enabled) -}} apiVersion: v1 kind: Secret metadata: diff --git a/charts/prometheus-mongodb-exporter/templates/servicemonitor.yaml b/charts/prometheus-mongodb-exporter/templates/servicemonitor.yaml index 576860a7650a..28336cf9cd43 100644 --- a/charts/prometheus-mongodb-exporter/templates/servicemonitor.yaml +++ b/charts/prometheus-mongodb-exporter/templates/servicemonitor.yaml @@ -13,22 +13,53 @@ metadata: {{- end }} spec: endpoints: - - port: {{ .Values.service.portName }} - interval: {{ .Values.serviceMonitor.interval }} - scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }} - {{- if .Values.serviceMonitor.metricRelabelings }} - metricRelabelings: {{ toYaml .Values.serviceMonitor.metricRelabelings | nindent 4 }} - {{- end }} - {{- if .Values.serviceMonitor.scheme }} - scheme: {{ .Values.serviceMonitor.scheme }} - {{- end }} - {{- if .Values.serviceMonitor.tlsConfig }} - tlsConfig: - {{- toYaml .Values.serviceMonitor.tlsConfig | nindent 6 }} - {{- end }} + {{- if .Values.serviceMonitor.multiTarget.enabled }} + {{- range .Values.serviceMonitor.multiTarget.targets }} + {{- $mongodburi := mustRegexFind "^mongodb(\\+srv)?://[^/]+" .uri }} + - path: /scrape + port: {{ $.Values.service.portName }} + {{- with $.Values.serviceMonitor.scheme }} + scheme: {{ . }} + {{- end }} + {{- with $.Values.serviceMonitor.tlsConfig }} + tlsConfig: {{ toYaml . | nindent 6 }} + {{- end }} + interval: {{ .interval | default $.Values.serviceMonitor.interval }} + scrapeTimeout: {{ .scrapeTimeout | default $.Values.serviceMonitor.scrapeTimeout }} + metricRelabelings: + - action: replace + sourceLabels: [instance] + targetLabel: instance + replacement: {{ $mongodburi }} + - action: replace + sourceLabels: [target] + targetLabel: target + replacement: {{ .name }} + {{- if or .metricRelabelings $.Values.serviceMonitor.metricRelabelings }} + {{- toYaml (.metricRelabelings | default $.Values.serviceMonitor.metricRelabelings) | nindent 8 }} + {{- end }} + params: + target: + - {{ $mongodburi }} + {{- end }} + {{- else }} + - port: {{ .Values.service.portName }} + interval: {{ .Values.serviceMonitor.interval }} + scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }} + {{- with .Values.serviceMonitor.metricRelabelings }} + metricRelabelings: {{ toYaml . | nindent 6 }} + {{- end }} + {{- with .Values.serviceMonitor.scheme }} + scheme: {{ . }} + {{- end }} + {{- with .Values.serviceMonitor.tlsConfig }} + tlsConfig: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- end }} namespaceSelector: matchNames: - - {{ .Release.Namespace }} + - {{ .Release.Namespace }} selector: matchLabels: {{- include "prometheus-mongodb-exporter.selectorLabels" . | nindent 6 }} diff --git a/charts/prometheus-mongodb-exporter/values.yaml b/charts/prometheus-mongodb-exporter/values.yaml index ab09d7e9ceba..74e498e0b15c 100644 --- a/charts/prometheus-mongodb-exporter/values.yaml +++ b/charts/prometheus-mongodb-exporter/values.yaml @@ -28,12 +28,14 @@ livenessProbe: port: metrics initialDelaySeconds: 10 +# If the multi-target mode is enabled, this value will be ignored. # [mongodb[+srv]://][user:pass@]host1[:port1][,host2[:port2],...][/database][?options] mongodb: uri: "mongodb://mongodb:27017" # Name of an externally managed secret (in the same namespace) containing the connection uri as key `mongodb-uri`. # If this is provided, the value mongodb.uri is ignored. +# If the multi-target mode is enabled, this value will be ignored. existingSecret: name: "" key: "mongodb-uri" @@ -93,6 +95,28 @@ serviceAccount: serviceMonitor: enabled: false + multiTarget: + enabled: false + # The username and password must be the same for every target configured below + username: "" + password: + existingSecret: + name: "" + key: password + value: "" + targets: [] + # Every target must have uri and name set + # - uri: "mongodb://mongodb-0.staging.internal:27017/admin" + # name: "mongodb-0" + # - uri: "mongodb://mongodb-1.staging.internaltld:27017/admin" + # name: "mongodb-1" + # scrapeTimeout: 30s + # interval: 60s + # metricRelabelings: + # - targetLabel: environment + # replacement: staging + # action: replace + interval: 30s scrapeTimeout: 10s namespace: