prometheus-fastapi-instrumentator icon indicating copy to clipboard operation
prometheus-fastapi-instrumentator copied to clipboard

Counters for specific use-cases

Open AbhishekBose opened this issue 2 years ago • 2 comments

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?

AbhishekBose avatar Jul 27 '22 10:07 AbhishekBose

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 avatar Aug 18 '22 21:08 oysteins10

@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

AbhishekBose avatar Jan 18 '23 12:01 AbhishekBose