Skip to content
This repository has been archived by the owner. It is now read-only.

Prometheus: count_scalar causes error #45

JamesBarwell opened this issue Oct 5, 2015 · 6 comments


None yet
7 participants
Copy link

commented Oct 5, 2015

Hi, I'm trying to use count_scalar to display the number of containers I have currently running in my environment. This is my query

count_scalar(container_last_seen{image="myimage"} - time() > -5)

This works in the Prometheus dashboard. But in Grafana I receive a JSON error and no data comes through:

labelData is null

A slightly modified query does work in Grafana:

count(container_last_seen{image="myimage"} - time() > -5)

However, unlike count_scalar, count returns empty datasets (when run on an empty vector), rather a scalar value of zero (see: prometheus/prometheus#375). I want to use this query to drive single-stats that go red when there are zero containers running, but haven't been able to find a way to work with the threshold options when the single-stat is receiving an empty vector, rather than a scalar zero.

I'm not sure that this is even a Grafana bug (could it be the Prometheus HTTP API?), but I thought given that it works in their dashboard I'll try here first and see if anyone can help to pinpoint what's wrong, and perhaps point me in the right direction. Cheers.


This comment has been minimized.

Copy link

commented Sep 8, 2016

I ran into a similar problem when using Prometheus and Grafana. I had a metric that represents test cases and it had a tag "status" with values "True" or "False" for a passing or failing test respectively. I wanted a graph of failing test cases. My first attempt was to do:


Which generally works except for when my chosen time frame has no failing tests the graph would be completely blank. Toggling the "Null value" drop down in the "Display" tab to all three values (connected, null, null as zero) doesn't effect the graph.

Second attempt:


This also generally works except when my chosen time frame has no failing tests the graph would have a line at zero including into the future where no data was available. I suspect this is because Prometheus does not distinguish between "there's no data here because it was filtered out" and "there's no data here at all". Both are empty vectors {} and count() and count_scalar() have different ways of handling it as per the issue linked above and the docs at

To get what I wanted I (ab)used the absent() function and taking advantage that an empty vector {} plus anything is still an empty vector:

absent(absent(testmetric)) - 1 + count_scalar(testmetric{status="False"})
  • The first absent(testmetric) returns 1 if there are no data points otherwise {}
  • The second absent(...) flips this and returns {} if there are no data points and returns 1 if there is.

In the event that there is no data then we get {} and the rest of the expression is moot since {} ± anything is {}.

If there is data available then we get 1 and the expression is equivalent to count_scalar(testmetric{status=False}).

I think through a combination of chained absent(...) calls and addition you can get what you want.


This comment has been minimized.

Copy link

commented Aug 16, 2017

You can do this more easily using the >bool operator. like:
sum(container_last_seen{image="myimage"} - time() >bool -5)

The operator returns a vector of 1s and 0s after running the comparison.


This comment has been minimized.

Copy link

commented Nov 30, 2017

sum(rate(node_cpu{instance="$host", mode!="idle"}[$interval])) by (mode)* 100 / count_scalar(node_cpu{mode="user", instance="$host"}) or sum(irate(node_cpu{instance="$host", mode!="idle"}[5m])) by (mode) * 100 / count_scalar(node_cpu{mode="user", instance="$host"})
this is from my dashboard, but the graph returned an error "parse error at char 96: unknown function with name "count_scalar"" ,which function i can use to replace count_scalar in prometheus2.0 ?


This comment has been minimized.

Copy link

commented Dec 17, 2017

In prometheus 1.8.2's change log, it said that the function count_scalar is removed,so, some dashboards which use this function may cause an error. which function i can use to replace? maybe 'absent'?


This comment has been minimized.

Copy link

commented Jan 3, 2018

Scanning through Pull Requests dealing with this change, it seems as if scalar(count()) might be a suitable replacement. See e.g. what Percona did:


This comment has been minimized.

Copy link

commented Jan 17, 2018!topic/prometheus-users/P5CZWArvMb0

count_scalar(...) -> scalar(count(...))

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.