How-To: Observe metrics with Prometheus

Use Prometheus to collect time-series data relating to the execution of the Dapr runtime itself

Setup Prometheus Locally

To run Prometheus on your local machine, you can either install and run it as a process or run it as a Docker container.

Install

Note

You don’t need to install Prometheus if you plan to run it as a Docker container. Please refer to the Container instructions.

To install Prometheus, follow the steps outlined here for your OS.

Configure

Now you’ve installed Prometheus, you need to create a configuration.

Below is an example Prometheus configuration, save this to a file i.e. /tmp/prometheus.yml or C:\Temp\prometheus.yml

  1. global:
  2. scrape_interval: 15s # By default, scrape targets every 15 seconds.
  3. # A scrape configuration containing exactly one endpoint to scrape:
  4. # Here it's Prometheus itself.
  5. scrape_configs:
  6. - job_name: 'dapr'
  7. # Override the global default and scrape targets from this job every 5 seconds.
  8. scrape_interval: 5s
  9. static_configs:
  10. - targets: ['localhost:9090'] # Replace with Dapr metrics port if not default

Run as Process

Run Prometheus with your configuration to start it collecting metrics from the specified targets.

  1. ./prometheus --config.file=/tmp/prometheus.yml --web.listen-address=:8080

We change the port so it doesn’t conflict with Dapr’s own metrics endpoint.

If you are not currently running a Dapr application, the target will show as offline. In order to start collecting metrics you must start Dapr with the metrics port matching the one provided as the target in the configuration.

Once Prometheus is running, you’ll be able to visit its dashboard by visiting http://localhost:8080.

Run as Container

To run Prometheus as a Docker container on your local machine, first ensure you have Docker installed and running.

Then you can run Prometheus as a Docker container using:

  1. docker run \
  2. --net=host \
  3. -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml \
  4. prom/prometheus --config.file=/etc/prometheus/prometheus.yml --web.listen-address=:8080

--net=host ensures that the Prometheus instance will be able to connect to any Dapr instances running on the host machine. If you plan to run your Dapr apps in containers as well, you’ll need to run them on a shared Docker network and update the configuration with the correct target address.

Once Prometheus is running, you’ll be able to visit its dashboard by visiting http://localhost:8080.

Setup Prometheus on Kubernetes

Prerequisites

Install Prometheus

  1. First create namespace that can be used to deploy the Grafana and Prometheus monitoring tools
  1. kubectl create namespace dapr-monitoring
  1. Install Prometheus
  1. helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
  2. helm repo update
  3. helm install dapr-prom prometheus-community/prometheus -n dapr-monitoring

If you are Minikube user or want to disable persistent volume for development purposes, you can disable it by using the following command.

  1. helm install dapr-prom prometheus-community/prometheus -n dapr-monitoring
  2. --set alertmanager.persistence.enabled=false --set pushgateway.persistentVolume.enabled=false --set server.persistentVolume.enabled=false

For automatic discovery of Dapr targets (Service Discovery), use:

  1. helm install dapr-prom prometheus-community/prometheus -f values.yaml -n dapr-monitoring --create-namespace

values.yaml File

  1. alertmanager:
  2. persistence:
  3. enabled: false
  4. pushgateway:
  5. persistentVolume:
  6. enabled: false
  7. server:
  8. persistentVolume:
  9. enabled: false
  10. # Adds additional scrape configurations to prometheus.yml
  11. # Uses service discovery to find Dapr and Dapr sidecar targets
  12. extraScrapeConfigs: |-
  13. - job_name: dapr-sidecars
  14. kubernetes_sd_configs:
  15. - role: pod
  16. relabel_configs:
  17. - action: keep
  18. regex: "true"
  19. source_labels:
  20. - __meta_kubernetes_pod_annotation_dapr_io_enabled
  21. - action: keep
  22. regex: "true"
  23. source_labels:
  24. - __meta_kubernetes_pod_annotation_dapr_io_enable_metrics
  25. - action: replace
  26. replacement: ${1}
  27. source_labels:
  28. - __meta_kubernetes_namespace
  29. target_label: namespace
  30. - action: replace
  31. replacement: ${1}
  32. source_labels:
  33. - __meta_kubernetes_pod_name
  34. target_label: pod
  35. - action: replace
  36. regex: (.*);daprd
  37. replacement: ${1}-dapr
  38. source_labels:
  39. - __meta_kubernetes_pod_annotation_dapr_io_app_id
  40. - __meta_kubernetes_pod_container_name
  41. target_label: service
  42. - action: replace
  43. replacement: ${1}:9090
  44. source_labels:
  45. - __meta_kubernetes_pod_ip
  46. target_label: __address__
  47. - job_name: dapr
  48. kubernetes_sd_configs:
  49. - role: pod
  50. relabel_configs:
  51. - action: keep
  52. regex: dapr
  53. source_labels:
  54. - __meta_kubernetes_pod_label_app_kubernetes_io_name
  55. - action: keep
  56. regex: dapr
  57. source_labels:
  58. - __meta_kubernetes_pod_label_app_kubernetes_io_part_of
  59. - action: replace
  60. replacement: ${1}
  61. source_labels:
  62. - __meta_kubernetes_pod_label_app
  63. target_label: app
  64. - action: replace
  65. replacement: ${1}
  66. source_labels:
  67. - __meta_kubernetes_namespace
  68. target_label: namespace
  69. - action: replace
  70. replacement: ${1}
  71. source_labels:
  72. - __meta_kubernetes_pod_name
  73. target_label: pod
  74. - action: replace
  75. replacement: ${1}:9090
  76. source_labels:
  77. - __meta_kubernetes_pod_ip
  78. target_label: __address__
  1. Validation

Ensure Prometheus is running in your cluster.

  1. kubectl get pods -n dapr-monitoring

Expected output:

  1. NAME READY STATUS RESTARTS AGE
  2. dapr-prom-kube-state-metrics-9849d6cc6-t94p8 1/1 Running 0 4m58s
  3. dapr-prom-prometheus-alertmanager-749cc46f6-9b5t8 2/2 Running 0 4m58s
  4. dapr-prom-prometheus-node-exporter-5jh8p 1/1 Running 0 4m58s
  5. dapr-prom-prometheus-node-exporter-88gbg 1/1 Running 0 4m58s
  6. dapr-prom-prometheus-node-exporter-bjp9f 1/1 Running 0 4m58s
  7. dapr-prom-prometheus-pushgateway-688665d597-h4xx2 1/1 Running 0 4m58s
  8. dapr-prom-prometheus-server-694fd8d7c-q5d59 2/2 Running 0 4m58s

Access the Prometheus Dashboard

To view the Prometheus dashboard and check service discovery:

  1. kubectl port-forward svc/dapr-prom-prometheus-server 9090:80 -n dapr-monitoring

Open a browser and visit http://localhost:9090. Navigate to Status > Service Discovery to verify that the Dapr targets are discovered correctly.

Prometheus Web UI

You can see the job_name and its discovered targets.

Prometheus Service Discovery

Example

References

Last modified October 11, 2024: Fixed typo (#4389) (fe17926)