You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-monitor/containers/prometheus-metrics-scrape-configuration.md
+152-7Lines changed: 152 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,8 @@ This article provides instructions on customizing metrics scraping for a Kuberne
15
15
Four different configmaps can be configured to provide scrape configuration and other settings for the metrics add-on. All config-maps should be applied to `kube-system` namespace for any cluster.
16
16
17
17
> [!NOTE]
18
-
> None of the four configmaps exist by default in the cluster when Managed Prometheus is enabled. Depending on what needs to be customized, you need to deploy any or all of these four configmaps with the same name specified, in `kube-system` namespace. AMA-Metrics pods will pick up these configmaps after you deploy them to `kube-system` namespace, and will restart in 2-3 minutes to apply the configuration settings specified in the configmap(s).
18
+
> None of the four configmaps exist by default in the cluster when Managed Prometheus is enabled. Depending on what needs to be customized, you need to deploy any or all of these four configmaps with the same name specified, in `kube-system` namespace. AMA-Metrics pods will pick up these configmaps after you deploy them to `kube-system` namespace, and will restart in 2-3 minutes to apply the configuration settings specified in the configmap(s).
This config map has below simple settings that can be configured. You can take the configmap from the above git hub repo, change the settings are required and apply/deploy the configmap to `kube-system` namespace for your cluster
@@ -25,15 +26,20 @@ Four different configmaps can be configured to provide scrape configuration and
25
26
* metric keep-lists - this setting is used to control which metrics are listed to be allowed from each default target and to change the default behavior
26
27
* scrape intervals for default/pre-definetargets. `30 secs` is the default scrape frequency and it can be changed per default target using this configmap
27
28
* debug-mode - turning this ON helps to debug missing metric/ingestion issues - see more on [troubleshooting](prometheus-metrics-troubleshoot.md#debug-mode)
This config map can be used to provide Prometheus scrape config for addon replica. Addon runs a singleton replica, and any cluster level services can be discovered and scraped by providing scrape jobs in this configmap. You can take the sample configmap from the above git hub repo, add scrape jobs that you would need and apply/deploy the config map to `kube-system` namespace for your cluster.
31
+
**Although this is supported, please note that the recommended way of scraping custom targets is using [custom resources](prometheus-metrics-scrape-configuration.md#custom-resource-definitions)**
This config map can be used to provide Prometheus scrape config for addon DaemonSet that runs on every **Linux** node in the cluster, and any node level targets on each node can be scraped by providing scrape jobs in this configmap. When you use this configmap, you can use `$NODE_IP` variable in your scrape config, which gets substituted by corresponding node's ip address in DaemonSet pod running on each node. This way you get access to scrape anything that runs on that node from the metrics addon DaemonSet. **Please be careful when you use discoveries in scrape config in this node level config map, as every node in the cluster will setup & discover the target(s) and will collect redundant metrics**.
32
34
You can take the sample configmap from the above git hub repo, add scrape jobs that you would need and apply/deploy the config map to `kube-system` namespace for your cluster
This config map can be used to provide Prometheus scrape config for addon DaemonSet that runs on every **Windows** node in the cluster, and node level targets on each node can be scraped by providing scrape jobs in this configmap. When you use this configmap, you can use `$NODE_IP` variable in your scrape config, which will be substituted by corresponding node's ip address in DaemonSet pod running on each node. This way you get access to scrape anything that runs on that node from the metrics addon DaemonSet. **Please be careful when you use discoveries in scrape config in this node level config map, as every node in the cluster will setup & discover the target(s) and will collect redundant metrics**.
35
37
You can take the sample configmap from the above git hub repo, add scrape jobs that you would need and apply/deploy the config map to `kube-system` namespace for your cluster
36
38
39
+
## Custom Resource Definitions
40
+
The Azure Monitor metrics add-on supports scraping Prometheus metrics using Prometheus - Pod Monitors and Service Monitors, similar to the OSS Prometheus operator. Enabling the add-on will deploy the Pod and Service Monitor custom resource definitions to allow you to create your own custom resources.
41
+
Follow the instructions to [create and apply custom resources](prometheus-metrics-scrape-crd.md) on your cluster.
42
+
37
43
## Metrics add-on settings configmap
38
44
39
45
The [ama-metrics-settings-configmap](https://aka.ms/azureprometheus-addon-settings-configmap) can be downloaded, edited, and applied to the cluster to customize the out-of-the-box features of the metrics add-on.
@@ -142,17 +148,21 @@ and apply the YAML using the following command: `kubectl apply -f .\ama-metrics-
142
148
143
149
## Configure custom Prometheus scrape jobs
144
150
145
-
You can configure the metrics add-on to scrape targets other than the default ones by using the same configuration format as the [Prometheus configuration file](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#configuration-file).
146
-
147
-
Follow the instructions to [create, validate, and apply the configmap](prometheus-metrics-scrape-validate.md) for your cluster.
148
-
151
+
You can scrape Prometheus metrics using Prometheus - Pod Monitors and Service Monitors(**Recommended**), similar to the OSS Prometheus operator.
152
+
Follow the instructions to [create and apply custom resources](prometheus-metrics-scrape-crd.md) on your cluster.
149
153
154
+
Additionally, you can follow the instructions to [create, validate, and apply the configmap](prometheus-metrics-scrape-validate.md) for your cluster.
155
+
The configuration format is similar to [Prometheus configuration file](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#configuration-file).
150
156
151
157
## Prometheus configuration tips and examples
152
158
153
159
Learn some tips from examples in this section.
154
160
155
-
### Configuration file for custom scrape config
161
+
### [Configuration using CRD for custom scrape config](#tab/CRDConfig)
162
+
Use the [Pod and Service Monitor templates](https://github.com/Azure/prometheus-collector/tree/main/otelcollector/customresources) and follow the API specification to create your custom resources([PodMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#podmonitor) and [Service Monitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor)). **Note** that the only change required to the existing OSS CRs for being picked up by the Managed Prometheus is the API group - **azmonitoring.coreos.com/v1**. See [here](prometheus-metrics-scrape-crd.md) to learn more
163
+
164
+
165
+
### [Configuration file for custom scrape config](#tab/ConfigFile)
156
166
157
167
The configuration format is the same as the [Prometheus configuration file](https://aka.ms/azureprometheus-promioconfig). Currently, the following sections are supported:
158
168
@@ -172,10 +182,143 @@ Any other unsupported sections must be removed from the config before they're ap
172
182
173
183
See the [Apply config file](prometheus-metrics-scrape-validate.md#deploy-config-file-as-configmap) section to create a configmap from the Prometheus config.
174
184
185
+
---
186
+
175
187
> [!NOTE]
176
188
> When custom scrape configuration fails to apply because of validation errors, default scrape configuration continues to be used.
177
189
190
+
> If you want to use global settings that apply to all the scrape jobs, and only have [Custom Resources](prometheus-metrics-scrape-crd.md) you would still need to create a configmap with just the global settings(Settings for each of these in the custom resources will override the ones in the global section)
191
+
192
+
178
193
## Scrape configs
194
+
### [Scrape Configs using CRD](#tab/CRDScrapeConfig)
195
+
Currently, the supported methods of target discovery for custom resources are pod and service monitor
196
+
197
+
#### Pod and Service Monitors
198
+
Targets discovered using pod and service monitors have different `__meta_*` labels depending on what monitor is used. You can use the labels in the `relabelings` section to filter targets or replace labels for the targets.
199
+
200
+
See the [Pod and Service Monitor examples](https://github.com/Azure/prometheus-collector/tree/main/otelcollector/deploy/example-custom-resources) of pod and service monitors.
201
+
202
+
### Relabelings
203
+
The `relabelings` section is applied at the time of target discovery and applies to each target for the job. The following examples show ways to use `relabelings`.
204
+
205
+
#### Add a label
206
+
Add a new label called `example_label` with the value `example_value` to every metric of the job. Use `__address__` as the source label only because that label always exists and adds the label for every target of the job.
207
+
208
+
```yaml
209
+
relabelings:
210
+
- sourceLabels: [__address__]
211
+
targetLabel: example_label
212
+
replacement: 'example_value'
213
+
```
214
+
215
+
#### Use Pod or Service Monitor labels
216
+
217
+
Targets discovered using pod and service monitors have different `__meta_*` labels depending on what monitor is used. The `__*` labels are dropped after discovering the targets. To filter by using them at the metrics level, first keep them using `relabelings` by assigning a label name. Then use `metricRelabelings` to filter.
218
+
219
+
```yaml
220
+
# Use the kubernetes namespace as a label called 'kubernetes_namespace'
221
+
relabelings:
222
+
- sourceLabels: [__meta_kubernetes_namespace]
223
+
action: replace
224
+
targetLabel: kubernetes_namespace
225
+
226
+
# Keep only metrics with the kubernetes namespace 'default'
227
+
metricRelabelings:
228
+
- sourceLabels: [kubernetes_namespace]
229
+
action: keep
230
+
regex: 'default'
231
+
```
232
+
233
+
#### Job and instance relabeling
234
+
235
+
You can change the `job` and `instance` label values based on the source label, just like any other label.
236
+
237
+
```yaml
238
+
# Replace the job name with the pod label 'k8s app'
# Replace the instance name with the node name. This is helpful to replace a node IP
244
+
# and port with a value that is more readable
245
+
relabelings:
246
+
- sourceLabels: [__meta_kubernetes_node_name]]
247
+
targetLabel: instance
248
+
```
249
+
250
+
### Metric Relabelings
251
+
252
+
Metric relabelings are applied after scraping and before ingestion. Use the `metricRelabelings` section to filter metrics after scraping. The following examples show how to do so.
253
+
254
+
#### Drop metrics by name
255
+
256
+
```yaml
257
+
# Drop the metric named 'example_metric_name'
258
+
metricRelabelings:
259
+
- sourceLabels: [__name__]
260
+
action: drop
261
+
regex: 'example_metric_name'
262
+
```
263
+
264
+
#### Keep only certain metrics by name
265
+
266
+
```yaml
267
+
# Keep only the metric named 'example_metric_name'
268
+
metricRelabelings:
269
+
- sourceLabels: [__name__]
270
+
action: keep
271
+
regex: 'example_metric_name'
272
+
```
273
+
274
+
```yaml
275
+
# Keep only metrics that start with 'example_'
276
+
metricRelabelings:
277
+
- sourceLabels: [__name__]
278
+
action: keep
279
+
regex: '(example_.*)'
280
+
```
281
+
282
+
#### Rename metrics
283
+
Metric renaming isn't supported.
284
+
285
+
#### Filter metrics by labels
286
+
287
+
```yaml
288
+
# Keep metrics only where example_label = 'example'
289
+
metricRelabelings:
290
+
- sourceLabels: [example_label]
291
+
action: keep
292
+
regex: 'example'
293
+
```
294
+
295
+
```yaml
296
+
# Keep metrics only if `example_label` equals `value_1` or `value_2`
297
+
metricRelabelings:
298
+
- sourceLabels: [example_label]
299
+
action: keep
300
+
regex: '(value_1|value_2)'
301
+
```
302
+
303
+
```yaml
304
+
# Keep metrics only if `example_label_1 = value_1` and `example_label_2 = value_2`
# Keep metrics only if `example_label` exists as a label
314
+
metricRelabelings:
315
+
- sourceLabels: [example_label_1]
316
+
action: keep
317
+
regex: '.+'
318
+
```
319
+
320
+
321
+
### [Scrape Configs using Config file](#tab/ConfigFileScrapeConfig)
179
322
Currently, the supported methods of target discovery for a [scrape config](https://aka.ms/azureprometheus-promioconfig-scrape) are either [`static_configs`](https://aka.ms/azureprometheus-promioconfig-static) or [`kubernetes_sd_configs`](https://aka.ms/azureprometheus-promioconfig-sdk8s) for specifying or discovering targets.
180
323
181
324
#### Static config
@@ -313,6 +456,8 @@ metric_relabel_configs:
313
456
regex: '.+'
314
457
```
315
458
459
+
---
460
+
316
461
### TLS based scraping
317
462
318
463
If you have a Prometheus instance served with TLS and you want to scrape metrics from it, you need to set scheme to `https` and set the TLS settings in your configmap or respective CRD. You can use the `tls_config` configuration property inside a custom scrape job to configure the TLS settings either using a CRD or a configmap. You need to provide a CA certificate to validate API server certificate with. The CA certificate is used to verify the authenticity of the server's certificate when Prometheus connects to the target over TLS. It helps ensure that the server's certificate is signed by a trusted authority.
title: Create and apply Pod and Service Monitors for Prometheus metrics in Azure Monitor
3
+
description: Describes how to create and apply pod and service monitors to scrape Prometheus metrics in Azure Monitor to Kubernetes cluster.
4
+
ms.topic: conceptual
5
+
ms.date: 3/13/2024
6
+
ms.reviewer: aul
7
+
---
8
+
# Custom Resource Definitions
9
+
The enablement of managed prometheus automatically deploys the custom resource definitions (CRD) for [pod monitors](https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/addon-chart/azure-monitor-metrics-addon/templates/ama-metrics-podmonitor-crd.yaml) and [service monitors](https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/addon-chart/azure-monitor-metrics-addon/templates/ama-metrics-servicemonitor-crd.yaml). These custom resource definitions are the same custom resource definitions (CRD) as [OSS Pod monitors](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.PodMonitor) and [OSS service monitors](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor) for Prometheus, except for a change in the group name. If you have existing Prometheus CRDs and custom resources on your cluster, these CRDs won't conflict with the CRDs created by the add-on. At the same time, the managed Prometheus addon does not pick up the CRDs created for the OSS Prometheus. This separation is intentional for the purposes of isolation of scrape jobs.
10
+
11
+
### Create a Pod or Service Monitor
12
+
Use the [Pod and Service Monitor templates](https://github.com/Azure/prometheus-collector/tree/main/otelcollector/customresources) and follow the API specification to create your custom resources([PodMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#podmonitor) and [Service Monitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor)). **Note** that the only change required to the existing OSS CRs(Custom Resources) for being picked up by the Managed Prometheus is the API group - **azmonitoring.coreos.com/v1**.
13
+
>Note - Please make sure to use the **labelLimit, labelNameLengthLimit and labelValueLengthLimit** specified in the templates so that they are not dropped during processing.
14
+
15
+
Your pod and service monitors should look like the following examples:
16
+
17
+
#### Example Pod Monitor -
18
+
19
+
```yaml
20
+
# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
21
+
apiVersion: azmonitoring.coreos.com/v1
22
+
kind: PodMonitor
23
+
24
+
# Can be deployed in any namespace
25
+
metadata:
26
+
name: reference-app
27
+
namespace: app-namespace
28
+
spec:
29
+
labelLimit: 63
30
+
labelNameLengthLimit: 511
31
+
labelValueLengthLimit: 1023
32
+
33
+
# The selector specifies which pods to filter for
34
+
selector:
35
+
36
+
# Filter by pod labels
37
+
matchLabels:
38
+
environment: test
39
+
matchExpressions:
40
+
- key: app
41
+
operator: In
42
+
values: [app-frontend, app-backend]
43
+
44
+
# [Optional] Filter by pod namespace
45
+
namespaceSelector:
46
+
matchNames: [app-frontend, app-backend]
47
+
48
+
# [Optional] Labels on the pod with these keys will be added as labels to each metric scraped
49
+
podTargetLabels: [app, region, environment]
50
+
51
+
# Multiple pod endpoints can be specified. Port requires a named port.
52
+
podMetricsEndpoints:
53
+
- port: metrics
54
+
```
55
+
#### Example Service Monitor -
56
+
```yaml
57
+
# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
58
+
apiVersion: azmonitoring.coreos.com/v1
59
+
kind: ServiceMonitor
60
+
61
+
# Can be deployed in any namespace
62
+
metadata:
63
+
name: reference-app
64
+
namespace: app-namespace
65
+
spec:
66
+
labelLimit: 63
67
+
labelNameLengthLimit: 511
68
+
labelValueLengthLimit: 1023
69
+
70
+
# The selector filters endpoints by service labels.
71
+
selector:
72
+
matchLabels:
73
+
app: reference-app
74
+
75
+
# Multiple endpoints can be specified. Port requires a named port.
76
+
endpoints:
77
+
- port: metrics
78
+
```
79
+
80
+
### Deploy a Pod or Service Monitor
81
+
You can then deploy the pod or service monitor using kubectl apply.
82
+
83
+
84
+
When applied, any errors in the custom resources should show up and the pod or service monitors should fail to apply.
85
+
A successful pod monitor creation looks like the following -
86
+
```bash
87
+
podmonitor.azmonitoring.coreos.com/my-pod-monitor created
88
+
```
89
+
90
+
### Examples
91
+
#### Create a sample application
92
+
Deploy a sample application exposing prometheus metrics to be configured by pod/service monitor.
When the pod or service monitors are successfully applied, if you want to make sure that the pod or service monitor targets get picked up by the addon, follow the instructions [here](prometheus-metrics-troubleshoot.md#prometheus-interface) for general troubleshooting of custom resources and also to ensure the targets show up in 127.0.0.1/targets.
113
+
114
+
:::image type="content" source="media/prometheus-metrics-troubleshoot/image-pod-service-monitor.png" alt-text="Screenshot showing targets for pod/service monitor" lightbox="media/prometheus-metrics-troubleshoot/image-pod-service-monitor.png":::
115
+
116
+
## Next steps
117
+
118
+
-[Learn more about collecting Prometheus metrics](../essentials/prometheus-metrics-overview.md).
0 commit comments