Skip to content

Commit df734dd

Browse files
authored
Add selectors to ServiceMonitor, PodMonitor and Probe objects (#389)
1 parent d73d5f6 commit df734dd

File tree

5 files changed

+78
-4
lines changed

5 files changed

+78
-4
lines changed

charts/k8s-monitoring/README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -300,16 +300,19 @@ The Prometheus and Loki services may be hosted on the same cluster, or remotely
300300
| metrics.podMonitors.extraMetricRelabelingRules | string | `""` | Rule blocks to be added to the prometheus.relabel component for PodMonitor objects. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block)) |
301301
| metrics.podMonitors.namespaces | list | `[]` | Which namespaces to look for PodMonitor objects. |
302302
| metrics.podMonitors.scrapeInterval | string | 60s | How frequently to scrape metrics from PodMonitor objects. Only used if the PodMonitor does not specify the scrape interval. Overrides metrics.scrapeInterval |
303+
| metrics.podMonitors.selector | string | `""` | Which podMonitor objects to select |
303304
| metrics.probes.enabled | bool | `true` | Include service discovery for Probe objects. |
304305
| metrics.probes.extraMetricRelabelingRules | string | `""` | Rule blocks to be added to the prometheus.relabel component for Probe objects. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block)) |
305306
| metrics.probes.namespaces | list | `[]` | Which namespaces to look for Probe objects. |
306307
| metrics.probes.scrapeInterval | string | 60s | How frequently to scrape metrics from Probe objects. Only used if the Probe does not specify the scrape interval. Overrides metrics.scrapeInterval |
308+
| metrics.probes.selector | string | `""` | Which probe objects to select |
307309
| metrics.receiver.filters | object | `{"datapoint":[],"metric":[]}` | Apply a filter to metrics received via the OTLP or OTLP HTTP receivers. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/otelcol.processor.filter/)) |
308310
| metrics.scrapeInterval | string | `"60s"` | How frequently to scrape metrics |
309311
| metrics.serviceMonitors.enabled | bool | `true` | Include service discovery for ServiceMonitor objects |
310312
| metrics.serviceMonitors.extraMetricRelabelingRules | string | `""` | Rule blocks to be added to the prometheus.relabel component for ServiceMonitor objects. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block)) |
311313
| metrics.serviceMonitors.namespaces | list | `[]` | Which namespaces to look for ServiceMonitor objects. |
312314
| metrics.serviceMonitors.scrapeInterval | string | 60s | How frequently to scrape metrics from ServiceMonitor objects. Only used if the ServiceMonitor does not specify the scrape interval. Overrides metrics.scrapeInterval |
315+
| metrics.serviceMonitors.selector | string | `""` | Which serviceMonitors objects to select |
313316
| metrics.windows-exporter.enabled | bool | `false` | Scrape node metrics |
314317
| metrics.windows-exporter.extraMetricRelabelingRules | string | `""` | Rule blocks to be added to the prometheus.relabel component for Windows Exporter. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block)) |
315318
| metrics.windows-exporter.extraRelabelingRules | string | `""` | Rule blocks to be added to the discovery.relabel component for Windows Exporter. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/discovery.relabel/#rule-block)) |
@@ -418,12 +421,31 @@ For an example values file and generated output, see [this example](../../exampl
418421
419422
### Using Prometheus Operator CRDs
420423
421-
The default config can deploy the CRDs for Prometheus Operator, and will add support for `PodMonitor` and
422-
`ServiceMonitor` objects.
424+
The default config can deploy the CRDs for Prometheus Operator, and will add support for `PodMonitor`,
425+
`ServiceMonitor` and `Probe` objects.
423426

424427
Simply deploy a PodMonitor or a ServiceMonitor in the same namespace as the Grafana Agent and it will discover it and
425428
take the appropriate action.
426429

430+
Use a selector to limit the discovered objects
431+
432+
Example:
433+
434+
In this example, the Agent will find `ServiceMonitor` objects labeld with
435+
`example.com/environment=production`, scrape them for Prometheus metrics, and send those metrics to Grafana Cloud.
436+
437+
```yaml
438+
serviceMonitors:
439+
enabled: true
440+
selector: |-
441+
match_expression {
442+
key = "example.com/environment"
443+
operator = "In"
444+
values = ["production"]
445+
}
446+
447+
```
448+
427449
## Platform-specific instructions
428450

429451
### OpenShift

charts/k8s-monitoring/README.md.gotmpl

+21-2
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,31 @@ For an example values file and generated output, see [this example](../../exampl
145145

146146
### Using Prometheus Operator CRDs
147147

148-
The default config can deploy the CRDs for Prometheus Operator, and will add support for `PodMonitor` and
149-
`ServiceMonitor` objects.
148+
The default config can deploy the CRDs for Prometheus Operator, and will add support for `PodMonitor`,
149+
`ServiceMonitor` and `Probe` objects.
150150

151151
Simply deploy a PodMonitor or a ServiceMonitor in the same namespace as the Grafana Agent and it will discover it and
152152
take the appropriate action.
153153

154+
Use a selector to limit the discovered objects
155+
156+
Example:
157+
158+
In this example, the Agent will find `ServiceMonitor` objects labeld with
159+
`example.com/environment=production`, scrape them for Prometheus metrics, and send those metrics to Grafana Cloud.
160+
161+
```yaml
162+
serviceMonitors:
163+
enabled: true
164+
selector: |-
165+
match_expression {
166+
key = "example.com/environment"
167+
operator = "In"
168+
values = ["production"]
169+
}
170+
171+
```
172+
154173
## Platform-specific instructions
155174

156175
### OpenShift

charts/k8s-monitoring/templates/agent_config/_prometheus_operator_objects.txt

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
prometheus.operator.podmonitors "pod_monitors" {
44
namespaces = {{ .Values.metrics.podMonitors.namespaces | toJson }}
55
forward_to = [prometheus.relabel.podmonitors.receiver]
6+
{{- if .Values.metrics.podMonitors.selector }}
7+
selector {
8+
{{ .Values.metrics.podMonitors.selector | indent 4 }}
9+
}
10+
{{- end }}
611
{{- if (index .Values "grafana-agent").agent.clustering.enabled }}
712
clustering {
813
enabled = true
@@ -26,6 +31,11 @@ prometheus.relabel "podmonitors" {
2631
prometheus.operator.probes "probes" {
2732
namespaces = {{ .Values.metrics.probes.namespaces | toJson }}
2833
forward_to = [prometheus.relabel.probes.receiver]
34+
{{- if .Values.metrics.probes.selector }}
35+
selector {
36+
{{ .Values.metrics.probes.selector | indent 4 }}
37+
}
38+
{{- end }}
2939
{{- if (index .Values "grafana-agent").agent.clustering.enabled }}
3040
clustering {
3141
enabled = true
@@ -50,6 +60,11 @@ prometheus.relabel "probes" {
5060
prometheus.operator.servicemonitors "service_monitors" {
5161
namespaces = {{ .Values.metrics.serviceMonitors.namespaces | toJson }}
5262
forward_to = [prometheus.relabel.servicemonitors.receiver]
63+
{{- if .Values.metrics.serviceMonitors.selector }}
64+
selector {
65+
{{ .Values.metrics.serviceMonitors.selector | indent 4 }}
66+
}
67+
{{- end }}
5368
{{- if (index .Values "grafana-agent").agent.clustering.enabled }}
5469
clustering {
5570
enabled = true

charts/k8s-monitoring/values.schema.json

+9
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,9 @@
997997
},
998998
"scrapeInterval": {
999999
"type": "string"
1000+
},
1001+
"selector": {
1002+
"type": "string"
10001003
}
10011004
}
10021005
},
@@ -1014,6 +1017,9 @@
10141017
},
10151018
"scrapeInterval": {
10161019
"type": "string"
1020+
},
1021+
"selector": {
1022+
"type": "string"
10171023
}
10181024
}
10191025
},
@@ -1050,6 +1056,9 @@
10501056
},
10511057
"scrapeInterval": {
10521058
"type": "string"
1059+
},
1060+
"selector": {
1061+
"type": "string"
10531062
}
10541063
}
10551064
},

charts/k8s-monitoring/values.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,9 @@ metrics:
600600
# -- Rule blocks to be added to the prometheus.relabel component for PodMonitor objects. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block))
601601
extraMetricRelabelingRules: ""
602602

603+
# -- Which podMonitor objects to select
604+
selector: ""
605+
603606
probes:
604607
# -- Include service discovery for Probe objects.
605608
enabled: true
@@ -615,6 +618,9 @@ metrics:
615618
# -- Rule blocks to be added to the prometheus.relabel component for Probe objects. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block))
616619
extraMetricRelabelingRules: ""
617620

621+
# -- Which probe objects to select
622+
selector: ""
623+
618624
serviceMonitors:
619625
# -- Include service discovery for ServiceMonitor objects
620626
enabled: true
@@ -630,6 +636,9 @@ metrics:
630636
# -- Rule blocks to be added to the prometheus.relabel component for ServiceMonitor objects. ([docs](https://grafana.com/docs/agent/latest/flow/reference/components/prometheus.relabel/#rule-block))
631637
extraMetricRelabelingRules: ""
632638

639+
# -- Which serviceMonitors objects to select
640+
selector: ""
641+
633642
kubernetesMonitoring:
634643
# -- Report telemetry about this Kubernetes Monitoring chart as a metric.
635644
enabled: true

0 commit comments

Comments
 (0)