Skip to content

Don't de-dot ecs.version #118

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 2 commits into from
Jun 13, 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: 1 addition & 1 deletion .github/workflows/periodic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
python: [ '3.7', '3.8', '3.9', '3.10' ]
python: [ '3.7', '3.8', '3.9', '3.10', '3.11', '3.12' ]
fail-fast: false
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
timeout-minutes: 5
strategy:
matrix:
python: [ '3.7', '3.8', '3.9', '3.10' ]
python: [ '3.7', '3.8', '3.9', '3.10', '3.11', '3.12' ]
fail-fast: false
steps:
- run: 'echo "No build required"'
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
timeout-minutes: 10
strategy:
matrix:
python: [ '3.7', '3.8', '3.9', '3.10' ]
python: [ '3.7', '3.8', '3.9', '3.10', '3.11', '3.12' ]
fail-fast: false
steps:
- uses: actions/checkout@v4
Expand Down
7 changes: 6 additions & 1 deletion ecs_logging/_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,12 @@ def format_to_ecs(self, record):
continue
value = extractors[field](record)
if value is not None:
merge_dicts(de_dot(field, value), result)
# special case ecs.version that should not be de-dotted
Copy link
Member Author

@xrmx xrmx Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like log.level survives de_dot without special cases apparently but I can't see where it gets a different treatment than the rest

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

weird! 🤔

if field == "ecs.version":
field_dict = {field: value}
else:
field_dict = de_dot(field, value)
merge_dicts(field_dict, result)

available = record.__dict__

Expand Down
2 changes: 1 addition & 1 deletion ecs_logging/_structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def format_to_ecs(self, event_dict):
else:
event_dict["error"] = {"stack_trace": stack_trace}

event_dict.setdefault("ecs", {}).setdefault("version", ECS_VERSION)
event_dict.setdefault("ecs.version", ECS_VERSION)
return event_dict

def _json_dumps(self, value):
Expand Down
2 changes: 1 addition & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def tests_impl(session):
)


@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10"])
@nox.session(python=["3.6", "3.7", "3.8", "3.9", "3.10", "3.11", "3.12"])
def test(session):
tests_impl(session)

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ classifiers = [
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Topic :: System :: Logging",
"License :: OSI Approved :: Apache Software License"
]
Expand Down
8 changes: 4 additions & 4 deletions tests/test_apm.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_elasticapm_structlog_log_correlation_ecs_fields(spec_validator, apm):
ecs = json.loads(spec_validator(stream.getvalue().rstrip()))
ecs.pop("@timestamp")
assert ecs == {
"ecs": {"version": "1.6.0"},
"ecs.version": "1.6.0",
"log.level": "info",
"message": "test message",
"span": {"id": span_id},
Expand Down Expand Up @@ -84,7 +84,7 @@ def test_elastic_apm_stdlib_no_filter_log_correlation_ecs_fields(apm):

ecs = json.loads(stream.getvalue().rstrip())
assert ecs == {
"ecs": {"version": "1.6.0"},
"ecs.version": "1.6.0",
"log.level": "info",
"log": {
"logger": "apm-logger",
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_elastic_apm_stdlib_with_filter_log_correlation_ecs_fields(apm):

ecs = json.loads(stream.getvalue().rstrip())
assert ecs == {
"ecs": {"version": "1.6.0"},
"ecs.version": "1.6.0",
"log.level": "info",
"log": {
"logger": "apm-logger",
Expand Down Expand Up @@ -175,7 +175,7 @@ def test_elastic_apm_stdlib_exclude_fields(apm):

ecs = json.loads(stream.getvalue().rstrip())
assert ecs == {
"ecs": {"version": "1.6.0"},
"ecs.version": "1.6.0",
"log.level": "info",
"log": {
"logger": "apm-logger",
Expand Down
27 changes: 19 additions & 8 deletions tests/test_stdlib_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_record_formatted(spec_validator):
formatter = ecs_logging.StdlibFormatter(exclude_fields=["process"])

assert spec_validator(formatter.format(make_record())) == (
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs":{"version":"1.6.0"},'
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs.version":"1.6.0",'
'"log":{"logger":"logger-name","origin":{"file":{"line":10,"name":"file.py"},"function":"test_function"},'
'"original":"1: hello"}}'
)
Expand All @@ -63,7 +63,7 @@ def test_extra_global_is_merged(spec_validator):
)

assert spec_validator(formatter.format(make_record())) == (
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs":{"version":"1.6.0"},'
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs.version":"1.6.0",'
'"environment":"dev",'
'"log":{"logger":"logger-name","origin":{"file":{"line":10,"name":"file.py"},"function":"test_function"},'
'"original":"1: hello"}}'
Expand All @@ -80,7 +80,7 @@ def format_to_ecs(self, record):
formatter = CustomFormatter(exclude_fields=["process"])
assert spec_validator(formatter.format(make_record())) == (
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello",'
'"custom":"field","ecs":{"version":"1.6.0"},"log":{"logger":"logger-name","origin":'
'"custom":"field","ecs.version":"1.6.0","log":{"logger":"logger-name","origin":'
'{"file":{"line":10,"name":"file.py"},"function":"test_function"},"original":"1: hello"}}'
)

Expand All @@ -94,7 +94,7 @@ def test_can_be_set_on_handler():

assert stream.getvalue() == (
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello",'
'"ecs":{"version":"1.6.0"},"log":{"logger":"logger-name","origin":{"file":{"line":10,'
'"ecs.version":"1.6.0","log":{"logger":"logger-name","origin":{"file":{"line":10,'
'"name":"file.py"},"function":"test_function"},"original":"1: hello"}}\n'
)

Expand Down Expand Up @@ -127,7 +127,7 @@ def test_extra_is_merged(time, logger):
assert isinstance(ecs["log"]["origin"]["file"].pop("line"), int)
assert ecs == {
"@timestamp": "2020-03-20T16:16:37.187Z",
"ecs": {"version": "1.6.0"},
"ecs.version": "1.6.0",
"log.level": "info",
"log": {
"logger": logger.name,
Expand Down Expand Up @@ -254,8 +254,6 @@ def test_stack_trace_limit_types_and_values():
"exclude_fields",
[
"process",
"ecs",
"ecs.version",
"log",
"log.level",
"message",
Expand All @@ -279,6 +277,19 @@ def test_exclude_fields(exclude_fields):
assert field_path[-1] not in obj


@pytest.mark.parametrize(
"exclude_fields",
[
"ecs.version",
],
)
def test_exclude_fields_not_dedotted(exclude_fields):
formatter = ecs_logging.StdlibFormatter(exclude_fields=[exclude_fields])
ecs = formatter.format_to_ecs(make_record())
for entry in exclude_fields:
assert entry not in ecs


def test_exclude_fields_empty_json_object():
"""Assert that if all JSON objects attributes are excluded then the object doesn't appear."""
formatter = ecs_logging.StdlibFormatter(
Expand Down Expand Up @@ -350,7 +361,7 @@ def test_apm_data_conflicts(spec_validator):
formatter = ecs_logging.StdlibFormatter(exclude_fields=["process"])

assert spec_validator(formatter.format(record)) == (
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs":{"version":"1.6.0"},'
'{"@timestamp":"2020-03-20T14:12:46.123Z","log.level":"debug","message":"1: hello","ecs.version":"1.6.0",'
'"log":{"logger":"logger-name","origin":{"file":{"line":10,"name":"file.py"},"function":"test_function"},'
'"original":"1: hello"},"service":{"environment":"dev","name":"myapp","version":"1.0.0"}}'
)
4 changes: 2 additions & 2 deletions tests/test_structlog_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_event_dict_formatted(time, spec_validator, event_dict):
'{"@timestamp":"2020-03-20T16:16:37.187Z","log.level":"debug",'
'"message":"test message",'
'"baz":"<NotSerializable>",'
'"ecs":{"version":"1.6.0"},'
'"ecs.version":"1.6.0",'
'"foo":"bar",'
'"log":{"logger":"logger-name"}}'
)
Expand All @@ -90,7 +90,7 @@ def test_can_be_set_as_processor(time, spec_validator):
assert spec_validator(stream.getvalue()) == (
'{"@timestamp":"2020-03-20T16:16:37.187Z","log.level":"debug",'
'"message":"test message","custom":"key","dot":{"ted":1},'
'"ecs":{"version":"1.6.0"}}\n'
'"ecs.version":"1.6.0"}\n'
)


Expand Down