prometheus-fastapi-instrumentator
prometheus-fastapi-instrumentator copied to clipboard
Counters for specific use-cases
Hello Team,
I want to create a counter and increment it only when a certain event occurs.
How can I do that? In the example given for http_requested_languages_total
, the counter is incremented for every request.
Can you selectively only call the increment function? What would be the best way to go about this?
I assume you can do something like this:
def instrumentator(info: Info):
if some_condition:
# Dont do anything
return
# Otherwise we increment
COUNT_METRIC.inc()
@oysteins10 @trallnag I have created a metric in this manner
@staticmethod
def invalid_doc_type_counter():
METRIC = Counter(
"invalid_doc_type_count",
"Number of times invalid doc type issue came up",
labelnames=("handler",)
)
METRIC.labels("info.modified_handler").inc()
And I am trying to call it in another function in this manner
if not filtered_det:
#Invalid doc type counter
Counters.invalid_doc_type_counter()
raise ValueError("Invalid Doc Type")
When I hit the /metrics endpoint
invalid_doc_type_count_total{handler="info.modified_handler"} 1.0
<---This value remains at one and doesn't change