Ngọc Bình
Ngọc Bình

Reputation: 87

Metrics sum of label in rate or irate

I'm calculate CPU usage of process by process-exporter, but it have two different label in one metric for example:

namedprocess_namegroup_cpu_seconds_total{groupname="(sd-pam)",instance="localhost:9256",job="process_exporter",mode="system"}
namedprocess_namegroup_cpu_seconds_total{groupname="(sd-pam)",instance="localhost:9256",job="process_exporter",mode="user"}

i need sum two of it to have % cpu usage of process.

i have tried with this, but the code won't show anything

topk(5,
rate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="user"}[20s]) + 
rate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="system"}[20s])
or 
(
irate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="user"}[5m]) +
irate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="system"}[5m])))

but it work with this (when i change all to user or system)

topk(5,
rate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="system"}[20s]) + 
rate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="system"}[20s])
or 
(
irate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="system"}[5m]) +
irate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host",mode="system"}[5m])))

What should i do to have sum two of this, thank you.

Upvotes: 0

Views: 3157

Answers (1)

Oliver
Oliver

Reputation: 13031

You can use regular expressions when label matching to aggregate the two time series. It would look something like this:

sum(rate(namedprocess_namegroup_cpu_seconds_total{groupname=~"$processes",instance="$host", mode=~"system|user"}[1m])) by (groupname, instance)

The above gives you the total CPU used by system and user, aggregated by groupname and instance.

Upvotes: 1

Related Questions