After you enable observability in a cluster and install the prometheus-agent add-on in the cluster, you can use a PodMonitor to configure custom collection rules. This topic describes how to configure service discovery by using a PodMonitor.
You can use the following demo code to integrate a Go application and use a PodMonitor for service discovery. In the following example, Go is used to create an application, and a metric named rpc_durations_seconds is exposed in the application to collect statistics on the RPC latency of the service.
Step 1: Deploy the application
You can use the image from CR to deploy the application in the target VKE cluster. The steps are as follows:
- In the left navigation bar, click Cluster. On the page that appears, click the name of the target cluster to go to the configuration page of the cluster.
- In the left navigation bar, click Workload > Deployment. Click Create with Yaml, and deploy the application.
tip
The image address in this example is only for demonstration purposes. You need to replace it with your actual image address.
Step 2: Configure a PodMonitor
- In the left navigation bar, click Workload > Resource Explorer. Click Create with Yaml and configure a PodMonitor.
- Select Custom from the Type drop-down menu.
- Edit the configurations in the Yaml box.
- In the top navigation bar, choose the target region.
- In the left navigation bar, click Explore to go to the Explore page.
- In the upper-right corner, specify the Workspace from which metrics will be queried.In the Query text box, enter up{job="volcano-metrics/golang-demo-discover"} to verify if the corresponding pod has been discovered as expected.
Specify a custom name for the job
By default, the name of the job discovered through the PodMonitor is <namespace>/<name>. In this example, the default job name is volcano-metrics/golang-demo-discover. You can specify a custom name for the job based on your business requirements.
You can use the jobLabel field in the PodMonitor to change the job name to the value of a specified pod label. In this example, you can change the job name to the value of the label app: golang-demo. As a result, the job name is golang-demo.
The expected result is as follows: You can query metrics by using the job name golang-demo.
tip
This method prevails over Method 1.
You can use the spec.podMetricsEndpoints.N.relabelings field in the PodMonitor to configure relabeling and specify a custom job name.
The expected result is as follows: You can query metrics by using the job name jobname-demo.
Adding pod labels to metrics
By default, pod labels are not added to metrics. You can use the spec.podTargetLabels field in the PodMonitor to add pod labels to metrics.
The expected result is as follows:
warning
Prometheus metric labels can only contain uppercase and lowercase letters, digits, and underscores (_). Therefore, any other characters in the pod label name will be automatically replaced with underscores (_). For example, app.kubernetes.io/component will change to app_kubernetes_io_component.
You can use metric relabeling to add, modify, or delete metric labels before metrics are collected or pushed to managed Prometheus workspaces. You can configure metric relabeling by using the following two methods:
- Modify or filter target labels before target collection.
- Modify or filter target labels during service discovery.
- Modify or filter metric labels after data collection.
- Delete specific metrics after data collection.
- Typical scenarios include deleting unnecessary metrics, renaming metric labels, and adding new metric labels.
If you want to add a specified label to all collected metrics, you can specify the spec.podMetricsEndpoints.N.relabelings parameter. For example, use the following configuration to add the label key-value pair label: value to all collected metrics.
Retaining specified metrics
If you want to write only specified metrics, you can configure action: keep. For example, use the following configuration to retain only metrics whose names match the regular expression vmagent_remotewrite.*.
If you do not want to write some metrics, you can discard these metrics by configuring action: drop. For example, use the following configuration to discard the vmagent_daily_series_limit_rows_dropped_total metric.
Modifying the value of a label
You can configure action: replace to modify the value of a label. For example, use the following configuration to remove the /api/v1 prefix for the path label value. As a result, path: /api/v1/targets will change to path: /targets.