From 24a8739472ef07acb16c4280742ce74771a6f480 Mon Sep 17 00:00:00 2001 From: Alex Szakaly Date: Wed, 9 Sep 2026 18:37:15 +0200 Subject: [PATCH] feat: add ServiceMonitor endpoint tuning options Expose monitoring.serviceMonitor.{interval,scrapeTimeout,honorLabels, metricRelabelings,relabelings} on the Prometheus ServiceMonitor. All fields are only rendered when set, so the default output is unchanged (honorLabels defaults to false and stays omitted). Signed-off-by: Alex Szakaly --- templates/monitor.yml | 15 ++++ tests/monitor_test.yaml | 157 ++++++++++++++++++++++++++++++++++++++++ values.yaml | 20 +++++ 3 files changed, 192 insertions(+) diff --git a/templates/monitor.yml b/templates/monitor.yml index 27ef370..f964243 100644 --- a/templates/monitor.yml +++ b/templates/monitor.yml @@ -14,6 +14,21 @@ spec: endpoints: - port: kamon path: /metrics + {{- if .Values.monitoring.serviceMonitor.interval }} + interval: {{ .Values.monitoring.serviceMonitor.interval }} + {{- end }} + {{- if .Values.monitoring.serviceMonitor.scrapeTimeout }} + scrapeTimeout: {{ .Values.monitoring.serviceMonitor.scrapeTimeout }} + {{- end }} + {{- if .Values.monitoring.serviceMonitor.honorLabels }} + honorLabels: {{ .Values.monitoring.serviceMonitor.honorLabels }} + {{- end }} + {{- if .Values.monitoring.serviceMonitor.metricRelabelings }} + metricRelabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.monitoring.serviceMonitor.metricRelabelings "context" $) | nindent 8 }} + {{- end }} + {{- if .Values.monitoring.serviceMonitor.relabelings }} + relabelings: {{- include "common.tplvalues.render" ( dict "value" .Values.monitoring.serviceMonitor.relabelings "context" $) | nindent 8 }} + {{- end }} selector: matchLabels: {{- include "common.labels.matchLabels" . | nindent 6 }} namespaceSelector: diff --git a/tests/monitor_test.yaml b/tests/monitor_test.yaml index 1651e06..e4d8a29 100644 --- a/tests/monitor_test.yaml +++ b/tests/monitor_test.yaml @@ -107,3 +107,160 @@ tests: - equal: path: metadata.labels["app.kubernetes.io/managed-by"] value: Helm + + - it: should omit all optional endpoint fields by default + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - isKind: + of: ServiceMonitor + - notExists: + path: spec.endpoints[0].interval + - notExists: + path: spec.endpoints[0].scrapeTimeout + - notExists: + path: spec.endpoints[0].honorLabels + - notExists: + path: spec.endpoints[0].metricRelabelings + - notExists: + path: spec.endpoints[0].relabelings + + - it: should keep honorLabels omitted when explicitly set to false + set: + monitoring.serviceMonitor.honorLabels: false + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - isKind: + of: ServiceMonitor + - notExists: + path: spec.endpoints[0].honorLabels + + - it: should render honorLabels when set to true + set: + monitoring.serviceMonitor.honorLabels: true + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].honorLabels + value: true + + - it: should render interval when set + set: + monitoring.serviceMonitor.interval: 30s + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].interval + value: 30s + + - it: should render scrapeTimeout when set + set: + monitoring.serviceMonitor.scrapeTimeout: 10s + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].scrapeTimeout + value: 10s + + - it: should render metricRelabelings when set + set: + monitoring.serviceMonitor.metricRelabelings: + - sourceLabels: [__name__] + regex: expensive_metric.* + action: drop + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].metricRelabelings + value: + - sourceLabels: [__name__] + regex: expensive_metric.* + action: drop + + - it: should render relabelings when set + set: + monitoring.serviceMonitor.relabelings: + - sourceLabels: [__meta_kubernetes_pod_node_name] + targetLabel: node + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].relabelings + value: + - sourceLabels: [__meta_kubernetes_pod_node_name] + targetLabel: node + + - it: should template relabelings values via common.tplvalues.render + release: + name: my-stream + set: + monitoring.serviceMonitor.relabelings: + - targetLabel: release + replacement: "{{ .Release.Name }}" + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].relabelings + value: + - targetLabel: release + replacement: my-stream + + - it: should template metricRelabelings values via common.tplvalues.render + set: + monitoring.serviceMonitor.metricRelabelings: + - sourceLabels: [namespace] + regex: "{{ .Release.Namespace }}" + targetLabel: kube_namespace + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints[0].metricRelabelings + value: + - sourceLabels: [namespace] + regex: NAMESPACE + targetLabel: kube_namespace + + - it: should render all endpoint fields together when set + set: + monitoring.serviceMonitor.interval: 30s + monitoring.serviceMonitor.scrapeTimeout: 10s + monitoring.serviceMonitor.honorLabels: true + capabilities: + apiVersions: + - monitoring.coreos.com/v1 + template: monitor.yml + asserts: + - equal: + path: spec.endpoints + value: + - port: kamon + path: /metrics + interval: 30s + scrapeTimeout: 10s + honorLabels: true diff --git a/values.yaml b/values.yaml index c67118e..5e170ca 100644 --- a/values.yaml +++ b/values.yaml @@ -401,6 +401,26 @@ ingress: monitoring: ## Enable the creation of a ServiceMonitor object for Stream if the cluster has the monitoring.coreos.com/v1 capability enabled: true + ## Prometheus Operator ServiceMonitor configuration + serviceMonitor: + ## Chooses the metric's labels on collisions with target labels + honorLabels: false + ## Interval at which metrics should be scraped. + ## @ref https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api-reference/api.md#monitoring.coreos.com/v1.Endpoint + ## @example + ## interval: 10s + ## + interval: "" + ## Timeout after which the scrape is ended + ## @ref https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api-reference/api.md#monitoring.coreos.com/v1.Endpoint + ## @example + ## scrapeTimeout: 10s + ## + scrapeTimeout: "" + ## Specify additional relabeling of metrics + metricRelabelings: [] + ## Specify general relabeling + relabelings: [] ## Configure the Play secret for the Stream instance. ## As this is used for cryptographic purposes, it should be fetched ## from the environment.