Skip to content

Commit

Permalink
[prometheus-mongodb-exporter] Support multi-target exporter (#4875)
Browse files Browse the repository at this point in the history
* feat(mongodb): support multi-target exporter

Signed-off-by: Baptiste Roux <[email protected]>

* feat(prometheus-mongodb-exporter): apply suggestions

Signed-off-by: Baptiste Roux <[email protected]>

* chore(prometheus-mongodb-exporter): bump chart version

Signed-off-by: Baptiste Roux <[email protected]>

* chore(prometheus-mongodb-exporter): add CI multi-target test

Signed-off-by: Baptiste Roux <[email protected]>

* fix(prometheus-mongodb-exporter): rename CI multi-target test file

Signed-off-by: Baptiste Roux <[email protected]>

---------

Signed-off-by: Baptiste Roux <[email protected]>
  • Loading branch information
BapRx authored Nov 5, 2024
1 parent d0cbf1b commit f828f54
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 16 deletions.
2 changes: 1 addition & 1 deletion charts/prometheus-mongodb-exporter/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ maintainers:
name: prometheus-mongodb-exporter
sources:
- https://github.com/percona/mongodb_exporter
version: 3.8.0
version: 3.9.0
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions charts/prometheus-mongodb-exporter/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
16 changes: 16 additions & 0 deletions charts/prometheus-mongodb-exporter/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
2 changes: 1 addition & 1 deletion charts/prometheus-mongodb-exporter/templates/secret.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
59 changes: 45 additions & 14 deletions charts/prometheus-mongodb-exporter/templates/servicemonitor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
24 changes: 24 additions & 0 deletions charts/prometheus-mongodb-exporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit f828f54

Please sign in to comment.