Allow CallsiteParameterAdder to use custom event dict keys - #838
Open
pranjalm37 wants to merge 1 commit into
Open
Allow CallsiteParameterAdder to use custom event dict keys#838pranjalm37 wants to merge 1 commit into
pranjalm37 wants to merge 1 commit into
Conversation
parameters now also accepts a Mapping[str, CallsiteParameter] in addition to a plain Collection[CallsiteParameter]. When a mapping is given, its keys are used as the event dictionary keys instead of the default CallsiteParameter.value strings -- useful for conforming to a third-party log ingest pipeline's expected field names (e.g. ECS, Datadog) without a separate renaming processor. Passing a plain collection (the existing usage) is unaffected: each parameter is still keyed by its own enum value. Closes hynek#553
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #553.
CallsiteParameterAdder.parameterscurrently only accepts aCollection[CallsiteParameter], and each parameter is always keyed in the event dict by its ownCallsiteParameter.value(e.g."lineno","func_name"). There's no way to use a different key without a separate renaming processor.This implements exactly the API proposed in the issue:
parametersnow also accepts aMapping[str, CallsiteParameter]. When a mapping is passed, its keys become the event dict keys instead of the default enum values — useful for conforming to a third-party log ingest pipeline's expected field names (the issue mentions ECS and Datadog specifically) without a second processor.Passing a plain collection — all existing usage — is completely unaffected: each parameter is still keyed by its own enum value, exactly as before.
Note for maintainer
I know this is a feature request rather than a bug fix, and the API shape here was proposed by the original reporter, not something I've gotten sign-off on. I implemented it as described since it looked genuinely small, useful, and backwards-compatible, but I'm very open to a different shape (or to this not being the direction you want at all) — happy to adjust or close if so.
Implementation
_active_handlersnow stores(key, handler)tuples instead of(CallsiteParameter, handler).keyis either the mapping's key (custom) orparameter.value(default), computed once in__init__.__call__— the structlog-native path (_active_handlers) and the foreign-LogRecordpath (_record_mappings) — use the resolved key consistently, so custom keys work whether the event originated from structlog or from stdliblogging.Test plan
test_custom_keys_structlog_originated— mapping keys show up correctly for structlog-native calls.test_custom_keys_foreign_log_record— same, for the stdlib-LogRecord-derived path.test_custom_keys_pickleable— a mapping-configured instance is still picklable (the class is explicitly designed to be, for propagating config to subprocesses).pytest tests/→ 903 passed (up from 900 pre-change), 21 skipped.mypy src/structlog/processors.pyclean.ruff check/ruff format --checkclean.## [Unreleased].