probirka 0.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- probirka/__init__.py +32 -0
- probirka/_aiohttp.py +41 -0
- probirka/_fastapi.py +29 -0
- probirka/_probes.py +81 -0
- probirka/_probirka.py +110 -0
- probirka/_results.py +21 -0
- probirka-0.3.0.dist-info/METADATA +33 -0
- probirka-0.3.0.dist-info/RECORD +10 -0
- probirka-0.3.0.dist-info/WHEEL +4 -0
- probirka-0.3.0.dist-info/licenses/LICENSE +21 -0
probirka/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
from contextlib import suppress
|
|
2
|
+
|
|
3
|
+
from probirka._probes import Probe, ProbeBase
|
|
4
|
+
from probirka._probirka import Probirka
|
|
5
|
+
from probirka._results import HealthCheckResult, ProbeResult
|
|
6
|
+
|
|
7
|
+
__title__ = 'probirka'
|
|
8
|
+
__version__ = '0.3.0'
|
|
9
|
+
__url__ = 'https://github.com/appKODE/probirka'
|
|
10
|
+
__author__ = ''
|
|
11
|
+
__author_email__ = ''
|
|
12
|
+
__license__ = ''
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
'HealthCheckResult',
|
|
16
|
+
# types
|
|
17
|
+
'Probe',
|
|
18
|
+
'ProbeBase',
|
|
19
|
+
# results
|
|
20
|
+
'ProbeResult',
|
|
21
|
+
'Probirka',
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
with suppress(ImportError):
|
|
25
|
+
from probirka._aiohttp import make_aiohttp_endpoint # noqa
|
|
26
|
+
|
|
27
|
+
__all__.append('make_aiohttp_endpoint')
|
|
28
|
+
|
|
29
|
+
with suppress(ImportError):
|
|
30
|
+
from probirka._fastapi import make_fastapi_endpoint # noqa
|
|
31
|
+
|
|
32
|
+
__all__.append('make_fastapi_endpoint')
|
probirka/_aiohttp.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from dataclasses import asdict
|
|
4
|
+
from typing import Any, Callable, Coroutine, List, Optional, Union
|
|
5
|
+
|
|
6
|
+
from aiohttp import web
|
|
7
|
+
|
|
8
|
+
from probirka import Probirka
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def make_aiohttp_endpoint(
|
|
12
|
+
probirka: Probirka,
|
|
13
|
+
timeout: Optional[int] = None,
|
|
14
|
+
with_groups: Union[str, List[str]] = '',
|
|
15
|
+
skip_required: bool = False,
|
|
16
|
+
return_results: bool = True,
|
|
17
|
+
success_code: int = 200,
|
|
18
|
+
error_code: int = 500,
|
|
19
|
+
) -> Callable[[web.Request], Coroutine[Any, Any, web.Response]]:
|
|
20
|
+
async def endpoint(
|
|
21
|
+
_: web.Request,
|
|
22
|
+
) -> web.Response:
|
|
23
|
+
res = await probirka.run(
|
|
24
|
+
timeout=timeout,
|
|
25
|
+
with_groups=with_groups,
|
|
26
|
+
skip_required=skip_required,
|
|
27
|
+
)
|
|
28
|
+
status_code = success_code if res.ok else error_code
|
|
29
|
+
return (
|
|
30
|
+
web.json_response(
|
|
31
|
+
text=json.dumps(obj=asdict(res), default=str),
|
|
32
|
+
status=status_code,
|
|
33
|
+
)
|
|
34
|
+
if return_results
|
|
35
|
+
else web.Response(
|
|
36
|
+
body='',
|
|
37
|
+
status=status_code,
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
return endpoint
|
probirka/_fastapi.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from typing import Any, Callable, Coroutine, List, Optional, Union
|
|
2
|
+
|
|
3
|
+
from fastapi import status
|
|
4
|
+
from fastapi.encoders import jsonable_encoder
|
|
5
|
+
from fastapi.responses import JSONResponse, Response
|
|
6
|
+
|
|
7
|
+
from probirka import Probirka
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def make_fastapi_endpoint(
|
|
11
|
+
probirka: Probirka,
|
|
12
|
+
timeout: Optional[int] = None,
|
|
13
|
+
with_groups: Union[str, List[str]] = '',
|
|
14
|
+
skip_required: bool = False,
|
|
15
|
+
return_results: bool = True,
|
|
16
|
+
success_code: int = status.HTTP_200_OK,
|
|
17
|
+
error_code: int = status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
18
|
+
) -> Callable[[], Coroutine[Any, Any, Response]]:
|
|
19
|
+
async def endpoint() -> Response:
|
|
20
|
+
res = await probirka.run(
|
|
21
|
+
timeout=timeout,
|
|
22
|
+
with_groups=with_groups,
|
|
23
|
+
skip_required=skip_required,
|
|
24
|
+
)
|
|
25
|
+
resp = JSONResponse(jsonable_encoder(res)) if return_results else Response()
|
|
26
|
+
resp.status_code = success_code if res.ok else error_code
|
|
27
|
+
return resp
|
|
28
|
+
|
|
29
|
+
return endpoint
|
probirka/_probes.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
from asyncio import iscoroutinefunction, wait_for
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from typing import Callable, Optional, Protocol
|
|
5
|
+
|
|
6
|
+
from probirka._results import ProbeResult
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class Probe(
|
|
10
|
+
Protocol,
|
|
11
|
+
):
|
|
12
|
+
async def run_check(
|
|
13
|
+
self,
|
|
14
|
+
) -> ProbeResult: ...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ProbeBase(
|
|
18
|
+
ABC,
|
|
19
|
+
):
|
|
20
|
+
def __init__(
|
|
21
|
+
self,
|
|
22
|
+
name: Optional[str] = None,
|
|
23
|
+
timeout: Optional[int] = None,
|
|
24
|
+
) -> None:
|
|
25
|
+
self._timeout = timeout
|
|
26
|
+
self._name = name or self.__class__.__name__
|
|
27
|
+
|
|
28
|
+
@abstractmethod
|
|
29
|
+
async def _check(
|
|
30
|
+
self,
|
|
31
|
+
) -> Optional[bool]:
|
|
32
|
+
raise NotImplementedError
|
|
33
|
+
|
|
34
|
+
async def run_check(
|
|
35
|
+
self,
|
|
36
|
+
) -> ProbeResult:
|
|
37
|
+
started_at = datetime.now()
|
|
38
|
+
error = None
|
|
39
|
+
task = self._check()
|
|
40
|
+
try:
|
|
41
|
+
result = await wait_for(
|
|
42
|
+
fut=task,
|
|
43
|
+
timeout=self._timeout,
|
|
44
|
+
)
|
|
45
|
+
if result is None:
|
|
46
|
+
result = True
|
|
47
|
+
except Exception as exc:
|
|
48
|
+
result = False
|
|
49
|
+
error = str(exc)
|
|
50
|
+
finally:
|
|
51
|
+
task.close()
|
|
52
|
+
return ProbeResult(
|
|
53
|
+
ok=False if result is None else result,
|
|
54
|
+
started_at=started_at,
|
|
55
|
+
elapsed=datetime.now() - started_at,
|
|
56
|
+
name=self._name,
|
|
57
|
+
error=error,
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class CallableProbe(
|
|
62
|
+
ProbeBase,
|
|
63
|
+
):
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
func: Callable[[], Optional[bool]],
|
|
67
|
+
name: Optional[str] = None,
|
|
68
|
+
timeout: Optional[int] = None,
|
|
69
|
+
) -> None:
|
|
70
|
+
self._func = func
|
|
71
|
+
super().__init__(
|
|
72
|
+
name=name or func.__name__,
|
|
73
|
+
timeout=timeout,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
async def _check(
|
|
77
|
+
self,
|
|
78
|
+
) -> Optional[bool]:
|
|
79
|
+
if iscoroutinefunction(self._func):
|
|
80
|
+
return await self._func()
|
|
81
|
+
return self._func()
|
probirka/_probirka.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
from asyncio import gather, wait_for
|
|
2
|
+
from collections import defaultdict
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from typing import Any, Callable, Dict, List, Optional, Sequence, Union
|
|
5
|
+
|
|
6
|
+
from probirka._probes import CallableProbe, Probe
|
|
7
|
+
from probirka._results import HealthCheckResult, ProbeResult
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Probirka:
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
) -> None:
|
|
14
|
+
self._required_probes: List[Probe] = []
|
|
15
|
+
self._optional_probes: Dict[str, List[Probe]] = defaultdict(list)
|
|
16
|
+
self._info: Dict[str, Any] = {}
|
|
17
|
+
|
|
18
|
+
def add_info(
|
|
19
|
+
self,
|
|
20
|
+
name: str,
|
|
21
|
+
value: Any,
|
|
22
|
+
) -> None:
|
|
23
|
+
self._info[name] = value
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
def info(
|
|
27
|
+
self,
|
|
28
|
+
) -> Dict[str, Any]:
|
|
29
|
+
return self._info
|
|
30
|
+
|
|
31
|
+
def add_probes(
|
|
32
|
+
self,
|
|
33
|
+
*probes: Probe,
|
|
34
|
+
groups: Union[str, List[str]] = '',
|
|
35
|
+
) -> None:
|
|
36
|
+
if groups:
|
|
37
|
+
if isinstance(groups, str):
|
|
38
|
+
groups = [groups]
|
|
39
|
+
for group in groups:
|
|
40
|
+
self._optional_probes[group].extend(probes)
|
|
41
|
+
return
|
|
42
|
+
self._required_probes.extend(probes)
|
|
43
|
+
|
|
44
|
+
def add(
|
|
45
|
+
self,
|
|
46
|
+
name: Optional[str] = None,
|
|
47
|
+
timeout: Optional[int] = None,
|
|
48
|
+
groups: Union[str, List[str]] = '',
|
|
49
|
+
) -> Callable:
|
|
50
|
+
def _wrapper(
|
|
51
|
+
func: Callable,
|
|
52
|
+
) -> Any:
|
|
53
|
+
self.add_probes(
|
|
54
|
+
CallableProbe(
|
|
55
|
+
func=func,
|
|
56
|
+
name=name,
|
|
57
|
+
timeout=timeout,
|
|
58
|
+
),
|
|
59
|
+
groups=groups,
|
|
60
|
+
)
|
|
61
|
+
return func
|
|
62
|
+
|
|
63
|
+
return _wrapper
|
|
64
|
+
|
|
65
|
+
async def _inner_run(
|
|
66
|
+
self,
|
|
67
|
+
with_groups: List[str],
|
|
68
|
+
skip_required: bool,
|
|
69
|
+
) -> Sequence[ProbeResult]:
|
|
70
|
+
tasks = [] if skip_required else [probe.run_check() for probe in self._required_probes]
|
|
71
|
+
for group in with_groups:
|
|
72
|
+
tasks += [probe.run_check() for probe in self._optional_probes[group]]
|
|
73
|
+
results = await gather(*tasks)
|
|
74
|
+
for coro in tasks:
|
|
75
|
+
coro.close()
|
|
76
|
+
return results
|
|
77
|
+
|
|
78
|
+
async def run(
|
|
79
|
+
self,
|
|
80
|
+
timeout: Optional[int] = None,
|
|
81
|
+
with_groups: Union[str, List[str]] = '',
|
|
82
|
+
skip_required: bool = False,
|
|
83
|
+
) -> HealthCheckResult:
|
|
84
|
+
if with_groups and isinstance(with_groups, str):
|
|
85
|
+
with_groups = [with_groups]
|
|
86
|
+
started_at = datetime.now()
|
|
87
|
+
fut = self._inner_run(
|
|
88
|
+
with_groups=with_groups, # type: ignore
|
|
89
|
+
skip_required=skip_required,
|
|
90
|
+
)
|
|
91
|
+
results = (
|
|
92
|
+
await wait_for(
|
|
93
|
+
fut=fut,
|
|
94
|
+
timeout=timeout,
|
|
95
|
+
)
|
|
96
|
+
if timeout
|
|
97
|
+
else await fut
|
|
98
|
+
)
|
|
99
|
+
ok = True
|
|
100
|
+
for result in results:
|
|
101
|
+
if result.ok is False:
|
|
102
|
+
ok = False
|
|
103
|
+
break
|
|
104
|
+
return HealthCheckResult(
|
|
105
|
+
ok=ok,
|
|
106
|
+
info=self._info,
|
|
107
|
+
started_at=started_at,
|
|
108
|
+
total_elapsed=datetime.now() - started_at,
|
|
109
|
+
checks=results,
|
|
110
|
+
)
|
probirka/_results.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from datetime import datetime, timedelta
|
|
3
|
+
from typing import Any, Dict, Optional, Sequence
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class ProbeResult:
|
|
8
|
+
ok: bool
|
|
9
|
+
started_at: datetime
|
|
10
|
+
elapsed: timedelta
|
|
11
|
+
name: str
|
|
12
|
+
error: Optional[str]
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@dataclass(frozen=True)
|
|
16
|
+
class HealthCheckResult:
|
|
17
|
+
ok: bool
|
|
18
|
+
info: Dict[str, Any]
|
|
19
|
+
started_at: datetime
|
|
20
|
+
total_elapsed: timedelta
|
|
21
|
+
checks: Sequence[ProbeResult]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: probirka
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Python 3 library to write simple asynchronous health checks (probes)
|
|
5
|
+
Project-URL: Repository, https://github.com/appKODE/probirka
|
|
6
|
+
Author-email: KODE <slurm@kode.ru>
|
|
7
|
+
License: MIT
|
|
8
|
+
Keywords: health-check,monitoring,probes
|
|
9
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
10
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.8
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# PROB🧪RKA
|
|
22
|
+
|
|
23
|
+
Python 3 library to write simple asynchronous health checks (probes)
|
|
24
|
+
|
|
25
|
+
[](https://github.com/astral-sh/ruff)
|
|
26
|
+
[](https://pypi.python.org/pypi/probirka)
|
|
27
|
+
[](https://pypi.python.org/pypi/probirka)
|
|
28
|
+
|
|
29
|
+
## Installation
|
|
30
|
+
|
|
31
|
+
```shell
|
|
32
|
+
pip install probirka
|
|
33
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
probirka/__init__.py,sha256=jsGdY0J3NZgJAO9EkJFc6MSfVxGYlQs_j6mSJZ3FKzM,736
|
|
2
|
+
probirka/_aiohttp.py,sha256=rpOGqfS_BuQY60T3PZ6VF-GeHvgGIh9XNLeYvT34ak4,1098
|
|
3
|
+
probirka/_fastapi.py,sha256=oV82iGuI4jWCil1OMlhqE_pb87h8Vt4Bu7464O5-q5U,960
|
|
4
|
+
probirka/_probes.py,sha256=jokvsTjwZ3sMehQn8vHe4dCt7LgFHAF17lia6aPDDYw,1865
|
|
5
|
+
probirka/_probirka.py,sha256=z7ZN7JGBhj5A6Ly-kupHdbCgjRu74bN7EWdvtptdLFg,3041
|
|
6
|
+
probirka/_results.py,sha256=3-eS6n1JibnXgVFYw3uTYj2Qbh_stehGG_M12BEiAAA,446
|
|
7
|
+
probirka-0.3.0.dist-info/METADATA,sha256=UssUrZcNgjUGYjFuRSVnyXy4_zcc-Ca7cwhAPr-AH-s,1317
|
|
8
|
+
probirka-0.3.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
|
9
|
+
probirka-0.3.0.dist-info/licenses/LICENSE,sha256=k-1sgbW05GN_ubAXMm7SVTlNtFCa8hrI74rmxiW6ecc,1065
|
|
10
|
+
probirka-0.3.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 KODE LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|