Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CallsiteParameterAdder to be pickled #603

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ You can find our backwards-compatibility policy [here](https://github.com/hynek/
- It is now possible to disable log level-padding in `structlog.dev.LogLevelColumnFormatter` and `structlog.dev.ConsoleRenderer`.
[#599](https://github.com/hynek/structlog/pull/599)

- The `structlog.processors.CallsiteParameterAdder` can now be pickled.
[#603](https://github.com/hynek/structlog/pull/603)


### Changed
Expand Down
76 changes: 49 additions & 27 deletions src/structlog/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,46 @@ class CallsiteParameter(enum.Enum):
PROCESS_NAME = "process_name"


def _get_callsite_pathname(module: str, frame_info: inspect.Traceback) -> Any:
return frame_info.filename


def _get_callsite_filename(module: str, frame_info: inspect.Traceback) -> Any:
return os.path.basename(frame_info.filename)


def _get_callsite_module(module: str, frame_info: inspect.Traceback) -> Any:
return os.path.splitext(os.path.basename(frame_info.filename))[0]


def _get_callsite_func_name(module: str, frame_info: inspect.Traceback) -> Any:
return frame_info.function


def _get_callsite_lineno(module: str, frame_info: inspect.Traceback) -> Any:
return frame_info.lineno


def _get_callsite_thread(module: str, frame_info: inspect.Traceback) -> Any:
return threading.get_ident()


def _get_callsite_thread_name(
module: str, frame_info: inspect.Traceback
) -> Any:
return threading.current_thread().name


def _get_callsite_process(module: str, frame_info: inspect.Traceback) -> Any:
return os.getpid()


def _get_callsite_process_name(
module: str, frame_info: inspect.Traceback
) -> Any:
return get_processname()


class CallsiteParameterAdder:
"""
Adds parameters of the callsite that an event dictionary originated from to
Expand Down Expand Up @@ -767,33 +807,15 @@ class CallsiteParameterAdder:
_handlers: ClassVar[
dict[CallsiteParameter, Callable[[str, inspect.Traceback], Any]]
] = {
CallsiteParameter.PATHNAME: (
lambda module, frame_info: frame_info.filename
),
CallsiteParameter.FILENAME: (
lambda module, frame_info: os.path.basename(frame_info.filename)
),
CallsiteParameter.MODULE: (
lambda module, frame_info: os.path.splitext(
os.path.basename(frame_info.filename)
)[0]
),
CallsiteParameter.FUNC_NAME: (
lambda module, frame_info: frame_info.function
),
CallsiteParameter.LINENO: (
lambda module, frame_info: frame_info.lineno
),
CallsiteParameter.THREAD: (
lambda module, frame_info: threading.get_ident()
),
CallsiteParameter.THREAD_NAME: (
lambda module, frame_info: threading.current_thread().name
),
CallsiteParameter.PROCESS: (lambda module, frame_info: os.getpid()),
CallsiteParameter.PROCESS_NAME: (
lambda module, frame_info: get_processname()
),
CallsiteParameter.PATHNAME: _get_callsite_pathname,
CallsiteParameter.FILENAME: _get_callsite_filename,
CallsiteParameter.MODULE: _get_callsite_module,
CallsiteParameter.FUNC_NAME: _get_callsite_func_name,
CallsiteParameter.LINENO: _get_callsite_lineno,
CallsiteParameter.THREAD: _get_callsite_thread,
CallsiteParameter.THREAD_NAME: _get_callsite_thread_name,
CallsiteParameter.PROCESS: _get_callsite_process,
CallsiteParameter.PROCESS_NAME: _get_callsite_process_name,
}
_record_attribute_map: ClassVar[dict[CallsiteParameter, str]] = {
CallsiteParameter.PATHNAME: "pathname",
Expand Down
9 changes: 9 additions & 0 deletions tests/processors/test_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import json
import logging
import os
import pickle
import sys
import threading

Expand Down Expand Up @@ -492,6 +493,14 @@ def test_e2e(

assert expected == actual

def test_pickeable_callsite_parameter_adder(self) -> None:
"""
An instance of ``CallsiteParameterAdder`` can be pickled. This
functionality may be used to propagate structlog configurations to
subprocesses.
"""
pickle.dumps(CallsiteParameterAdder())

@classmethod
def make_processor(
cls,
Expand Down