Skip to content

Allow CallsiteParameterAdder to use custom event dict keys - #838

Open
pranjalm37 wants to merge 1 commit into
hynek:mainfrom
pranjalm37:feat/callsite-parameter-adder-rename-keys-553
Open

Allow CallsiteParameterAdder to use custom event dict keys#838
pranjalm37 wants to merge 1 commit into
hynek:mainfrom
pranjalm37:feat/callsite-parameter-adder-rename-keys-553

Conversation

@pranjalm37

Copy link
Copy Markdown

Summary

Closes #553.

CallsiteParameterAdder.parameters currently only accepts a Collection[CallsiteParameter], and each parameter is always keyed in the event dict by its own CallsiteParameter.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: parameters now also accepts a Mapping[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.

CallsiteParameterAdder({"tid": CallsiteParameter.THREAD, "pid": CallsiteParameter.PROCESS})

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_handlers now stores (key, handler) tuples instead of (CallsiteParameter, handler). key is either the mapping's key (custom) or parameter.value (default), computed once in __init__.
  • Both code paths in __call__ — the structlog-native path (_active_handlers) and the foreign-LogRecord path (_record_mappings) — use the resolved key consistently, so custom keys work whether the event originated from structlog or from stdlib logging.

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).
  • Full suite: pytest tests/ → 903 passed (up from 900 pre-change), 21 skipped.
  • mypy src/structlog/processors.py clean.
  • ruff check / ruff format --check clean.
  • Added a changelog entry under ## [Unreleased].

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
@codspeed-hq

codspeed-hq Bot commented Aug 8, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 10 untouched benchmarks


Comparing pranjalm37:feat/callsite-parameter-adder-rename-keys-553 (54a85ac) with main (bf3cfd0)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (ab24a26) during the generation of this report, so bf3cfd0 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow renaming keys added by CallsiteParameterAdder

1 participant