3tears-observe 0.14.0__tar.gz
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.
- 3tears_observe-0.14.0/.gitignore +216 -0
- 3tears_observe-0.14.0/LICENSE +21 -0
- 3tears_observe-0.14.0/PKG-INFO +44 -0
- 3tears_observe-0.14.0/README.md +18 -0
- 3tears_observe-0.14.0/pyproject.toml +41 -0
- 3tears_observe-0.14.0/src/threetears/observe/__init__.py +57 -0
- 3tears_observe-0.14.0/src/threetears/observe/background.py +92 -0
- 3tears_observe-0.14.0/src/threetears/observe/health.py +464 -0
- 3tears_observe-0.14.0/src/threetears/observe/logging.py +355 -0
- 3tears_observe-0.14.0/src/threetears/observe/middleware.py +264 -0
- 3tears_observe-0.14.0/src/threetears/observe/py.typed +0 -0
- 3tears_observe-0.14.0/src/threetears/observe/resilience.py +117 -0
- 3tears_observe-0.14.0/src/threetears/observe/setup.py +298 -0
- 3tears_observe-0.14.0/src/threetears/observe/tracing.py +238 -0
- 3tears_observe-0.14.0/tests/__init__.py +0 -0
- 3tears_observe-0.14.0/tests/enforcement/__init__.py +0 -0
- 3tears_observe-0.14.0/tests/enforcement/test_codebase_conventions.py +38 -0
- 3tears_observe-0.14.0/tests/enforcement/test_no_metallm_imports.py +26 -0
- 3tears_observe-0.14.0/tests/test_background.py +130 -0
- 3tears_observe-0.14.0/tests/test_health.py +479 -0
- 3tears_observe-0.14.0/tests/test_logging.py +342 -0
- 3tears_observe-0.14.0/tests/test_middleware.py +407 -0
- 3tears_observe-0.14.0/tests/test_setup.py +121 -0
- 3tears_observe-0.14.0/tests/test_smoke.py +50 -0
- 3tears_observe-0.14.0/tests/test_tracing.py +195 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
|
|
209
|
+
# Claude Code local state
|
|
210
|
+
.claude/
|
|
211
|
+
|
|
212
|
+
# prawduct session evidence (local governance artifacts, never shipped)
|
|
213
|
+
.prawduct/
|
|
214
|
+
|
|
215
|
+
# macOS folder metadata
|
|
216
|
+
.DS_Store
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mark Pace
|
|
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.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: 3tears-observe
|
|
3
|
+
Version: 0.14.0
|
|
4
|
+
Summary: Structured logging, tracing, and OpenTelemetry setup for 3tears applications
|
|
5
|
+
Project-URL: Repository, https://github.com/pacepace/3tears
|
|
6
|
+
Author: pace
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: AsyncIO
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
14
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
15
|
+
Classifier: Topic :: System :: Logging
|
|
16
|
+
Classifier: Topic :: System :: Monitoring
|
|
17
|
+
Classifier: Typing :: Typed
|
|
18
|
+
Requires-Python: >=3.14
|
|
19
|
+
Provides-Extra: asgi
|
|
20
|
+
Requires-Dist: uuid-utils; extra == 'asgi'
|
|
21
|
+
Provides-Extra: otel
|
|
22
|
+
Requires-Dist: opentelemetry-api>=1.28; extra == 'otel'
|
|
23
|
+
Requires-Dist: opentelemetry-exporter-otlp>=1.28; extra == 'otel'
|
|
24
|
+
Requires-Dist: opentelemetry-sdk>=1.28; extra == 'otel'
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# 3tears-observe
|
|
28
|
+
|
|
29
|
+
Structured logging, tracing, and OpenTelemetry setup for 3tears applications.
|
|
30
|
+
|
|
31
|
+
## Modules
|
|
32
|
+
|
|
33
|
+
- `threetears.observe.logging` -- Structured logging with context correlation and automatic call-site capture.
|
|
34
|
+
- `threetears.observe.tracing` -- `@traced` decorator (zero-cost without OpenTelemetry).
|
|
35
|
+
- `threetears.observe.setup` -- OpenTelemetry SDK bootstrap (TracerProvider, LoggerProvider, OTLP exporters).
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install 3tears-observe
|
|
41
|
+
|
|
42
|
+
# With OpenTelemetry SDK support:
|
|
43
|
+
pip install 3tears-observe[otel]
|
|
44
|
+
```
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# 3tears-observe
|
|
2
|
+
|
|
3
|
+
Structured logging, tracing, and OpenTelemetry setup for 3tears applications.
|
|
4
|
+
|
|
5
|
+
## Modules
|
|
6
|
+
|
|
7
|
+
- `threetears.observe.logging` -- Structured logging with context correlation and automatic call-site capture.
|
|
8
|
+
- `threetears.observe.tracing` -- `@traced` decorator (zero-cost without OpenTelemetry).
|
|
9
|
+
- `threetears.observe.setup` -- OpenTelemetry SDK bootstrap (TracerProvider, LoggerProvider, OTLP exporters).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install 3tears-observe
|
|
15
|
+
|
|
16
|
+
# With OpenTelemetry SDK support:
|
|
17
|
+
pip install 3tears-observe[otel]
|
|
18
|
+
```
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "3tears-observe"
|
|
7
|
+
version = "0.14.0"
|
|
8
|
+
description = "Structured logging, tracing, and OpenTelemetry setup for 3tears applications"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14"
|
|
11
|
+
authors = [{name = "pace"}]
|
|
12
|
+
license = "MIT"
|
|
13
|
+
license-files = ["LICENSE"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Framework :: AsyncIO",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Programming Language :: Python :: 3",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Topic :: System :: Logging",
|
|
21
|
+
"Topic :: System :: Monitoring",
|
|
22
|
+
"Topic :: Software Development :: Libraries",
|
|
23
|
+
"Typing :: Typed",
|
|
24
|
+
]
|
|
25
|
+
dependencies = []
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
otel = [
|
|
29
|
+
"opentelemetry-api>=1.28",
|
|
30
|
+
"opentelemetry-sdk>=1.28",
|
|
31
|
+
"opentelemetry-exporter-otlp>=1.28",
|
|
32
|
+
]
|
|
33
|
+
asgi = [
|
|
34
|
+
"uuid-utils",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Repository = "https://github.com/pacepace/3tears"
|
|
39
|
+
|
|
40
|
+
[tool.hatch.build.targets.wheel]
|
|
41
|
+
packages = ["src/threetears"]
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""3tears-observe: structured logging, tracing, and OpenTelemetry setup.
|
|
2
|
+
|
|
3
|
+
Provides three modules:
|
|
4
|
+
|
|
5
|
+
- ``threetears.observe.logging`` -- structured logging with generic context
|
|
6
|
+
correlation and automatic call-site capture.
|
|
7
|
+
- ``threetears.observe.tracing`` -- ``@traced`` decorator that creates
|
|
8
|
+
OpenTelemetry spans (zero-cost when OTel is not installed).
|
|
9
|
+
- ``threetears.observe.setup`` -- OpenTelemetry SDK bootstrap for host
|
|
10
|
+
applications (TracerProvider, LoggerProvider, OTLP exporters).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# Version derived from pyproject.toml so the metadata is the single
|
|
14
|
+
# source of truth -- a future release that bumps pyproject without
|
|
15
|
+
# updating ``__init__.py`` can't drift the runtime ``__version__``.
|
|
16
|
+
# The except guard handles the rare case where the package isn't
|
|
17
|
+
# installed via importlib.metadata (e.g. running directly from a
|
|
18
|
+
# checked-out source tree without ``uv sync``); the fallback keeps
|
|
19
|
+
# imports working but reports ``unknown`` rather than crashing.
|
|
20
|
+
from importlib.metadata import PackageNotFoundError as _PackageNotFoundError
|
|
21
|
+
from importlib.metadata import version as _version
|
|
22
|
+
|
|
23
|
+
try:
|
|
24
|
+
__version__ = _version("3tears-observe")
|
|
25
|
+
except _PackageNotFoundError: # pragma: no cover - dev fallback
|
|
26
|
+
__version__ = "unknown"
|
|
27
|
+
|
|
28
|
+
from threetears.observe.background import spawn_background
|
|
29
|
+
from threetears.observe.health import HealthCheck, HealthServer
|
|
30
|
+
from threetears.observe.logging import (
|
|
31
|
+
ContextFormatter,
|
|
32
|
+
ThreeTearsLogger,
|
|
33
|
+
clear_context,
|
|
34
|
+
configure_logging,
|
|
35
|
+
configure_third_party_logging,
|
|
36
|
+
get_context,
|
|
37
|
+
get_logger,
|
|
38
|
+
set_context,
|
|
39
|
+
)
|
|
40
|
+
from threetears.observe.resilience import retry_with_backoff
|
|
41
|
+
from threetears.observe.tracing import traced
|
|
42
|
+
|
|
43
|
+
__all__ = [
|
|
44
|
+
"ContextFormatter",
|
|
45
|
+
"HealthCheck",
|
|
46
|
+
"HealthServer",
|
|
47
|
+
"ThreeTearsLogger",
|
|
48
|
+
"clear_context",
|
|
49
|
+
"configure_logging",
|
|
50
|
+
"configure_third_party_logging",
|
|
51
|
+
"get_context",
|
|
52
|
+
"get_logger",
|
|
53
|
+
"set_context",
|
|
54
|
+
"retry_with_backoff",
|
|
55
|
+
"spawn_background",
|
|
56
|
+
"traced",
|
|
57
|
+
]
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""fire-and-forget asyncio task helper with logged done-callbacks.
|
|
2
|
+
|
|
3
|
+
an ``asyncio.create_task(...)`` call that nobody awaits is a silent
|
|
4
|
+
fire: if the coroutine raises, the exception is logged by the default
|
|
5
|
+
event loop handler on task destruction -- which may or may not reach
|
|
6
|
+
our structured logger depending on how the loop is configured. worse,
|
|
7
|
+
cancelled tasks (normal shutdown) look identical to crashed tasks in
|
|
8
|
+
the default log.
|
|
9
|
+
|
|
10
|
+
``spawn_background`` wraps ``asyncio.create_task`` with a
|
|
11
|
+
done-callback that routes outcomes through ``threetears.observe``:
|
|
12
|
+
|
|
13
|
+
- normal completion -> INFO (background task stop)
|
|
14
|
+
- ``CancelledError`` -> INFO (shutdown protocol, not failure)
|
|
15
|
+
- any other exception -> WARNING with ``exc_info=True``
|
|
16
|
+
|
|
17
|
+
use for any ``create_task`` where the creator does not ``await`` the
|
|
18
|
+
task and does not attach a custom ``add_done_callback``. for tasks
|
|
19
|
+
that are stored on ``self._foo_task`` and cancelled + awaited during
|
|
20
|
+
shutdown, ``spawn_background`` is still the right call -- the task is
|
|
21
|
+
returned unchanged so callers can cancel or await.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import asyncio
|
|
27
|
+
import logging
|
|
28
|
+
from collections.abc import Coroutine
|
|
29
|
+
from typing import Any
|
|
30
|
+
|
|
31
|
+
__all__ = ["spawn_background"]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def spawn_background(
|
|
35
|
+
coro: Coroutine[Any, Any, Any],
|
|
36
|
+
*,
|
|
37
|
+
name: str,
|
|
38
|
+
logger: logging.Logger,
|
|
39
|
+
) -> asyncio.Task[Any]:
|
|
40
|
+
"""schedule ``coro`` as background task with logged done-callback.
|
|
41
|
+
|
|
42
|
+
wraps ``asyncio.create_task`` and attaches done-callback that
|
|
43
|
+
logs task outcome through ``logger``. INFO on normal completion
|
|
44
|
+
and cancellation, WARNING with ``exc_info`` on any other exception.
|
|
45
|
+
returned ``Task`` is suitable for storing on ``self._foo_task``
|
|
46
|
+
and cancelling + awaiting during shutdown; callers may still
|
|
47
|
+
``await`` it directly if desired.
|
|
48
|
+
|
|
49
|
+
accepts any ``logging.Logger`` -- including ``ThreeTearsLogger`` --
|
|
50
|
+
so callers in any repo can pass their existing logger without
|
|
51
|
+
importing ``threetears.observe`` types.
|
|
52
|
+
|
|
53
|
+
:param coro: coroutine to run as background task
|
|
54
|
+
:ptype coro: Coroutine[Any, Any, Any]
|
|
55
|
+
:param name: short human-readable task name for log messages
|
|
56
|
+
:ptype name: str
|
|
57
|
+
:param logger: logger used to emit done-callback outcomes
|
|
58
|
+
:ptype logger: logging.Logger
|
|
59
|
+
:return: scheduled asyncio task
|
|
60
|
+
:rtype: asyncio.Task[Any]
|
|
61
|
+
"""
|
|
62
|
+
task = asyncio.create_task(coro, name=name)
|
|
63
|
+
|
|
64
|
+
def _on_done(t: asyncio.Task[Any]) -> None:
|
|
65
|
+
"""done-callback that routes task outcomes to ``logger``."""
|
|
66
|
+
if t.cancelled():
|
|
67
|
+
logger.info(
|
|
68
|
+
f"background task cancelled: {name}",
|
|
69
|
+
extra={"extra_data": {"task_name": name, "outcome": "cancelled"}},
|
|
70
|
+
)
|
|
71
|
+
else:
|
|
72
|
+
exc = t.exception()
|
|
73
|
+
if exc is None:
|
|
74
|
+
logger.info(
|
|
75
|
+
f"background task stop: {name}",
|
|
76
|
+
extra={"extra_data": {"task_name": name, "outcome": "ok"}},
|
|
77
|
+
)
|
|
78
|
+
else:
|
|
79
|
+
logger.warning(
|
|
80
|
+
f"background task failed: {name}",
|
|
81
|
+
extra={
|
|
82
|
+
"extra_data": {
|
|
83
|
+
"task_name": name,
|
|
84
|
+
"outcome": "error",
|
|
85
|
+
"exc_type": type(exc).__name__,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
exc_info=(type(exc), exc, exc.__traceback__),
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
task.add_done_callback(_on_done)
|
|
92
|
+
return task
|