[python prometheus_client] is Gauge.set guaranteed to always cast as float given value?

548 views
Skip to first unread message

Jonathan Martin

unread,
Apr 28, 2021, 6:16:28 AM4/28/21
to Prometheus Developers
Hi there,

I have a question relative to Gauge and booleans, if you don't mind.

I'm considering to use a gauge to reflect the evolution of a boolean value.

Knowing that the set method of a Gauge cast the given value as a float, I could call directly my_gauge.set(my_bool_value), and have it set to 0.0 or 1.0.

So the question here is: Is the casting as float part of the API design, or simply an implementation detail which may change?

If we have no guarantee that this cast will remain, then the call to set needs to be protected by my_gauge.set(float(my_bool_value)), but it is a pity to call it twice...

It's a nice built in capability, so if it is going to last, I may argue that it would be worth mentioning it explicitly somewhere, either in the doc of the set function or the documentation of the Gauge (https://github.com/prometheus/client_python#gauge).

Thanks in advance!

Chris Marchbanks

unread,
Apr 28, 2021, 3:46:29 PM4/28/21
to Jonathan Martin, Prometheus Developers
Hi Jonathan,

As it stands I would say that is an implementation detail that could change, for example we might treat integers differently from floats in the future. That is just the status quo, and if the community would like to see this as the official behavior we could add a test case for setting values to booleans. OpenMetrics even specifies that booleans must be represented as 0/1 so I think there is a reasonable case to make it official.

Chris

--
You received this message because you are subscribed to the Google Groups "Prometheus Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prometheus-devel...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/prometheus-developers/ffcee28e-d367-4897-8b30-ff5a1e28daean%40googlegroups.com.

Jonathan Martin, MARSS

unread,
Apr 28, 2021, 4:38:56 PM4/28/21
to Chris Marchbanks, Prometheus Developers
Hi Chris,

Thanks a lot for the clarification!

As long as it is considered OK to give a boolean (whatever it becomes under the hood), then I'm happy.
Indeed, adding a test case and official doc for using booleans makes total sense and would avoid unexpected change in that area.

Best regards,

Jonathan


Reply all
Reply to author
Forward
0 new messages