iGEL
iGEL

Reputation: 17392

Prometheus: Add a label based on other labels

I've set up Prometheus to use the file sd from prometheus-ecs-discovery:

scrape_configs:
- job_name: ecs
  file_sd_configs:
    - files:
      - /prometheus/ecs_file_sd.yml

This adds among other labels the task arn as a label:

container_label_com_amazonaws_ecs_task_arn=
"arn:aws:ecs:us-west-1:xxxxxx:task/2c1655cd-36b7-4db9-4326-ee90537b6271"

In grafana, I'd like to use the task ID (2c1655cd-36b7-4db9-4326-ee90537b6271 in my example) as the legend for most of my stats, which can be extracted from the task arn. Can I somehow add a new label?

Upvotes: 2

Views: 8634

Answers (2)

Mark Tickner
Mark Tickner

Reputation: 1053

Though I don't think it solves the problem for the OP, I came across this question as the title is similar to what I was stuck on.

I had labels, eg: label_1="blue" and label_2="green" and I wanted to add a new label (label_3), using the value of label_2 with a regex match on label_1.

This could be achieved using relabel_configs with multiple source_labels https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config

- source_labels: [label_1, label_2]
  separator: ';'
  target_label: label_3
  regex: "blue;(.+)"
  replacement: '${2}'
  action: replace

Upvotes: 1

yamenk
yamenk

Reputation: 51768

This can be acheived using a relabel_configs. This will allow you to create a new target label from a source label.

scrape_configs:
- job_name: ecs
  file_sd_configs:
    - files:
      - /prometheus/ecs_file_sd.yml
  relabel_configs:
    - source_labels: [container_label_com_amazonaws_ecs_task_arn]
      regex: '.*\/(.*)'
      replacement: '${1}'
      target_label: task_id

The above will create a new label named task_id with value being the part after the / of the container_label_com_amazonaws_ecs_task_arn label.

Upvotes: 2

Related Questions