Search code examples
prometheuspromqlprometheus-operator

Prometheus Substring or Split Functionality


Is it possible to split the values of a label in a metric?

rows_inserted_total{job_name="maejcc100-telemetry-vm-6c96c655c6", type="opentdb"} rows_inserted_total{job_name="vacocc100-telemetry-vm-5d49ff7d9b", type="graphite"} rows_inserted_total{pod_name="azaacc100-telemetry-vm-7475f44758", type="graphite"} rows_inserted_total{job_name="fl06cc200-telemetry-vm-6c99b4f4cb", type="opentsdbhttp"}

The label job_name has a long text value. I want to use only the initial part “maejcc100” out of “maejcc100-telemetry-vm-6c96c655c6”. How can I achieve this? Is there a way to split the label value or use substring to get only a part of the long text?


Solution

  • The following query will create the "new_job_name" label with only the "job_name" prefix (maejcc100, vacocc100, etc):

    label_replace(rows_inserted_total, "new_job_name", "$1", "job_name", "(.+)-telemetry.+")