label_replace - extract string from label_value

2,448 views
Skip to first unread message

Nabil L.

unread,
Feb 1, 2021, 3:10:14 PM2/1/21
to Prometheus Users
Hi Folks,

I wonder if someone know how to extract a string from a label_value  and using the the label_replace .

For example I have the following metric, which return

kube_pod_container_info which return the following metric:

{container="d2conf", container_id="docker://9e9fdd34fa960000abbf309c279a19fcae828646a7b11169292c7da05e3349c9", image_id="docker-pullable://docker-registry/toto-jx/deploy-ws@sha256:d6c2db22b2b677a7c6017d6f3eeb4055750750349a48b716d6215721ccce1aa8", instance="kube-state-metrics.kube-system.svc.cluster.local:8080", job="kube-state-metrics", namespace="toto", pod="d2conf-1", image="docker-registry/toto/deploy-ws:1.4.2"}    

Using  the label_replace, the label image is replaced by release, but what I need as well is to extract the string after character ":" in the label value (below in yellow)

sum without (image) (label_replace(kube_pod_container_info, "release", "$1", "image", "(.*)"))


{container="d2conf", container_id="docker://9e9fdd34fa960000abbf309c279a19fcae828646a7b11169292c7da05e3349c9", image_id="docker-pullable://docker-registry/toto-jx/deploy-ws@sha256:d6c2db22b2b677a7c6017d6f3eeb4055750750349a48b716d6215721ccce1aa8", instance="kube-state-metrics.kube-system.svc.cluster.local:8080", job="kube-state-metrics", namespace="toto", pod="d2conf-1", release="docker-registry/toto/deploy-ws:1.4.2"}  


any idea?

Thanks in advance
Neb.

Nabil L.

unread,
Feb 2, 2021, 2:33:10 AM2/2/21
to Prometheus Users
Any help ? :)

Matthias Rieber

unread,
Feb 2, 2021, 2:52:07 AM2/2/21
to Prometheus Users
Hello,

On Mon, 1 Feb 2021, Nabil L. wrote:

> Any help ? :)

maybe:

sum without (image) (label_replace(kube_pod_container_info, "release", "$1", "image", ".*:([^:]*)"))

Regards,
Matthias

>
> Le lundi 1 février 2021 à 21:10:14 UTC+1, Nabil L. a écrit :
>
>> Hi Folks,
>>
>> I wonder if someone know how to extract a string from a label_value and
>> using the the label_replace .
>>
>> For example I have the following metric, which return
>>
>> *kube_pod_container_info* which return the following metric:
>>
>> {container="d2conf",
>> container_id="docker://9e9fdd34fa960000abbf309c279a19fcae828646a7b11169292c7da05e3349c9",
>> image_id="docker-pullable://docker-registry/toto-jx/deploy-ws@sha256:d6c2db22b2b677a7c6017d6f3eeb4055750750349a48b716d6215721ccce1aa8",
>> instance="kube-state-metrics.kube-system.svc.cluster.local:8080",
>> job="kube-state-metrics", namespace="toto", pod="d2conf-1",
>> image="docker-registry/toto/deploy-ws:1.4.2"}
>>
>> Using the label_replace, the label *image *is replaced by *release*, but
>> what I need as well is to extract the string after character "*:*" in the
>> label value (below in yellow)
>>
>> sum without (image) (label_replace(kube_pod_container_info, "release",
>> "$1", "image", "(.*)"))
>>
>>
>> {container="d2conf",
>> container_id="docker://9e9fdd34fa960000abbf309c279a19fcae828646a7b11169292c7da05e3349c9",
>> image_id="docker-pullable://docker-registry/toto-jx/deploy-ws@sha256:d6c2db22b2b677a7c6017d6f3eeb4055750750349a48b716d6215721ccce1aa8",
>> instance="kube-state-metrics.kube-system.svc.cluster.local:8080",
>> job="kube-state-metrics", namespace="toto", pod="d2conf-1", *release*
>> ="docker-registry/toto/deploy-ws:1.4.2"}
>>
>>
>> any idea?
>>
>> Thanks in advance
>> Neb.
>>
>
> --
> You received this message because you are subscribed to the Google Groups "Prometheus Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-use...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/4acbe3b4-a5c3-4655-adf6-53b0e8b15fe4n%40googlegroups.com.
>


Nabil L.

unread,
Feb 4, 2021, 7:52:57 AM2/4/21
to Prometheus Users
Hi Matthias,

This is exactly what I am looking for!

 Thanks a lot for your help!

Regards,
Neb

Reply all
Reply to author
Forward
0 new messages