Skip to content

Commit

Permalink
Merge pull request #881 from jvansanten/count-multiple-exceptions
Browse files Browse the repository at this point in the history
Allow tuples of exceptions in ExceptionCounter
  • Loading branch information
csmarchbanks committed Jan 23, 2023
2 parents 1fd0ded + 8a27f80 commit 4f994ec
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 4 additions & 2 deletions prometheus_client/context_managers.py
@@ -1,7 +1,9 @@
import sys
from timeit import default_timer
from types import TracebackType
from typing import Any, Callable, Optional, Type, TYPE_CHECKING, TypeVar
from typing import (
Any, Callable, Optional, Tuple, Type, TYPE_CHECKING, TypeVar, Union,
)

if sys.version_info >= (3, 8, 0):
from typing import Literal
Expand All @@ -14,7 +16,7 @@


class ExceptionCounter:
def __init__(self, counter: "Counter", exception: Type[BaseException]) -> None:
def __init__(self, counter: "Counter", exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]]) -> None:
self._counter = counter
self._exception = exception

Expand Down
6 changes: 3 additions & 3 deletions prometheus_client/metrics.py
Expand Up @@ -3,8 +3,8 @@
import time
import types
from typing import (
Any, Callable, Dict, Iterable, List, Optional, Sequence, Type, TypeVar,
Union,
Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Type,
TypeVar, Union,
)

from . import values # retain this import style for testability
Expand Down Expand Up @@ -288,7 +288,7 @@ def inc(self, amount: float = 1, exemplar: Optional[Dict[str, str]] = None) -> N
_validate_exemplar(exemplar)
self._value.set_exemplar(Exemplar(exemplar, amount, time.time()))

def count_exceptions(self, exception: Type[BaseException] = Exception) -> ExceptionCounter:
def count_exceptions(self, exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = Exception) -> ExceptionCounter:
"""Count exceptions in a block of code or function.
Can be used as a function decorator or context manager.
Expand Down

0 comments on commit 4f994ec

Please sign in to comment.