narrativetrace-structlog 0.1.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.
- narrativetrace_structlog-0.1.0/.gitignore +28 -0
- narrativetrace_structlog-0.1.0/LICENSE +99 -0
- narrativetrace_structlog-0.1.0/PKG-INFO +29 -0
- narrativetrace_structlog-0.1.0/README.md +13 -0
- narrativetrace_structlog-0.1.0/pyproject.toml +30 -0
- narrativetrace_structlog-0.1.0/src/narrativetrace_structlog/__init__.py +36 -0
- narrativetrace_structlog-0.1.0/src/narrativetrace_structlog/py.typed +0 -0
- narrativetrace_structlog-0.1.0/tests/test_processor.py +66 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
.venv/
|
|
4
|
+
.pytest_cache/
|
|
5
|
+
.mypy_cache/
|
|
6
|
+
.ruff_cache/
|
|
7
|
+
.hypothesis/
|
|
8
|
+
.mutmut-cache
|
|
9
|
+
mutants/
|
|
10
|
+
# pytest-benchmark autosave (poe bench-gate): per-machine comparison history, never comparable
|
|
11
|
+
# across machines -- stays local to whichever machine runs the nightly, like .venv/.uv-cache.
|
|
12
|
+
.benchmarks/
|
|
13
|
+
htmlcov/
|
|
14
|
+
.coverage
|
|
15
|
+
.coverage.*
|
|
16
|
+
dist/
|
|
17
|
+
build/
|
|
18
|
+
*.egg-info/
|
|
19
|
+
|
|
20
|
+
# Cache for security-tool binaries auto-fetched by scripts/run_security_tool.py
|
|
21
|
+
.tools/
|
|
22
|
+
|
|
23
|
+
# Local tooling state (contains credentials — never commit)
|
|
24
|
+
.devcontainer/claude-home/
|
|
25
|
+
.agent-run*
|
|
26
|
+
|
|
27
|
+
# Local tooling state (holds host-absolute paths)
|
|
28
|
+
.claude/settings.local.json
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
License text copyright © 2017 MariaDB Corporation Ab, All Rights Reserved.
|
|
4
|
+
"Business Source License" is a trademark of MariaDB Corporation Ab.
|
|
5
|
+
|
|
6
|
+
Parameters
|
|
7
|
+
|
|
8
|
+
Licensor: Empower Agile
|
|
9
|
+
|
|
10
|
+
Licensed Work: NarrativeTrace for Python (the narrativetrace PyPI distributions) version {{VERSION}}. The Licensed Work is (c) 2026 Empower Agile.
|
|
11
|
+
|
|
12
|
+
Additional Use Grant: You may make production use of the Licensed Work for any
|
|
13
|
+
purpose, including internal use and use in products and
|
|
14
|
+
services you provide to your own customers, provided that
|
|
15
|
+
such production use does not include offering the
|
|
16
|
+
Licensed Work, or a product or service whose value
|
|
17
|
+
derives substantially from the Licensed Work, to third
|
|
18
|
+
parties as a logging, tracing, or code-narrative product
|
|
19
|
+
or service.
|
|
20
|
+
|
|
21
|
+
Change Date: Four years from the date the Licensed Work is published. ({{CHANGE_DATE}} for this version)
|
|
22
|
+
|
|
23
|
+
Change License: Apache License, Version 2.0
|
|
24
|
+
|
|
25
|
+
For information about alternative licensing arrangements for the Licensed
|
|
26
|
+
Work, please contact Empower Agile at hello@narrativetrace.ai.
|
|
27
|
+
|
|
28
|
+
Notice
|
|
29
|
+
|
|
30
|
+
The Business Source License (this document, or the "License") is not an Open
|
|
31
|
+
Source license. However, the Licensed Work will eventually be made available
|
|
32
|
+
under an Open Source License, as stated in this License.
|
|
33
|
+
|
|
34
|
+
Terms
|
|
35
|
+
|
|
36
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
37
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
38
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
39
|
+
production use.
|
|
40
|
+
|
|
41
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
42
|
+
available distribution of a specific version of the Licensed Work under this
|
|
43
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
44
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
45
|
+
above terminate.
|
|
46
|
+
|
|
47
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
48
|
+
currently in effect as described in this License, you must purchase a
|
|
49
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
50
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
51
|
+
|
|
52
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
53
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
54
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
55
|
+
for each version of the Licensed Work released by Licensor.
|
|
56
|
+
|
|
57
|
+
You must conspicuously display this License on each original or modified copy
|
|
58
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
59
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
60
|
+
License apply to your use of that work.
|
|
61
|
+
|
|
62
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
63
|
+
terminate your rights under this License for the current and all other
|
|
64
|
+
versions of the Licensed Work.
|
|
65
|
+
|
|
66
|
+
This License does not grant you any right in any trademark or logo of
|
|
67
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
68
|
+
Licensor as expressly required by this License).
|
|
69
|
+
|
|
70
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
71
|
+
AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
72
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
73
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
74
|
+
TITLE.
|
|
75
|
+
|
|
76
|
+
MariaDB hereby grants you permission to use this License’s text to license
|
|
77
|
+
your works, and to refer to it using the trademark “Business Source License”,
|
|
78
|
+
as long as you comply with the Covenants of Licensor below.
|
|
79
|
+
|
|
80
|
+
Covenants of Licensor
|
|
81
|
+
|
|
82
|
+
In consideration of the right to use this License’s text and the “Business
|
|
83
|
+
Source License” name and trademark, Licensor covenants to MariaDB, and to all
|
|
84
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
85
|
+
|
|
86
|
+
1. To specify as the Change License the GPL Version 2.0 or any later version,
|
|
87
|
+
or a license that is compatible with GPL Version 2.0 or a later version,
|
|
88
|
+
where “compatible” means that software provided under the Change License can
|
|
89
|
+
be included in a program with software provided under GPL Version 2.0 or a
|
|
90
|
+
later version. Licensor may specify additional Change Licenses without
|
|
91
|
+
limitation.
|
|
92
|
+
|
|
93
|
+
2. To either: (a) specify an additional grant of rights to use that does not
|
|
94
|
+
impose any additional restriction on the right granted in this License, as
|
|
95
|
+
the Additional Use Grant; or (b) insert the text “None”.
|
|
96
|
+
|
|
97
|
+
3. To specify a Change Date.
|
|
98
|
+
|
|
99
|
+
4. Not to modify this License in any other way.
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: narrativetrace-structlog
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: structlog processor injecting narrativetrace correlation keys
|
|
5
|
+
Project-URL: Homepage, https://github.com/narrativetrace/narrativetrace-python
|
|
6
|
+
Project-URL: Source, https://github.com/narrativetrace/narrativetrace-python
|
|
7
|
+
Author: narrativetrace contributors
|
|
8
|
+
License-Expression: BUSL-1.1
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Typing :: Typed
|
|
11
|
+
Requires-Python: >=3.12
|
|
12
|
+
Requires-Dist: narrativetrace
|
|
13
|
+
Provides-Extra: structlog
|
|
14
|
+
Requires-Dist: structlog>=24; extra == 'structlog'
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# narrativetrace-structlog
|
|
18
|
+
|
|
19
|
+
A [structlog](https://www.structlog.org) processor that injects [narrativetrace](../narrativetrace)
|
|
20
|
+
correlation keys (`traceId`, `traceName`, `spanId`, `nt.class`, HTTP/request keys, …) into every
|
|
21
|
+
event dict — the same canonical key set the stdlib `NarrativeContextFilter` stamps onto log
|
|
22
|
+
records, so both logging front-ends stay in lockstep.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
import structlog
|
|
26
|
+
from narrativetrace_structlog import narrative_context_processor
|
|
27
|
+
|
|
28
|
+
structlog.configure(processors=[narrative_context_processor, structlog.processors.JSONRenderer()])
|
|
29
|
+
```
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# narrativetrace-structlog
|
|
2
|
+
|
|
3
|
+
A [structlog](https://www.structlog.org) processor that injects [narrativetrace](../narrativetrace)
|
|
4
|
+
correlation keys (`traceId`, `traceName`, `spanId`, `nt.class`, HTTP/request keys, …) into every
|
|
5
|
+
event dict — the same canonical key set the stdlib `NarrativeContextFilter` stamps onto log
|
|
6
|
+
records, so both logging front-ends stay in lockstep.
|
|
7
|
+
|
|
8
|
+
```python
|
|
9
|
+
import structlog
|
|
10
|
+
from narrativetrace_structlog import narrative_context_processor
|
|
11
|
+
|
|
12
|
+
structlog.configure(processors=[narrative_context_processor, structlog.processors.JSONRenderer()])
|
|
13
|
+
```
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "narrativetrace-structlog"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "structlog processor injecting narrativetrace correlation keys"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "BUSL-1.1"
|
|
8
|
+
# See packages/narrativetrace/pyproject.toml: one byte-identical copy of the root LICENSE per
|
|
9
|
+
# distribution, because PEP 639 forbids reaching out of the project directory.
|
|
10
|
+
license-files = ["LICENSE"]
|
|
11
|
+
authors = [{ name = "narrativetrace contributors" }]
|
|
12
|
+
classifiers = ["Typing :: Typed"]
|
|
13
|
+
dependencies = ["narrativetrace"]
|
|
14
|
+
|
|
15
|
+
[project.urls]
|
|
16
|
+
Homepage = "https://github.com/narrativetrace/narrativetrace-python"
|
|
17
|
+
Source = "https://github.com/narrativetrace/narrativetrace-python"
|
|
18
|
+
|
|
19
|
+
[project.optional-dependencies]
|
|
20
|
+
structlog = ["structlog>=24"]
|
|
21
|
+
|
|
22
|
+
[build-system]
|
|
23
|
+
requires = ["hatchling"]
|
|
24
|
+
build-backend = "hatchling.build"
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.wheel]
|
|
27
|
+
packages = ["src/narrativetrace_structlog"]
|
|
28
|
+
|
|
29
|
+
[dependency-groups]
|
|
30
|
+
dev = ["structlog>=24"]
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""structlog processor injecting narrativetrace correlation keys.
|
|
2
|
+
|
|
3
|
+
The SLF4J/MDC enrichment onto structlog. :func:`narrative_context_processor`
|
|
4
|
+
merges the *same* canonical keys as the stdlib :class:`~narrativetrace.NarrativeContextFilter`
|
|
5
|
+
(via :func:`narrativetrace.current_scope_keys`) into every structlog event dict, so the two
|
|
6
|
+
logging front-ends never drift — the cautionary tale being the TypeScript runtime's
|
|
7
|
+
enricher-vs-winston/pino key drift.
|
|
8
|
+
|
|
9
|
+
Usage::
|
|
10
|
+
|
|
11
|
+
import structlog
|
|
12
|
+
from narrativetrace_structlog import narrative_context_processor
|
|
13
|
+
|
|
14
|
+
structlog.configure(
|
|
15
|
+
processors=[narrative_context_processor, structlog.processors.JSONRenderer()]
|
|
16
|
+
)
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from typing import Any
|
|
22
|
+
|
|
23
|
+
from narrativetrace import current_scope_keys
|
|
24
|
+
|
|
25
|
+
__version__ = "0.1.0"
|
|
26
|
+
|
|
27
|
+
__all__ = ["narrative_context_processor"]
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def narrative_context_processor(
|
|
31
|
+
_logger: Any, _method_name: str, event_dict: dict[str, Any]
|
|
32
|
+
) -> dict[str, Any]:
|
|
33
|
+
"""A structlog processor adding the current scope's correlation keys (without overwriting)."""
|
|
34
|
+
for key, value in current_scope_keys().items():
|
|
35
|
+
event_dict.setdefault(key, value)
|
|
36
|
+
return event_dict
|
|
File without changes
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"""Pins the structlog processor to the same key set as the stdlib NarrativeContextFilter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
import pytest
|
|
8
|
+
from narrativetrace_structlog import narrative_context_processor
|
|
9
|
+
|
|
10
|
+
from narrativetrace.events import EnterEvent
|
|
11
|
+
from narrativetrace.ids import SpanId, TraceId
|
|
12
|
+
from narrativetrace.logging_bridge import (
|
|
13
|
+
_REQUEST_SCOPE,
|
|
14
|
+
_SCOPE_STACK,
|
|
15
|
+
LoggingTraceConsumer,
|
|
16
|
+
NarrativeContextFilter,
|
|
17
|
+
request_log_scope,
|
|
18
|
+
)
|
|
19
|
+
from narrativetrace.signature import MethodSignature
|
|
20
|
+
from narrativetrace.span import SpanContext
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@pytest.fixture(autouse=True)
|
|
24
|
+
def _reset_scope() -> None:
|
|
25
|
+
_SCOPE_STACK.set(None)
|
|
26
|
+
_REQUEST_SCOPE.set(None)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _enter_span() -> None:
|
|
30
|
+
span = SpanContext(TraceId("0" * 32), SpanId("a" * 16))
|
|
31
|
+
LoggingTraceConsumer().accept(EnterEvent(span, 0, MethodSignature("OrderService", "place", [])))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class TestProcessor:
|
|
35
|
+
def test_injects_span_keys(self) -> None:
|
|
36
|
+
_enter_span()
|
|
37
|
+
event = narrative_context_processor(None, "info", {"event": "hi"})
|
|
38
|
+
assert event["traceId"] == "0" * 32
|
|
39
|
+
assert event["nt.class"] == "OrderService"
|
|
40
|
+
assert event["nt.method"] == "place"
|
|
41
|
+
|
|
42
|
+
def test_injects_request_scope_keys(self) -> None:
|
|
43
|
+
with request_log_scope({"httpMethod": "GET", "traceId": "req"}):
|
|
44
|
+
event = narrative_context_processor(None, "info", {"event": "hi"})
|
|
45
|
+
assert event["httpMethod"] == "GET"
|
|
46
|
+
|
|
47
|
+
def test_does_not_overwrite_existing_event_keys(self) -> None:
|
|
48
|
+
_enter_span()
|
|
49
|
+
event = narrative_context_processor(None, "info", {"traceId": "preset"})
|
|
50
|
+
assert event["traceId"] == "preset"
|
|
51
|
+
|
|
52
|
+
def test_no_keys_outside_a_trace(self) -> None:
|
|
53
|
+
assert narrative_context_processor(None, "info", {"event": "hi"}) == {"event": "hi"}
|
|
54
|
+
|
|
55
|
+
def test_emits_identical_keys_to_stdlib_filter(self) -> None:
|
|
56
|
+
"""The structlog processor and the stdlib filter must produce the same key set."""
|
|
57
|
+
_enter_span()
|
|
58
|
+
with request_log_scope({"httpMethod": "GET"}):
|
|
59
|
+
processor_keys = set(narrative_context_processor(None, "info", {})) - {"event"}
|
|
60
|
+
|
|
61
|
+
record = logging.LogRecord("t", logging.INFO, __file__, 1, "m", None, None)
|
|
62
|
+
NarrativeContextFilter().filter(record)
|
|
63
|
+
base = set(logging.LogRecord("t", logging.INFO, __file__, 1, "m", None, None).__dict__)
|
|
64
|
+
filter_keys = set(record.__dict__) - base
|
|
65
|
+
|
|
66
|
+
assert processor_keys == filter_keys
|