Skip to content

Commit

Permalink
[chart] Create a service monitor for each script (#112)
Browse files Browse the repository at this point in the history
* feat(chart): create service monitor for each script

* chore(chart): bump version to v2.17.0

* feat(servicemonitor): add option to create one SM for each script of to customize which target to scrape
  • Loading branch information
BapRx authored Feb 8, 2024
1 parent b93ce0f commit 44ddcaf
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 21 deletions.
4 changes: 2 additions & 2 deletions charts/script-exporter/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ apiVersion: v2
name: script-exporter
description: Prometheus exporter to execute scripts and collect metrics from the output or the exit status.
type: application
version: 0.7.1
appVersion: v2.16.0
version: 0.8.0
appVersion: v2.17.0
36 changes: 36 additions & 0 deletions charts/script-exporter/templates/selfservicemonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{{- if .Values.serviceMonitor.selfMonitor.enabled }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "script-exporter.fullname" . }}
labels:
{{- include "script-exporter.labels" . | nindent 4 }}
{{- include "script-exporter.serviceMonitorLabels" . | nindent 4 }}
namespace: {{ default .Release.Namespace .Values.serviceMonitor.namespace }}
spec:
endpoints:
- port: http
{{- with .Values.serviceMonitor.interval }}
interval: {{ . }}
{{- end }}
{{- with .Values.serviceMonitor.scrapeTimeout }}
scrapeTimeout: {{ . }}
{{- end }}
path: /metrics
honorLabels: {{ .Values.serviceMonitor.honorLabels }}
{{- with .Values.serviceMonitor.metricRelabelings }}
metricRelabelings:
{{ toYaml . | nindent 6 }}
{{- end }}
{{- with .Values.serviceMonitor.relabelings }}
relabelings:
{{ toYaml . | nindent 6 }}
{{- end }}
namespaceSelector:
matchNames:
- {{ .Release.Namespace }}
selector:
matchLabels:
{{- include "script-exporter.selectorLabels" . | nindent 6 }}
{{- end }}
96 changes: 79 additions & 17 deletions charts/script-exporter/templates/servicemonitor.yaml
Original file line number Diff line number Diff line change
@@ -1,34 +1,96 @@
{{- if .Values.serviceMonitor.enabled }}
{{- if .Values.serviceMonitor.autoCreate.enabled }}
{{- range (fromYaml .Values.config).scripts }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: {{ include "script-exporter.fullname" . }}
labels:
{{- include "script-exporter.labels" . | nindent 4 }}
{{- include "script-exporter.serviceMonitorLabels" . | nindent 4 }}
{{- include "script-exporter.labels" $ | nindent 4 }}
{{- include "script-exporter.serviceMonitorLabels" $ | nindent 4 }}
name: {{ include "script-exporter.fullname" $ }}-{{ kebabcase .name }}
namespace: {{ default $.Release.Namespace $.Values.serviceMonitor.namespace }}
spec:
endpoints:
- port: http
{{- if .Values.serviceMonitor.interval }}
interval: {{ .Values.serviceMonitor.interval }}
- path: /probe
port: http
{{- with $.Values.serviceMonitor.interval }}
interval: {{ . }}
{{- end }}
{{- if .Values.serviceMonitor.scrapeTimeout }}
scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }}
{{- with $.Values.serviceMonitor.scrapeTimeout }}
scrapeTimeout: {{ . }}
{{- end }}
path: /metrics
honorLabels: {{ .Values.serviceMonitor.honorLabels }}
{{- if .Values.serviceMonitor.metricRelabelings }}
params:
script:
- {{ .name }}
metricRelabelings:
{{ toYaml .Values.serviceMonitor.metricRelabelings | nindent 6 }}
- action: replace
replacement: {{ .name }}
targetLabel: script
{{- with $.Values.serviceMonitor.metricRelabelings }}
{{- toYaml . | nindent 8 }}
{{- end }}
{{- if .Values.serviceMonitor.relabelings }}
relabelings:
{{ toYaml .Values.serviceMonitor.relabelings | nindent 6 }}
- action: replace
replacement: {{ .name }}
targetLabel: script
{{- with $.Values.serviceMonitor.relabelings }}
{{- toYaml . | nindent 8 }}
{{- end }}
namespaceSelector:
matchNames:
- {{ .Release.Namespace }}
- {{ $.Release.Namespace }}
selector:
matchLabels:
{{- include "script-exporter.selectorLabels" . | nindent 6 }}
{{- end -}}
{{- include "script-exporter.selectorLabels" $ | nindent 6 }}
{{- end }}
{{- else }}
{{- range .Values.serviceMonitor.targets }}
---
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
{{- include "script-exporter.labels" $ | nindent 4 }}
{{- include "script-exporter.serviceMonitorLabels" $ | nindent 4 }}
{{- with .labels }}
{{- toYaml . | nindent 4 }}
{{- end }}
name: {{ include "script-exporter.fullname" $ }}-{{ kebabcase .name }}
namespace: {{ default $.Release.Namespace $.Values.serviceMonitor.namespace }}
spec:
endpoints:
- path: /probe
port: http
{{- with .interval | default $.Values.serviceMonitor.interval }}
interval: {{ . }}
{{- end }}
{{- with .scrapeTimeout | default $.Values.serviceMonitor.scrapeTimeout }}
scrapeTimeout: {{ . }}
{{- end }}
params:
script:
- {{ .script }}
metricRelabelings:
- action: replace
replacement: {{ .script }}
targetLabel: script
{{- with .additionalMetricsRelabels | default $.Values.serviceMonitor.metricRelabelings }}
{{- toYaml . | nindent 8 }}
{{- end }}
relabelings:
- action: replace
replacement: {{ .script }}
targetLabel: script
{{- with .additionalRelabeling | default $.Values.serviceMonitor.relabelings }}
{{- toYaml . | nindent 8 }}
{{- end }}
namespaceSelector:
matchNames:
- {{ $.Release.Namespace }}
selector:
matchLabels:
{{- include "script-exporter.selectorLabels" $ | nindent 6 }}
{{- end }}
{{- end }}
{{- end }}
38 changes: 36 additions & 2 deletions charts/script-exporter/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,34 @@ serviceAccount:
## See: https://github.com/coreos/prometheus-operator
##
serviceMonitor:
## If true, a ServiceMonitor CRD is created for a prometheus operator
## https://github.com/coreos/prometheus-operator for script-exporter itself
##
selfMonitor:
enabled: true
additionalMetricsRelabels: {}
additionalRelabeling: []
labels: {}
path: /metrics
interval: 30s
scrapeTimeout: 30s

## If true, a ServiceMonitor CRD is created for a prometheus operator
## https://github.com/coreos/prometheus-operator for each target
##
enabled: false

## Namespace for the ServiceMonitor. Fallback to the the release namespace.
##
namespace: ""

## Interval at which metrics should be scraped. Fallback to the Prometheus default unless specified.
##
# interval: 10s
interval: ""

## Timeout after which the scrape is ended. Fallback to the Prometheus default unless specified.
##
# scrapeTimeout: 30s
scrapeTimeout: ""

## Additional labels that are used by the Prometheus installed in your cluster to select Service Monitors to work with
## See: https://github.com/coreos/prometheus-operator/blob/master/Documentation/api.md#prometheusspec
Expand Down Expand Up @@ -162,6 +181,21 @@ serviceMonitor:
# replacement: $1
# action: replace

## Automatically create a serviceMonitor for each script defined in the 'config' section below
## This option is mutaly exclusive with the following 'targets' list
##
autoCreate:
enabled: true

targets: []
# - name: example # Human readable URL that will appear in Prometheus / AlertManager
# script: ping # Name of the script to target.
# labels: {} # Map of labels for ServiceMonitor. Overrides value set in `defaults`
# interval: 60s # Scraping interval. Overrides value set in `defaults`
# scrapeTimeout: 60s # Scrape timeout. Overrides value set in `defaults`
# additionalMetricsRelabels: [] # List of metric relabeling actions to run
# additionalRelabeling: [] # List of relabeling actions to run

## The configuration for the script_exporter as shown in
## https://github.com/ricoberger/script_exporter/tree/main#usage-and-configuration
##
Expand Down

0 comments on commit 44ddcaf

Please sign in to comment.