Skip to content

Commit 4f994ec

Browse files
authored
Merge pull request #881 from jvansanten/count-multiple-exceptions
Allow tuples of exceptions in ExceptionCounter
2 parents 1fd0ded + 8a27f80 commit 4f994ec

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

prometheus_client/context_managers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import sys
22
from timeit import default_timer
33
from types import TracebackType
4-
from typing import Any, Callable, Optional, Type, TYPE_CHECKING, TypeVar
4+
from typing import (
5+
Any, Callable, Optional, Tuple, Type, TYPE_CHECKING, TypeVar, Union,
6+
)
57

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

1517

1618
class ExceptionCounter:
17-
def __init__(self, counter: "Counter", exception: Type[BaseException]) -> None:
19+
def __init__(self, counter: "Counter", exception: Union[Type[BaseException], Tuple[Type[BaseException], ...]]) -> None:
1820
self._counter = counter
1921
self._exception = exception
2022

prometheus_client/metrics.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import time
44
import types
55
from typing import (
6-
Any, Callable, Dict, Iterable, List, Optional, Sequence, Type, TypeVar,
7-
Union,
6+
Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Type,
7+
TypeVar, Union,
88
)
99

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

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

0 commit comments

Comments
 (0)