prometheus / prometheus/client_python

Allow count_exceptions to add the error type as a label

Aperta
#744 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
Python
Stelle
4.4k
Fork
876
Merge medio
8g 4h
PR unite (30g)
1

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da Counter.count_exceptions e context_managers.ExceptionCounter, quindi traccia il modo in cui il metodo with gestisce i tipi di eccezione e le etichette. Definisci come aggiungere un’etichetta di errore preservando le chiamate e il comportamento esistenti, quindi verifica l’endpoint richiesto e il caso d’uso delle etichette delle eccezioni con test di compatibilità all’indietro.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
observability-sre
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.