prometheus / prometheus/client_python

Allow count_exceptions to add the error type as a label

オープン
#744 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
Python
スター
4.4k
フォーク
876
平均マージ
8日 4時間
マージ済み PR(30日)
1

説明

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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

Counter.count_exceptions と context_managers.ExceptionCounter から始め、次に with メソッドが例外の型とラベルをどのように処理するかを追跡します。既存の呼び出しと動作を維持したままエラーラベルを追加する方法を定義し、後方互換性のテストによって、要求されたエンドポイントと例外ラベルのユースケースを検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
observability-sre
issue の種類
機能追加
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。