Skip to content

Fix merging dicts for duplicate dotted keys #46

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

Merged
merged 9 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Fix for duplicate dotted keys ([#46])

## 1.0.0 (2021-02-08)

* Remove "beta" designation
Expand Down
2 changes: 1 addition & 1 deletion ecs_logging/_structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __call__(self, _, name, event_dict):

def format_to_ecs(self, event_dict):
# type: (Dict[str, Any]) -> Dict[str, Any]
event_dict["message"] = event_dict.pop("event")
event_dict["message"] = str(event_dict.pop("event"))
if "@timestamp" not in event_dict:
event_dict["@timestamp"] = (
datetime.datetime.utcfromtimestamp(time.time()).strftime(
Expand Down
4 changes: 2 additions & 2 deletions ecs_logging/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ def merge_dicts(from_, into):
When called has side-effects within 'destination'.
"""
for key, value in from_.items():
if isinstance(value, dict):
merge_dicts(value, into.setdefault(key, {}))
if isinstance(value, dict) and isinstance(into.setdefault(key, {}), dict):
merge_dicts(value, into[key])
else:
into[key] = value
return into
Expand Down
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import json
import os
import collections
import sys

import pytest

Expand All @@ -27,6 +28,10 @@ class ValidationError(Exception):
pass


if sys.version_info[0] >= 3:
basestring = str


@pytest.fixture
def spec_validator():
with open(
Expand Down
8 changes: 7 additions & 1 deletion tests/test_structlog_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@


def make_event_dict():
return {"event": "test message", "log.logger": "logger-name"}
return {
"event": "test message",
"log.logger": "logger-name",
"foo": "bar",
"foo.dataset": "baz",
}


@mock.patch("time.time")
Expand All @@ -33,6 +38,7 @@ def test_event_dict_formatted(time, spec_validator):
assert spec_validator(formatter(None, "debug", make_event_dict())) == (
'{"@timestamp":"2020-03-20T16:16:37.187Z","log.level":"debug",'
'"message":"test message","ecs":{"version":"1.6.0"},'
'"foo":{"dataset":"baz"},'
'"log":{"logger":"logger-name"}}'
)

Expand Down