structlog icon indicating copy to clipboard operation
structlog copied to clipboard

Allow renaming keys added by CallsiteParameterAdder

Open Cnoor0171 opened this issue 2 years ago • 4 comments

Currently, CallsiteParameterAdder always adds each parameter under a predetermined key. It would be nice if the user can specify an alternate key for each parameter instead.

A pretty simple to implement and fully backwards compatible way would be to change the init method from

    def __init__(
        self,
        parameters: Collection[CallsiteParameter] = _all_parameters,
        additional_ignores: list[str] | None = None,
    ) -> None:

to

    def __init__(
        self,
        parameters: Collection[CallsiteParameter] | Mapping[str, CallsiteParameter] = _all_parameters,
        additional_ignores: list[str] | None = None,
    ) -> None:

The user can then instantiate the processor as

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

Cnoor0171 avatar Sep 14 '23 21:09 Cnoor0171

This would be handy. My use-case is conforming to the field names expected by Elastic:

  • https://www.elastic.co/guide/en/ecs-logging/overview/master/intro.html#_field_mapping

My (planned) workaround is to have a renaming processor to do a final mapping of names to the ECS equivalents.

dhirschfeld avatar Nov 20 '24 23:11 dhirschfeld

Would be handy for us as well, we have an org-wide Datadog log ingest pipeline, that maps threadName for python apps, would appreciate this renaming ability for any keys in the JSON output.

rumblefrog avatar Nov 25 '24 23:11 rumblefrog

Would be handy for us as well, we have an org-wide Datadog log ingest pipeline, that maps threadName for python apps, would appreciate this renaming ability for any keys in the JSON output.

You can build this trivially yourself:

renames = {
    "foo": "bar"
}
sentinel = object()

def conform(_, __, ed):
    for key in renames.keys():
        if (val := ed.pop(key, sentinel)) is not sentinel:
            ed[renames[key]] = val

    return ed
>>> conform(None, None, {"foo": 42, "unrelated": 420})
{'unrelated': 420, 'bar': 42}

hynek avatar Nov 30 '24 06:11 hynek

Would be handy for us as well, we have an org-wide Datadog log ingest pipeline, that maps threadName for python apps, would appreciate this renaming ability for any keys in the JSON output.

You can build this trivially yourself:

renames = {
    "foo": "bar"
}
sentinel = object()

def conform(_, __, ed):
    for key in renames.keys():
        if (val := ed.pop(key, sentinel)) is not sentinel:
            ed[renames[key]] = val

    return ed
>>> conform(None, None, {"foo": 42, "unrelated": 420})
{'unrelated': 420, 'bar': 42}

Thanks for the snippet, but I'd like to see something like EventRenamer expanded for any arbitrary mapping, and that would become a general remapping rather than just for event, so they are within the same processor, but this is more of a nit request.

rumblefrog avatar Nov 30 '24 09:11 rumblefrog