prometheus / prometheus/client_python

Allow count_exceptions to add the error type as a label

Offen
#744 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
4.4k
Forks
876
Ø Merge
8 T. 4 Std.
Gemergte PRs (30 T.)
1

Beschreibung

I'd like count_exceptions to add the error type as a label so I can track what errors are occurring against what endpoints for my application.

I'd like to do this via the with method.

from prometheus_client import Counter

ERRORS = Counter(
    'http_request_errors_count',
    'Count of  exceptions that happen during an HTTP request',
    ['method', 'endpoint', 'error']
)

async def error_middleware(request, handler):
    try:
        with ERRORS.count_exceptions(request.method, request.path):
            return await handler(request)
    except SomeHTTPException as e:
        ...

To do this I've created my own custom Counter classes.

from prometheus_client import Counter, context_managers


class CustomExceptionCounter(context_managers.ExceptionCounter):
    def __init__(self, counter, exception, *labels):
        self._counter = counter
        self._exception = exception
        self._labels = labels

    def __exit__(self, typ, value, traceback):
        if isinstance(value, self._exception):
            self._counter.labels(*(*self._labels, typ.__name__)).inc()  # Append exception's name as a label


class CustomCounter(Counter):
    def count_exceptions(self, *labels, exception=Exception):
        return CustomExceptionCounter(self, exception, *labels)


ERRORS = CustomCounter(
    'http_request_errors_count',
    'Count of CancelledError exceptions',
    ['method', 'endpoint', 'error']
)

I think there is a general solution that's backward compatible, I've just not figured it out. Is this something you'd support? I'd be happy to submit a PR if I can figure out how to make this backward compatible.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit Counter.count_exceptions und context_managers.ExceptionCounter und verfolge anschließend, wie die with-Methode Ausnahmetypen und Labels behandelt. Lege fest, wie ein Fehlerlabel hinzugefügt werden kann, ohne bestehende Aufrufe und Verhaltensweisen zu verändern, und überprüfe den angeforderten Endpunkt sowie den Anwendungsfall für die Verwendung von Ausnahme-Labels mit Tests auf Abwärtskompatibilität.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
observability-sre
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.