prometheus-api-client-python icon indicating copy to clipboard operation
prometheus-api-client-python copied to clipboard

Can the metrics_name be in the format of an expression?

Open lynBIT opened this issue 3 years ago • 2 comments

Describe the solution you'd like
I want to get the value of the computed expression through Prometheus' API。 pc.get_metric_range_data(metric_name=train_data[0].metric_name ....) Can I make metric_name = (1- sum(increase(node_cpu_seconds_total{mode='idle',instance='172.29.3.13:9101'}[2m])) by (instance)/sum(increase(node_cpu_seconds_total{instance='172.29.3.13:9101'}[2m])) by (instance)) *100 The calculated metrics

Additional context

image

image

lynBIT avatar Apr 15 '22 08:04 lynBIT

Hi @lynBIT, thanks for opening an issue :smile:

I believe it is possible, but using custom_query_range instead of get_metric_range_data. I was able to run a query similar to yours on the robustperception.io demo prometheus instance:

start_time = parse_datetime("3d")
end_time = parse_datetime("now")

query = '1 - sum(increase(node_cpu_seconds_total{mode="idle"}[5m])) by (instance) / sum(increase(node_cpu_seconds_total[5m])) by (instance)'

metric_data = pc.custom_query_range(
    query,
    start_time=start_time,
    end_time=end_time,
    step="1d",
)
metric_data

# output:
[{'metric': {'instance': 'demo.do.prometheus.io:9100'},
  'values': [[1656361768, '0.06370670074256046'],
   [1656448168, '0.057729768018532024'],
   [1656534568, '0.06394668746362964'],
   [1656620968, '0.06474131821663776']]}]

Hope this helps!

chauhankaranraj avatar Jun 30 '22 20:06 chauhankaranraj

Hi @lynBIT, did the above suggestion help resolve your problem? Can this issue be closed now?

chauhankaranraj avatar Mar 31 '23 03:03 chauhankaranraj