python-corekit 0.2.0__py3-none-any.whl → 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.
- corekit/api/application.py +47 -9
- corekit/api/lifespan.py +26 -3
- corekit/concurrency/__init__.py +2 -2
- corekit/concurrency/decorators.py +32 -5
- corekit/concurrency/thread_local.py +2 -2
- corekit/concurrency/worker.py +9 -0
- corekit/config/loader.py +42 -5
- corekit/config/settings.py +11 -1
- corekit/connections/__init__.py +7 -1
- corekit/connections/connectable.py +45 -4
- corekit/connections/redis/connection.py +53 -10
- corekit/connections/sql/__init__.py +2 -1
- corekit/connections/sql/connection.py +39 -5
- corekit/connections/sql/fields/__init__.py +2 -2
- corekit/connections/sql/fields/jsonb.py +13 -6
- corekit/connections/sql/migration/__init__.py +4 -0
- corekit/connections/sql/migration/operations.py +69 -2
- corekit/connections/sql/operations/base.py +11 -2
- corekit/connections/sql/operations/statements.py +25 -5
- corekit/connections/sql/table.py +7 -29
- corekit/crypto/__init__.py +3 -1
- corekit/crypto/constants.py +2 -2
- corekit/crypto/hasher.py +9 -4
- corekit/data/dataset.py +8 -2
- corekit/data/expressions/__init__.py +3 -3
- corekit/data/expressions/comparison.py +19 -80
- corekit/data/expressions/expression.py +0 -32
- corekit/data/expressions/operator.py +13 -28
- corekit/data/stats.py +3 -0
- corekit/decorators/exception_handling.py +36 -8
- corekit/docker/watchdog.py +50 -31
- corekit/etl/__init__.py +2 -1
- corekit/etl/connection.py +14 -12
- corekit/etl/extract/extractor.py +6 -13
- corekit/etl/orchestrator.py +19 -2
- corekit/etl/schemas.py +2 -2
- corekit/etl/transform/transformer.py +4 -1
- corekit/events/publisher.py +1 -1
- corekit/events/reader.py +26 -21
- corekit/events/sse.py +4 -1
- corekit/events/websocket.py +24 -11
- corekit/exceptions/__init__.py +24 -9
- corekit/exceptions/base.py +139 -10
- corekit/exceptions/enum.py +17 -0
- corekit/exceptions/types.py +6 -6
- corekit/files/__init__.py +2 -4
- corekit/files/base.py +15 -2
- corekit/files/enum.py +0 -5
- corekit/files/json.py +16 -2
- corekit/http/__init__.py +43 -5
- corekit/http/api.py +24 -0
- corekit/http/client.py +100 -73
- corekit/http/exceptions.py +140 -0
- corekit/http/response.py +50 -1
- corekit/http/status.py +89 -0
- corekit/jobs/runner.py +12 -1
- corekit/jobs/task.py +23 -2
- corekit/log_monitor/models.py +8 -2
- corekit/log_monitor/service.py +77 -38
- corekit/notifications/base.py +18 -10
- corekit/observability/__init__.py +9 -2
- corekit/observability/benchmarkable.py +23 -5
- corekit/observability/loggable.py +21 -0
- corekit/observability/request_context.py +55 -2
- corekit/observability/timing/timer.py +4 -2
- corekit/registry/__init__.py +2 -2
- corekit/registry/registry.py +55 -14
- corekit/schemas/enum.py +22 -1
- corekit/schemas/types.py +6 -1
- corekit/serialization/__init__.py +2 -0
- corekit/serialization/pickle_file.py +61 -0
- corekit/serialization/serializable.py +22 -2
- corekit/serialization/serializer.py +9 -2
- corekit/utils/collections.py +22 -13
- corekit/utils/payload.py +12 -0
- {python_corekit-0.2.0.dist-info → python_corekit-0.3.0.dist-info}/METADATA +7 -7
- python_corekit-0.3.0.dist-info/RECORD +145 -0
- corekit/constants.py +0 -45
- corekit/exceptions/http/exceptions.py +0 -37
- corekit/files/pickle.py +0 -12
- python_corekit-0.2.0.dist-info/RECORD +0 -143
- {python_corekit-0.2.0.dist-info → python_corekit-0.3.0.dist-info}/WHEEL +0 -0
- {python_corekit-0.2.0.dist-info → python_corekit-0.3.0.dist-info}/licenses/LICENSE +0 -0
- {python_corekit-0.2.0.dist-info → python_corekit-0.3.0.dist-info}/top_level.txt +0 -0
corekit/log_monitor/service.py
CHANGED
|
@@ -11,6 +11,7 @@ per container so a crash loop cannot become a restart loop.
|
|
|
11
11
|
"""
|
|
12
12
|
|
|
13
13
|
import re
|
|
14
|
+
import shlex
|
|
14
15
|
import signal
|
|
15
16
|
import subprocess
|
|
16
17
|
import threading
|
|
@@ -51,7 +52,7 @@ class LogMonitor(Watchdog):
|
|
|
51
52
|
config_path: str | None = None,
|
|
52
53
|
config: LogMonitorConfig | None = None,
|
|
53
54
|
notification_service: BaseNotificationService | None = None,
|
|
54
|
-
enforce_label: bool =
|
|
55
|
+
enforce_label: bool = True,
|
|
55
56
|
max_workers: int | None = None,
|
|
56
57
|
docker_host: str | None = None,
|
|
57
58
|
handle_signals: bool = False,
|
|
@@ -61,6 +62,7 @@ class LogMonitor(Watchdog):
|
|
|
61
62
|
:param config: an already-built configuration, instead of a path.
|
|
62
63
|
:param notification_service: where notifications go; logs by default.
|
|
63
64
|
:param enforce_label: only act on containers carrying the managed label.
|
|
65
|
+
On by default, matching Watchdog.
|
|
64
66
|
:param handle_signals: install SIGTERM and SIGINT handlers. Off by
|
|
65
67
|
default because installing them is process-global and only the
|
|
66
68
|
program's entry point should decide that; ``run()`` turns it on.
|
|
@@ -117,12 +119,31 @@ class LogMonitor(Watchdog):
|
|
|
117
119
|
}
|
|
118
120
|
return mapping.get(severity, NotificationType.INFO)
|
|
119
121
|
|
|
120
|
-
def _send_notification(
|
|
121
|
-
|
|
122
|
+
def _send_notification(
|
|
123
|
+
self,
|
|
124
|
+
message: str,
|
|
125
|
+
severity: Severity,
|
|
126
|
+
container_name: str,
|
|
127
|
+
throttle: float = 0,
|
|
128
|
+
) -> bool:
|
|
129
|
+
"""
|
|
130
|
+
Send one notification for a container, honoring its throttle.
|
|
131
|
+
|
|
132
|
+
This is the only delivery path. A YAML notify action and a rule's
|
|
133
|
+
``send_notification`` flag both come through here, so the throttle
|
|
134
|
+
cannot be recorded in one place and ignored in another.
|
|
135
|
+
"""
|
|
136
|
+
last_notify = self.last_notify_times.get(container_name, 0.0)
|
|
137
|
+
if throttle and time.time() - last_notify < throttle:
|
|
138
|
+
self.debug(f"Notification throttled for {container_name}")
|
|
139
|
+
return False
|
|
140
|
+
|
|
141
|
+
self.last_notify_times[container_name] = time.time()
|
|
122
142
|
notification = Notification(
|
|
123
143
|
message=message, type=self._severity_to_notification_type(severity), meta={"source": "LogMonitor"}
|
|
124
144
|
)
|
|
125
145
|
self._notification_service.notify(notification)
|
|
146
|
+
return True
|
|
126
147
|
|
|
127
148
|
def check_rate_limit(self, action_type: ActionType) -> bool:
|
|
128
149
|
"""Check if action is within rate limits"""
|
|
@@ -145,14 +166,17 @@ class LogMonitor(Watchdog):
|
|
|
145
166
|
message = action.message.format(container=container_name, text=matched_text)
|
|
146
167
|
self.info(f"[LogAction] {message}")
|
|
147
168
|
|
|
148
|
-
def _execute_notify_action(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
169
|
+
def _execute_notify_action(
|
|
170
|
+
self,
|
|
171
|
+
action: NotifyAction,
|
|
172
|
+
container_name: str,
|
|
173
|
+
matched_text: str,
|
|
174
|
+
severity: Severity,
|
|
175
|
+
rule_name: str,
|
|
176
|
+
) -> None:
|
|
177
|
+
"""Deliver a notification, applying this action's throttle."""
|
|
178
|
+
message = f"*{rule_name}* in `{container_name}`\n```{matched_text.strip()}```"
|
|
179
|
+
self._send_notification(message, severity, container_name, throttle=action.throttle)
|
|
156
180
|
|
|
157
181
|
def _execute_restart_action(self, action: RestartContainerAction, container_name: str) -> None:
|
|
158
182
|
"""Execute restart action - delegate to Watchdog parent"""
|
|
@@ -172,42 +196,51 @@ class LogMonitor(Watchdog):
|
|
|
172
196
|
self.info(f"Waiting {action.delay}s before restarting {container_name}")
|
|
173
197
|
time.sleep(action.delay)
|
|
174
198
|
|
|
175
|
-
|
|
176
|
-
# Delegate to parent Watchdog method
|
|
177
|
-
container = self._client.containers.get(container_name)
|
|
178
|
-
container.restart()
|
|
199
|
+
if self.restart_container_by_name(container_name):
|
|
179
200
|
self.restart_counts[container_name].append(time.time())
|
|
180
|
-
self.info(f"Restarted container: {container_name}")
|
|
181
|
-
except NotFound:
|
|
182
|
-
self.error(f"Container '{container_name}' not found")
|
|
183
|
-
except APIError as e:
|
|
184
|
-
self.error(f"Failed to restart {container_name}: {e}")
|
|
185
201
|
|
|
186
202
|
def _execute_pause_action(self, action: PauseContainerAction, container_name: str) -> None:
|
|
187
203
|
"""Execute pause action - delegate to Watchdog parent"""
|
|
188
204
|
self.pause_container_by_name(container_name)
|
|
189
205
|
|
|
190
206
|
def _execute_command_action(self, action: ExecuteCommandAction, container_name: str) -> None:
|
|
191
|
-
"""
|
|
192
|
-
command
|
|
207
|
+
"""
|
|
208
|
+
Run a command as an argument list. The config string is not a shell.
|
|
209
|
+
"""
|
|
210
|
+
try:
|
|
211
|
+
argv = [part.format(container=container_name) for part in shlex.split(action.command)]
|
|
212
|
+
except ValueError as exc:
|
|
213
|
+
self.error(f"Invalid command for {container_name}: {exc}")
|
|
214
|
+
return
|
|
215
|
+
if not argv:
|
|
216
|
+
self.error(f"Empty command for {container_name}")
|
|
217
|
+
return
|
|
193
218
|
try:
|
|
194
219
|
result = subprocess.run(
|
|
195
|
-
|
|
196
|
-
shell=
|
|
220
|
+
argv,
|
|
221
|
+
shell=False,
|
|
197
222
|
capture_output=True,
|
|
198
223
|
text=True,
|
|
199
|
-
timeout=30,
|
|
224
|
+
timeout=30,
|
|
200
225
|
)
|
|
226
|
+
rendered = " ".join(argv)
|
|
201
227
|
if result.returncode == 0:
|
|
202
|
-
self.info(f"Executed: {
|
|
228
|
+
self.info(f"Executed: {rendered}\nOutput: {result.stdout}")
|
|
203
229
|
else:
|
|
204
|
-
self.error(f"Command failed: {
|
|
230
|
+
self.error(f"Command failed: {rendered}\nError: {result.stderr}")
|
|
205
231
|
except subprocess.TimeoutExpired:
|
|
206
|
-
self.error(f"Command timed out: {command}")
|
|
232
|
+
self.error(f"Command timed out: {action.command}")
|
|
207
233
|
except Exception as e:
|
|
208
234
|
self.error(f"Failed to execute command: {e}")
|
|
209
235
|
|
|
210
|
-
def execute_action(
|
|
236
|
+
def execute_action(
|
|
237
|
+
self,
|
|
238
|
+
action: Action,
|
|
239
|
+
container_name: str,
|
|
240
|
+
matched_text: str = "",
|
|
241
|
+
severity: Severity = Severity.INFO,
|
|
242
|
+
rule_name: str = "",
|
|
243
|
+
) -> None:
|
|
211
244
|
"""Execute configured actions by delegating to specific action handlers"""
|
|
212
245
|
action_type = action.type
|
|
213
246
|
|
|
@@ -221,7 +254,7 @@ class LogMonitor(Watchdog):
|
|
|
221
254
|
if action_type is ActionType.LOG:
|
|
222
255
|
self._execute_log_action(action, container_name, matched_text)
|
|
223
256
|
elif action_type is ActionType.NOTIFY:
|
|
224
|
-
self._execute_notify_action(action, container_name)
|
|
257
|
+
self._execute_notify_action(action, container_name, matched_text, severity, rule_name)
|
|
225
258
|
elif action_type is ActionType.RESTART_CONTAINER:
|
|
226
259
|
self._execute_restart_action(action, container_name)
|
|
227
260
|
elif action_type is ActionType.PAUSE_CONTAINER:
|
|
@@ -246,14 +279,20 @@ class LogMonitor(Watchdog):
|
|
|
246
279
|
else:
|
|
247
280
|
self.info(log_message)
|
|
248
281
|
|
|
249
|
-
#
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
self.
|
|
282
|
+
# A notify flag is the same delivery path as a notify action.
|
|
283
|
+
# Do not send beside it: that path used to ignore the throttle.
|
|
284
|
+
actions = list(rule.actions)
|
|
285
|
+
if rule.send_notification and not any(action.type is ActionType.NOTIFY for action in actions):
|
|
286
|
+
actions.append(NotifyAction())
|
|
287
|
+
|
|
288
|
+
for action in actions:
|
|
289
|
+
self.execute_action(
|
|
290
|
+
action,
|
|
291
|
+
container_name,
|
|
292
|
+
line,
|
|
293
|
+
severity=rule.severity,
|
|
294
|
+
rule_name=rule.name,
|
|
295
|
+
)
|
|
257
296
|
|
|
258
297
|
# FIXME: does this actually make more sense being part of watchdog?
|
|
259
298
|
def monitor_container(self, container_name: str, rules: list[Rule]) -> None:
|
corekit/notifications/base.py
CHANGED
|
@@ -8,13 +8,15 @@ Subclass ``BaseNotificationService`` and implement ``_send``::
|
|
|
8
8
|
Sends notifications by email.
|
|
9
9
|
'''
|
|
10
10
|
|
|
11
|
-
def _send(self,
|
|
12
|
-
smtp.send(message)
|
|
11
|
+
def _send(self, notification: Notification) -> None:
|
|
12
|
+
smtp.send(notification.message, extra=notification.meta)
|
|
13
13
|
|
|
14
14
|
service.notify(Notification(message="disk full", type=NotificationType.ERROR))
|
|
15
15
|
|
|
16
|
-
Override ``_send``, not ``
|
|
17
|
-
``
|
|
16
|
+
Override ``_send``, not ``notify``, if you only want to replace the transport.
|
|
17
|
+
``notify`` hands the whole ``Notification`` to ``_send``, including ``meta``.
|
|
18
|
+
Events are a different job: they fan a payload out over Redis, they do not
|
|
19
|
+
alert a person.
|
|
18
20
|
"""
|
|
19
21
|
|
|
20
22
|
from corekit.notifications.models import Notification
|
|
@@ -36,16 +38,22 @@ class BaseNotificationService(Loggable):
|
|
|
36
38
|
"""
|
|
37
39
|
Render a notification as the text a transport will send.
|
|
38
40
|
"""
|
|
39
|
-
|
|
41
|
+
text = f"[{notification.type} Notification]: {notification.message}"
|
|
42
|
+
if notification.meta:
|
|
43
|
+
return f"{text} {notification.meta}"
|
|
44
|
+
return text
|
|
40
45
|
|
|
41
|
-
def _send(self,
|
|
46
|
+
def _send(self, notification: Notification) -> None:
|
|
42
47
|
"""
|
|
43
|
-
Deliver
|
|
48
|
+
Deliver a notification. Override this in a subclass.
|
|
49
|
+
|
|
50
|
+
The argument is the notification, not a preformatted string, so a
|
|
51
|
+
transport can use ``message``, ``type``, and ``meta``.
|
|
44
52
|
"""
|
|
45
|
-
self.warning(
|
|
53
|
+
self.warning(self._format(notification))
|
|
46
54
|
|
|
47
55
|
def notify(self, notification: Notification) -> None:
|
|
48
56
|
"""
|
|
49
|
-
|
|
57
|
+
Hand a notification to the transport, including its meta.
|
|
50
58
|
"""
|
|
51
|
-
self._send(
|
|
59
|
+
self._send(notification)
|
|
@@ -16,8 +16,15 @@ are the same concern -- knowing what a running system is doing.
|
|
|
16
16
|
|
|
17
17
|
from corekit.observability.benchmarkable import Benchmarkable
|
|
18
18
|
from corekit.observability.loggable import Loggable
|
|
19
|
-
from corekit.observability.request_context import BaseRequestContext
|
|
19
|
+
from corekit.observability.request_context import BaseRequestContext, RequestContextFilter
|
|
20
20
|
from corekit.observability.timing.split import Split
|
|
21
21
|
from corekit.observability.timing.timer import Timer
|
|
22
22
|
|
|
23
|
-
__all__ = [
|
|
23
|
+
__all__ = [
|
|
24
|
+
"BaseRequestContext",
|
|
25
|
+
"Benchmarkable",
|
|
26
|
+
"Loggable",
|
|
27
|
+
"RequestContextFilter",
|
|
28
|
+
"Split",
|
|
29
|
+
"Timer",
|
|
30
|
+
]
|
|
@@ -1,12 +1,30 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
1
3
|
from corekit.observability.loggable import Loggable
|
|
4
|
+
from corekit.observability.timing.split import Split
|
|
2
5
|
from corekit.observability.timing.timer import Timer
|
|
3
6
|
|
|
4
7
|
|
|
5
8
|
class Benchmarkable(Loggable):
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
"""
|
|
10
|
+
``Loggable`` plus split timing.
|
|
11
|
+
|
|
12
|
+
``__init__`` forwards ``*args``/``**kwargs`` the same way ``Loggable``
|
|
13
|
+
does, so a service can sit in a cooperative ``super()`` chain without
|
|
14
|
+
this class swallowing the arguments the next base expects.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
18
|
+
super().__init__(*args, **kwargs)
|
|
8
19
|
self._timing = Timer()
|
|
9
20
|
|
|
10
|
-
def timing(self, split_name: str | None = None) ->
|
|
11
|
-
|
|
12
|
-
|
|
21
|
+
def timing(self, split_name: str | None = None) -> Split:
|
|
22
|
+
"""
|
|
23
|
+
Log the time since the last split and return it.
|
|
24
|
+
|
|
25
|
+
The log line is unchanged. The return value is for a caller that
|
|
26
|
+
wants the numbers without parsing that line.
|
|
27
|
+
"""
|
|
28
|
+
split = self._timing.split(split_name=split_name)
|
|
29
|
+
self.info(str(split))
|
|
30
|
+
return split
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
2
|
from typing import Any
|
|
3
3
|
|
|
4
|
+
from corekit.observability.request_context import RequestContextFilter
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
class Loggable:
|
|
6
8
|
"""
|
|
@@ -8,10 +10,16 @@ class Loggable:
|
|
|
8
10
|
|
|
9
11
|
Accepts and ignores ``*args``/``**kwargs`` so it can sit anywhere in a
|
|
10
12
|
cooperative ``super().__init__()`` chain.
|
|
13
|
+
|
|
14
|
+
The logger name is the class name, not ``module.Class``. Consuming
|
|
15
|
+
projects filter on that name; renaming it is a log-config change, not a
|
|
16
|
+
local cleanup. Request context is attached as record attributes instead,
|
|
17
|
+
so a formatter can include it without renaming the logger.
|
|
11
18
|
"""
|
|
12
19
|
|
|
13
20
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
14
21
|
self.logger = logging.getLogger(self.__class__.__name__)
|
|
22
|
+
_attach_request_context(self.logger)
|
|
15
23
|
|
|
16
24
|
def debug(self, message: str, **kwargs: Any) -> None:
|
|
17
25
|
self.logger.debug(message, **kwargs)
|
|
@@ -27,3 +35,16 @@ class Loggable:
|
|
|
27
35
|
|
|
28
36
|
def exception(self, message: str, **kwargs: Any) -> None:
|
|
29
37
|
self.logger.exception(message, **kwargs)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def _attach_request_context(logger: logging.Logger) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Attach the request-context filter once per logger name.
|
|
43
|
+
|
|
44
|
+
Loggers are process-global and keyed by name, so every instance of a
|
|
45
|
+
class shares one. Adding the filter again would stamp the same fields
|
|
46
|
+
twice.
|
|
47
|
+
"""
|
|
48
|
+
if any(isinstance(existing, RequestContextFilter) for existing in logger.filters):
|
|
49
|
+
return
|
|
50
|
+
logger.addFilter(RequestContextFilter())
|
|
@@ -27,13 +27,14 @@ Each subclass gets its own storage, so one application's context cannot be read
|
|
|
27
27
|
through another's class.
|
|
28
28
|
"""
|
|
29
29
|
|
|
30
|
+
import logging
|
|
30
31
|
from contextlib import contextmanager
|
|
31
32
|
from contextvars import ContextVar, Token
|
|
32
|
-
from typing import Any, Iterator, Self
|
|
33
|
+
from typing import Any, ClassVar, Iterator, Self
|
|
33
34
|
|
|
34
35
|
from pydantic import BaseModel
|
|
35
36
|
|
|
36
|
-
__all__ = ["BaseRequestContext"]
|
|
37
|
+
__all__ = ["BaseRequestContext", "RequestContextFilter"]
|
|
37
38
|
|
|
38
39
|
|
|
39
40
|
class BaseRequestContext(BaseModel):
|
|
@@ -45,12 +46,18 @@ class BaseRequestContext(BaseModel):
|
|
|
45
46
|
between them leaks the context into whatever runs next on that task.
|
|
46
47
|
"""
|
|
47
48
|
|
|
49
|
+
#: Subclasses that own storage. The logging filter walks this rather than
|
|
50
|
+
#: guessing which application's context class is in use.
|
|
51
|
+
_known_subclasses: ClassVar[list[type["BaseRequestContext"]]] = []
|
|
52
|
+
|
|
48
53
|
def __init_subclass__(cls, **kwargs: Any) -> None:
|
|
49
54
|
"""
|
|
50
55
|
Give every subclass its own ContextVar.
|
|
51
56
|
"""
|
|
52
57
|
super().__init_subclass__(**kwargs)
|
|
53
58
|
cls._context_var = ContextVar(f"{cls.__module__}.{cls.__name__}", default=None)
|
|
59
|
+
if cls not in BaseRequestContext._known_subclasses:
|
|
60
|
+
BaseRequestContext._known_subclasses.append(cls)
|
|
54
61
|
|
|
55
62
|
@classmethod
|
|
56
63
|
def _var(cls) -> ContextVar[Any]:
|
|
@@ -133,3 +140,49 @@ class BaseRequestContext(BaseModel):
|
|
|
133
140
|
yield type(self).current()
|
|
134
141
|
finally:
|
|
135
142
|
type(self).deactivate(token)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
class RequestContextFilter(logging.Filter):
|
|
146
|
+
"""
|
|
147
|
+
Stamp the active request context onto each log record.
|
|
148
|
+
|
|
149
|
+
The message is left alone, so existing log lines and anything that
|
|
150
|
+
matches them stay stable. Fields land on the record for a formatter
|
|
151
|
+
that opts in: ``%(request_context)s`` is a compact summary, and
|
|
152
|
+
``request_context_fields`` is the dumped model. Both are always set,
|
|
153
|
+
to an empty string and an empty dict when nothing is active, so a
|
|
154
|
+
format string that names them does not raise.
|
|
155
|
+
"""
|
|
156
|
+
|
|
157
|
+
def filter(self, record: logging.LogRecord) -> bool:
|
|
158
|
+
fields: dict[str, Any] = {}
|
|
159
|
+
parts: list[str] = []
|
|
160
|
+
for cls in BaseRequestContext._known_subclasses:
|
|
161
|
+
current = cls.current()
|
|
162
|
+
if current is None:
|
|
163
|
+
continue
|
|
164
|
+
dumped = _dump_context(current)
|
|
165
|
+
if not dumped:
|
|
166
|
+
continue
|
|
167
|
+
fields[cls.__name__] = dumped
|
|
168
|
+
rendered = ", ".join(f"{key}={value}" for key, value in dumped.items())
|
|
169
|
+
parts.append(f"{cls.__name__}({rendered})")
|
|
170
|
+
|
|
171
|
+
record.request_context = " ".join(parts)
|
|
172
|
+
record.request_context_fields = fields
|
|
173
|
+
return True
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _dump_context(context: BaseRequestContext) -> dict[str, Any]:
|
|
177
|
+
"""
|
|
178
|
+
JSON-safe fields, or stringified values if a field will not dump.
|
|
179
|
+
"""
|
|
180
|
+
try:
|
|
181
|
+
dumped = context.model_dump(mode="json")
|
|
182
|
+
except Exception:
|
|
183
|
+
dumped = {key: getattr(context, key, None) for key in type(context).model_fields}
|
|
184
|
+
return {key: value if _is_log_safe(value) else str(value) for key, value in dumped.items()}
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _is_log_safe(value: Any) -> bool:
|
|
188
|
+
return value is None or isinstance(value, (str, int, float, bool))
|
|
@@ -6,7 +6,9 @@ from corekit.observability.timing.split import Split
|
|
|
6
6
|
|
|
7
7
|
class Timer:
|
|
8
8
|
def __init__(self, precision: int = DEFAULT_PRECISION) -> None:
|
|
9
|
-
|
|
9
|
+
# perf_counter is monotonic. time.time() can step backwards, which
|
|
10
|
+
# makes a split look negative for no reason the caller can act on.
|
|
11
|
+
_time = time.perf_counter()
|
|
10
12
|
self.start = _time
|
|
11
13
|
self.latest = _time
|
|
12
14
|
self.num = 0
|
|
@@ -17,7 +19,7 @@ class Timer:
|
|
|
17
19
|
return round(value, self.precision)
|
|
18
20
|
|
|
19
21
|
def split(self, split_name: str | None = None) -> Split:
|
|
20
|
-
_time = time.
|
|
22
|
+
_time = time.perf_counter()
|
|
21
23
|
self.num += 1
|
|
22
24
|
split = Split(
|
|
23
25
|
num=self.num,
|
corekit/registry/__init__.py
CHANGED
|
@@ -12,6 +12,6 @@ duplicates. Reach for it when position is what matters and no name is needed.
|
|
|
12
12
|
"""
|
|
13
13
|
|
|
14
14
|
from corekit.registry.ordered import OrderedRegistry
|
|
15
|
-
from corekit.registry.registry import SmartRegistry
|
|
15
|
+
from corekit.registry.registry import SmartRegistry, normalize_key
|
|
16
16
|
|
|
17
|
-
__all__ = ["OrderedRegistry", "SmartRegistry"]
|
|
17
|
+
__all__ = ["OrderedRegistry", "SmartRegistry", "normalize_key"]
|
corekit/registry/registry.py
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
+
import logging
|
|
1
2
|
import re
|
|
2
3
|
from typing import Any, Iterator
|
|
3
4
|
|
|
5
|
+
from corekit.config import get_settings
|
|
6
|
+
|
|
4
7
|
REPLACEMENT_CHAR = "-"
|
|
5
8
|
NORMALIZATION_PATTERN = re.compile(r"[\s_-]+")
|
|
6
9
|
# Split CamelCase into words: "HTTPServerError" -> "HTTP-Server-Error".
|
|
7
10
|
CAMEL_BOUNDARY_PATTERN = re.compile(r"(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])")
|
|
8
11
|
|
|
12
|
+
_logger = logging.getLogger("SmartRegistry")
|
|
13
|
+
_MISSING = object()
|
|
14
|
+
|
|
9
15
|
|
|
10
16
|
class SmartRegistry:
|
|
11
17
|
"""
|
|
@@ -31,19 +37,33 @@ class SmartRegistry:
|
|
|
31
37
|
"""
|
|
32
38
|
Retrieve an item from the registry using a normalized key.
|
|
33
39
|
"""
|
|
34
|
-
return self.__registry__[
|
|
40
|
+
return self.__registry__[normalize_key(key)]
|
|
35
41
|
|
|
36
42
|
def __setitem__(self, key: str, value: Any) -> None:
|
|
37
43
|
"""
|
|
38
44
|
Store an item in the registry with a normalized key.
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
|
|
46
|
+
A second write to the same normalized key used to replace the first
|
|
47
|
+
with no signal. That drops a handler that spelled the same name a
|
|
48
|
+
different way. The replacement is still allowed so an intentional
|
|
49
|
+
update works; it is warned, and refused when ``strict_mode`` is on.
|
|
50
|
+
"""
|
|
51
|
+
normalized = normalize_key(key)
|
|
52
|
+
existing = self.__registry__.get(normalized, _MISSING)
|
|
53
|
+
if existing is not _MISSING and existing is not value:
|
|
54
|
+
if _strict_mode():
|
|
55
|
+
raise ValueError(
|
|
56
|
+
f"Registry already contains {normalized!r}. "
|
|
57
|
+
f"strict_mode refuses a second registration under the same key."
|
|
58
|
+
)
|
|
59
|
+
_logger.warning(f"Overwriting registry key {normalized!r}")
|
|
60
|
+
self.__registry__[normalized] = value
|
|
41
61
|
|
|
42
62
|
def __delitem__(self, key: str) -> None:
|
|
43
63
|
"""
|
|
44
64
|
Remove an item from the registry.
|
|
45
65
|
"""
|
|
46
|
-
del self.__registry__[
|
|
66
|
+
del self.__registry__[normalize_key(key)]
|
|
47
67
|
|
|
48
68
|
def __iter__(self) -> Iterator[str]:
|
|
49
69
|
"""
|
|
@@ -73,7 +93,7 @@ class SmartRegistry:
|
|
|
73
93
|
"""
|
|
74
94
|
Check if a normalized key exists in the registry.
|
|
75
95
|
"""
|
|
76
|
-
return
|
|
96
|
+
return normalize_key(key) in self.__registry__
|
|
77
97
|
|
|
78
98
|
def __str__(self) -> str:
|
|
79
99
|
"""
|
|
@@ -114,21 +134,42 @@ class SmartRegistry:
|
|
|
114
134
|
self.__registry__ = state
|
|
115
135
|
|
|
116
136
|
@staticmethod
|
|
117
|
-
def
|
|
137
|
+
def normalize_key(key: str) -> str:
|
|
118
138
|
"""
|
|
119
139
|
Reduce a key to a canonical hyphenated form.
|
|
120
140
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
and ``"Greeting Handler"`` all normalize to ``"greeting-handler"``.
|
|
124
|
-
Without the CamelCase step a class registered under its ``__name__``
|
|
125
|
-
could not be found by the snake_case name a caller would naturally type.
|
|
141
|
+
Public so callers do not have to reach for the old private name.
|
|
142
|
+
``__normalize_key__`` remains as an alias.
|
|
126
143
|
"""
|
|
127
|
-
|
|
128
|
-
|
|
144
|
+
return normalize_key(key)
|
|
145
|
+
|
|
146
|
+
# Kept so existing callers, including ThreadLocalRegistry and tests, keep
|
|
147
|
+
# working. New code should call ``normalize_key``.
|
|
148
|
+
__normalize_key__ = staticmethod(normalize_key)
|
|
129
149
|
|
|
130
150
|
def get(self, key: str, fallback: Any = None) -> Any:
|
|
131
151
|
"""
|
|
132
152
|
Safely retrieve an item from the registry with an optional fallback.
|
|
133
153
|
"""
|
|
134
|
-
return self.__registry__.get(
|
|
154
|
+
return self.__registry__.get(normalize_key(key), fallback)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _strict_mode() -> bool:
|
|
158
|
+
"""
|
|
159
|
+
Whether a colliding key should be refused.
|
|
160
|
+
"""
|
|
161
|
+
return get_settings().standards.strict_mode
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def normalize_key(key: str) -> str:
|
|
165
|
+
"""
|
|
166
|
+
Reduce a key to a canonical hyphenated form.
|
|
167
|
+
|
|
168
|
+
Word boundaries are taken from CamelCase as well as from whitespace,
|
|
169
|
+
underscores and hyphens, so ``"GreetingHandler"``, ``"greeting_handler"``
|
|
170
|
+
and ``"Greeting Handler"`` all normalize to ``"greeting-handler"``.
|
|
171
|
+
Without the CamelCase step a class registered under its ``__name__``
|
|
172
|
+
could not be found by the snake_case name a caller would naturally type.
|
|
173
|
+
"""
|
|
174
|
+
spaced = CAMEL_BOUNDARY_PATTERN.sub(REPLACEMENT_CHAR, key.strip())
|
|
175
|
+
return NORMALIZATION_PATTERN.sub(REPLACEMENT_CHAR, spaced.lower()).strip(REPLACEMENT_CHAR)
|
corekit/schemas/enum.py
CHANGED
|
@@ -18,7 +18,7 @@ class ValidatingEnum(Enum):
|
|
|
18
18
|
def validate_and_create(cls, value: Any) -> "ValidatingEnum":
|
|
19
19
|
if cls.is_member(value):
|
|
20
20
|
return cls(value)
|
|
21
|
-
raise ValueError(f"{cls.__name__} does not contain {
|
|
21
|
+
raise ValueError(f"{cls.__name__} does not contain {value!r}")
|
|
22
22
|
|
|
23
23
|
@classmethod
|
|
24
24
|
def get_all_members(cls) -> list["ValidatingEnum"]:
|
|
@@ -32,6 +32,15 @@ class ValidatingEnum(Enum):
|
|
|
32
32
|
|
|
33
33
|
|
|
34
34
|
class StringEnum(str, ValidatingEnum):
|
|
35
|
+
"""
|
|
36
|
+
String-valued enum that stringifies to its value.
|
|
37
|
+
|
|
38
|
+
Plain ``(str, Enum)`` members compare equal to their value but ``str()`` /
|
|
39
|
+
f-strings still render as ``ClassName.MEMBER``. Override that so DB defaults,
|
|
40
|
+
status comparisons, and log lines get ``"pending"`` rather than
|
|
41
|
+
``"AccountRequestStatus.PENDING"``.
|
|
42
|
+
"""
|
|
43
|
+
|
|
35
44
|
@classmethod
|
|
36
45
|
def from_string(cls, value: str) -> "StringEnum":
|
|
37
46
|
"""
|
|
@@ -39,6 +48,12 @@ class StringEnum(str, ValidatingEnum):
|
|
|
39
48
|
"""
|
|
40
49
|
return cls.validate_and_create(value)
|
|
41
50
|
|
|
51
|
+
def __str__(self) -> str:
|
|
52
|
+
return str(self.value)
|
|
53
|
+
|
|
54
|
+
def __format__(self, format_spec: str) -> str:
|
|
55
|
+
return self.value.__format__(format_spec)
|
|
56
|
+
|
|
42
57
|
|
|
43
58
|
class IntegerEnum(int, ValidatingEnum):
|
|
44
59
|
@classmethod
|
|
@@ -47,3 +62,9 @@ class IntegerEnum(int, ValidatingEnum):
|
|
|
47
62
|
Convert an integer to the corresponding enum member.
|
|
48
63
|
"""
|
|
49
64
|
return cls.validate_and_create(value)
|
|
65
|
+
|
|
66
|
+
def __str__(self) -> str:
|
|
67
|
+
return str(self.value)
|
|
68
|
+
|
|
69
|
+
def __format__(self, format_spec: str) -> str:
|
|
70
|
+
return self.value.__format__(format_spec)
|
corekit/schemas/types.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from datetime import date, datetime
|
|
2
|
-
from typing import Any, Sequence
|
|
2
|
+
from typing import Any, Awaitable, Callable, Coroutine, Sequence
|
|
3
3
|
|
|
4
4
|
# ========== Primitive Types ==========
|
|
5
5
|
Number = int | float
|
|
@@ -38,3 +38,8 @@ UnknownSet = set[Any]
|
|
|
38
38
|
|
|
39
39
|
# ========== Date Types ==========
|
|
40
40
|
ArbitraryDate = date | datetime
|
|
41
|
+
|
|
42
|
+
# ========== Function Types ==========
|
|
43
|
+
AsyncFunction = Callable[..., Awaitable[Any] | Coroutine[Any, Any, Any]]
|
|
44
|
+
SyncFunction = Callable[..., Any]
|
|
45
|
+
ArbitraryFunction = AsyncFunction | SyncFunction
|
|
@@ -10,10 +10,12 @@ Set a key with ``COREKIT_SERIALIZATION__KEY`` to use an executing engine.
|
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
12
|
from corekit.serialization.enum import SerializerEngine
|
|
13
|
+
from corekit.serialization.pickle_file import PickleFileManager
|
|
13
14
|
from corekit.serialization.serializable import Serializable
|
|
14
15
|
from corekit.serialization.serializer import Serializer, SignatureError, UnsafeEngineError
|
|
15
16
|
|
|
16
17
|
__all__ = [
|
|
18
|
+
"PickleFileManager",
|
|
17
19
|
"Serializable",
|
|
18
20
|
"Serializer",
|
|
19
21
|
"SerializerEngine",
|