Anthony
Anthony

Reputation: 35928

How to change the port number using which prometheus scrapes metrcs

I have a pod with replication factor of 3. The app puts metrics on port 9001. I would like prometheus operator to scrape the metrics. I have the following in my values.yaml when using stable/prometheus-operator helm chart.

prometheus:
  prometheusSpec:
    additionalScrapeConfigs:
    - job_name: 'akka-metrics'
      scrape_interval: 15s
      kubernetes_sd_configs:
      - role: pod
        namespaces:
          names:
          - default
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_container_name]
        action: keep   
        regex: 'my_pod_name.*'

Using the setting above, I can see the pod in /target but it is trying to get metrics from port 2551 and 8558. I would like to change on of these ports to 9001 (the actual port where my app spits metrics). I am wondering how I can do that?

Upvotes: 4

Views: 8631

Answers (1)

weibeld
weibeld

Reputation: 15232

You should be able to do this with a relabelling rule:

- job_name: 'akka-metrics'
      scrape_interval: 15s
      kubernetes_sd_configs:
      - role: pod
        namespaces:
          names:
          - default
      relabel_configs:
      - source_labels: [__meta_kubernetes_pod_container_name]
        action: keep   
        regex: 'my_pod_name.*'
      - source_labels: [__address__]
        action: replace
        regex: ([^:]+):.*
        replacement: $1:9001
        target_label: __address__

The last rule modifies the __address__ label of the target. It extracts the IP address and sets the port to 9001. This should cause Prometheus to always use <ip>:9001 as the target.

Something similar is used in the official example scrape config for Kubernetes.

Upvotes: 5

Related Questions