Query to return only the last data point in the time series

11,004 views
Skip to first unread message

mala...@gmail.com

unread,
Nov 17, 2017, 12:50:35 PM11/17/17
to Prometheus Users
I am using another web application to make some displays that need to show the last value in the time series for some metrics. This application generates the reports directly from the JSON of the query, but is pretty restrictive in what I can do with the query (For instance I can't generate time stamps to limit the results or modify the data to select certain points). How can I submit a simple query to prometheus that will return similar data to an instant query, but only return the latest data point rather than a series?

Ben Kochie

unread,
Nov 20, 2017, 1:25:24 AM11/20/17
to mala...@gmail.com, Prometheus Users
I'm confused, you say you want the latest result, which is a basic instant query, that's done via the instant query API.


On Fri, Nov 17, 2017 at 6:50 PM, <mala...@gmail.com> wrote:
I am using another web application to make some displays that need to show the last value in the time series for some metrics. This application generates the reports directly from the JSON of the query, but is pretty restrictive in what I can do with the query (For instance I can't generate time stamps to limit the results or modify the data to select certain points). How can I submit a simple query to prometheus that will return similar data to an instant query, but only return the latest data point rather than a series?

--
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-users+unsubscribe@googlegroups.com.
To post to this group, send email to prometheus-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-users/cdd0b8d6-7224-4a7f-b1ef-af13af89648b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Waldron

unread,
Nov 20, 2017, 1:59:11 AM11/20/17
to Ben Kochie, Prometheus Users
That returns a time series of 4 data points for each metric matching the query. I need just the single latest reported value....hence my question.

Ben Kochie

unread,
Nov 20, 2017, 3:09:15 AM11/20/17
to Ryan Waldron, Prometheus Users
That doesn't sound correct.  Are you asking the instant API endpoint for a range vector?  What is the actual query you're sending?

Ryan Waldron

unread,
Nov 20, 2017, 3:21:13 AM11/20/17
to Ben Kochie, Prometheus Users
Here is the info on the data we are scraping, the query, and the result we are getting back with the names changed as this is a subset of production data. As you can see we get a set of 4 data points back. This is true for every individual metric we are monitoring every time we query the API.

The data we are scraping:
# HELP vpn_status The status of the VPN tunnels.
# TYPE vpn_status status
vpn_status{name="VPN1",expectedtunnels="3",connectedtunnels="3",routes="1",timeofstatus="8848141",color="green,black",humstat="UP",dattim="2017-11-20 08:11:02",node="NODE1"} 0 1511165462000
....

The HTTP API query:

https://prometheus.host.com/api/v1/query?query=vpn_status

The result:
  "status": "success",
  "data": {
    "resultType": "vector",
    "result": [
      {
      {
        "metric": {
          "__name__": "vpn_status",
          "color": "green,black",
          "connectedtunnels": "3",
          "dattim": "2017-11-20 08:10:01",
          "expectedtunnels": "3",
          "humstat": "UP",
          "instance": "10.118.50.187:80",
          "job": "JOB1",
          "name": "VPN1",
          "node": "NODE1",
          "routes": "1",
          "timeofstatus": "8848080"
        },
        "value": [
          1511165677.218,
          "0"
        ]
      },
      {
        "metric": {
          "__name__": "vpn_status",
          "color": "green,black",
          "connectedtunnels": "3",
          "dattim": "2017-11-20 08:11:02",
          "expectedtunnels": "3",
          "humstat": "UP",
          "instance": "10.118.50.187:80",
          "job": "JOB1",
          "name": "VPN1",
          "node": "NODE1",
          "routes": "1",
          "timeofstatus": "8848141"
        },
        "value": [
          1511165677.218,
          "0"
        ]
      },
      {
        "metric": {
          "__name__": "vpn_status",
          "color": "green,black",
          "connectedtunnels": "3",
          "dattim": "2017-11-20 08:12:01",
          "expectedtunnels": "3",
          "humstat": "UP",
          "instance": "10.118.50.187:80",
          "job": "JOB1",
          "name": "VPN1",
          "node": "NODE1",
          "routes": "1",
          "timeofstatus": "8848200"
        },
        "value": [
          1511165677.218,
          "0"
        ]
      },
      {
        "metric": {
          "__name__": "vpn_status",
          "color": "green,black",
          "connectedtunnels": "3",
          "dattim": "2017-11-20 08:13:02",
          "expectedtunnels": "3",
          "humstat": "UP",
          "instance": "10.118.50.187:80",
          "job": "JOB1",
          "name": "VPN1",
          "node": "NODE1",
          "routes": "1",
          "timeofstatus": "8848261"
        },
        "value": [
          1511165677.218,
          "0"
        ]
      }
    ]
  }
}

Ben Kochie

unread,
Nov 20, 2017, 3:27:28 AM11/20/17
to Ryan Waldron, Prometheus Users
The reason you're seeing this is due to the metric data.  What you're seeing is not multiple samples for the same metric, but several completely different time series.  You're churning labels like crazy by having the `dattim` and `timeofstatus` labels.

Can you share more about how you're generating/collecting this? This code needs to be fixed.

Ryan Waldron

unread,
Nov 20, 2017, 3:36:32 AM11/20/17
to Ben Kochie, Prometheus Users
The data is published by a custom script running on our multiple VPN servers. We have both machine and human sources querying prometheus for this data, so the two time formats are for those multiple uses. I still don't see how having both the time stamp and a human readable date-time would cause prometheus to return 4 separate data points all with different time stamps. It is all published as one metric on one line of the data that is scraped with unique labels to identify the tunnel.

Ryan Waldron

unread,
Nov 20, 2017, 3:43:21 AM11/20/17
to Ben Kochie, Prometheus Users
Sorry, I was mistaken. Looking at the code again the timeofstatus is the up time of the tunnel.....not a time stamp.

Ben Kochie

unread,
Nov 20, 2017, 3:52:05 AM11/20/17
to Ryan Waldron, Prometheus Users
Every time you change a label value, Prometheus considers this a completely different time-series metric.  Labels are meant to identify individual instances of a metric.

If you could share this custom script, we might be able to help fix it.
Reply all
Reply to author
Forward
0 new messages