Gauge
The Gauge
is a metric whose value can go up and down. It's useful to measure things like memory usage or temperatures for example.
Usage
from pytheus.metrics import Gauge
gauge = Gauge('room_temperature', 'temperature in the living room')
Increment
It is possible to increment by 1 by calling inc()
:
it's also possible to specify the amount:
Decrement
As for incrementing we can call dec()
to decrement the value by 1:
or we can specify the amount we want to decrement:
Set a value
A Gauge
value can be set directly with the set()
method:
and as an utility you can also set it to the current time (unix timestamp):
Track progress
You can use the track_inprogress
context manager to track progress of things, each time the context manager is entered it will increase the gauge value by 1 and each time it exits it will decrease that value:
Track time
A Gauge
can time a piece of code, it will set the value to the duration in seconds if you call the time()
context manager:
As a Decorator
When used as a decorator the Gauge
will time the piece of code, syntactic sugar to the time()
context manager:
It is also possible to pass the track_inprogress
flag to make the decorator work as syntactic sugar for the track_inprogress
context manager: