Skip to content

Commit

Permalink
feat(mongodb): support multi-target exporter
Browse files Browse the repository at this point in the history
Signed-off-by: Baptiste Roux <[email protected]>
  • Loading branch information
BapRx committed Oct 30, 2024
1 parent 5f64862 commit 4f647c9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 15 deletions.
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: {{ .Values.serviceMonitor.multiTarget.username }}
- name: MONGODB_PASSWORD
{{- if .Values.serviceMonitor.multiTarget.password.existingSecret }}
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
56 changes: 42 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,50 @@ 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 }}
- 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: {{ mustRegexFind "^mongodb(\\+srv)?://[^/]+" .uri }}
- action: replace
sourceLabels: [target]
targetLabel: target
replacement: {{ .name }}
{{- toYaml (.metricRelabelings | default $.Values.serviceMonitor.metricRelabelings) | nindent 8 }}
params:
target:
- {{ mustRegexFind "^mongodb(\\+srv)?://[^/]+" .uri }}
{{- 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
21 changes: 21 additions & 0 deletions charts/prometheus-mongodb-exporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ serviceAccount:

serviceMonitor:
enabled: false
multiTarget:
enabled: false
username: ""
password:
existingSecret:
name: ""
key: ""
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 4f647c9

Please sign in to comment.