Counter is a metric that only increases in value, it could also reset to 0 for example in case of a service restart.
As the name suggests it's useful to count things, for example cache hits, number of connections or even count exceptions.
If you need a metric that can go up and down in value, checkout the Gauge.
To create a
Counter import it and instantiate:
Increment the value
Now it's possible to increment the count by calling the
it is also possible to specify by which amount to increase:
inc() accepts only positive values as counters cannot decrease.
As counters are a good fit for counting exceptions there are some nicities included, you can count exceptions within a
with statement with
the following examples assume a
It is also possible to specify which
Exceptions to count:
count_exceptions accepts an
Exception or a tuple of exceptions.
As a Decorator
When used as a decorator the
Counter will count exceptions, syntactic sugar to
you are still able to specify which exceptions you want to count: