Make HTTP API query with function

208 views
Skip to first unread message

Sued Tech

unread,
Nov 9, 2021, 6:33:00 AM11/9/21
to Prometheus Users
Hi,

How to make an  request with function in HTTP API ?
This query, dont return this metric using function of avg_over_time,

query={__name__=~"a100_001_FT_DACA_PV|a101_401_0_PIC_PIDA_PV|avg_over_time(E002_C04_kW)"}

example of output:
prometheus.JPG

Brian Candler

unread,
Nov 9, 2021, 7:22:29 AM11/9/21
to Prometheus Users
On Tuesday, 9 November 2021 at 11:33:00 UTC vis...@gmail.com wrote:
How to make an  request with function in HTTP API ?

You're using it correctly, but your query is bad.

This query, dont return this metric using function of avg_over_time,

First get your query working in the PromQL browser in the web interface, before sending it to the API.

Your query doesn't do what you think it does, in fact it's a pretty meaningless query.  Your query is:

{__name__=~"a100_001_FT_DACA_PV|a101_401_0_PIC_PIDA_PV|avg_over_time(E002_C04_kW)"}

What you are doing is matching the timeseries name (which is in an internal label __name__) against one regular expression.  So what this query means is:

Return every timeseries whose name is either "a100_001_FT_DACA_PV" or "a101_401_0_PIC_PIDA_PV" or "avg_over_timeE002_C04_kW"

(The vertical bar in a regular expression gives alternatives.  Parentheses also have special meanings in a regular expression, so a(b) matches "ab")

And that's exactly what your result shows:
- one timeseries with name "a100_001_FT_DACA_PV"
- one timeseries with name "a101_401_0_PIC_PIDA_P"
- no timeseries matching name "avg_over_timeE002_C04_kW"

If you want to use average_over_time then you'll need to invest some time learning promQL.  This is a function, and it applies to a range vector not an instance vector.  For example,

average_over_time(a100_001_FT_DACA_PV[2h])

is a valid query: a100_001_FT_DACA_PV[2h] is a range vector, i.e. it returns all timeseries with that name, and all data covering a 2 hour period.   average_over_time(...) gives you an instant vector which averages all the data points within that time range for each matching timeseries.

Here are some resources to start learning about promQL:




Sued Tech

unread,
Nov 9, 2021, 10:28:43 AM11/9/21
to Prometheus Users
Ok, you clarifying,
But,
How to make this querys in the same request?

query = {__name__=~"a100_001_FT_DACA_PV|a101_401_0_PIC_PIDA_PV"}

and

query = avg_over_time(E002_C04_kW[1h])/1000

Brian Candler

unread,
Nov 9, 2021, 2:11:34 PM11/9/21
to Prometheus Users
As far as I know, the HTTP API only supports one request per call.

In fact, really you have three queries, so you should be making three HTTP requests:

query=a100_001_FT_DACA_PV
query=a101_401_0_PIC_PIDA_PV
query=avg_over_time(E002_C04_kW[1h])/1000

Sued Tech

unread,
Nov 9, 2021, 2:40:11 PM11/9/21
to Prometheus Users
Thanks Brian,
I realy make one request per call,
My problem now is,

For this request

query=a100_001_FT_DACA_PV

Json return this
{
    "status": "success",
    "data": {
        "resultType": "vector",
        "result": [
            {
                "metric": {
                    "__name__": "a100_001_FT_DACA_PV",
                    "instance": "172.115.0.25:4242",
                    "job": "prometheus"
                },
                "value": [
                    1636486652.982,
                    "597.5383929357245"
                ]
            }
        ]
    }
}

An for this query:
query= avg_over_time(E001_C04_kW[1h])
This ...
{
    "status": "success",
    "data": {
        "resultType": "vector",
        "result": [
            {
                "metric": {
                    "instance": "172.115.0.20:5001",
                    "job": "prometheus"
                },
                "value": [
                    1636486694.982,
                    "23354.967702907958"
                ]
            }
        ]
    }
}

Note output of query= avg_over_time(E001_C04_kW[1h]) a key __name__ doenst return, how to force this query returning key __name__ in result ?

Best Regards

Brian Candler

unread,
Nov 9, 2021, 5:12:33 PM11/9/21
to Prometheus Users
That is correct.  The metric being returned is not E001_C04_kW; it is a calculated value. In this case it is derived from a single metric E001_C04_kW, but in general could be derived from multiple metrics.

You'll see the same in the PromQL browser. For example, if you give this query:

up

then each metric returned is of the form up{...labels...}. But if you give this query:

up * 2

then each metric returned is of the form {...labels...}  (i.e. there is no metric name, because this is no longer the "up" metric but a calculated value).
Reply all
Reply to author
Forward
0 new messages