Skip to content

Add service.environment to stdlib formatter #96

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 6, 2023
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.1.0 (unreleased)

- Add support for `service.environment` from APM log correlation ([#96](https://github.com/elastic/ecs-logging-python/pull/96))

## 2.0.2 (2023-05-17)

- Allow flit-core 3+ ([#94](https://github.com/elastic/ecs-logging-python/pull/94))
Expand Down
14 changes: 9 additions & 5 deletions ecs_logging/_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,20 @@
import sys
import time
from traceback import format_tb

from ._meta import ECS_VERSION
from ._utils import (
merge_dicts,
de_dot,
json_dumps,
TYPE_CHECKING,
collections_abc,
lru_cache,
de_dot,
flatten_dict,
json_dumps,
lru_cache,
merge_dicts,
)

if TYPE_CHECKING:
from typing import Optional, Any, Callable, Dict, Sequence
from typing import Any, Callable, Dict, Optional, Sequence

try:
from typing import Literal, Union # type: ignore
Expand Down Expand Up @@ -235,6 +236,9 @@ def format_to_ecs(self, record):
)
extras.setdefault("trace.id", extras.pop("elasticapm_trace_id", None))
extras.setdefault("service.name", extras.pop("elasticapm_service_name", None))
extras.setdefault(
"service.environment", extras.pop("elasticapm_service_environment", None)
)

# Merge in any keys that were set within 'extra={...}'
for field, value in extras.items():
Expand Down
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@
# specific language governing permissions and limitations
# under the License.

import collections
import datetime
import json
import logging
import os
import collections
import sys
import logging
import elasticapm

import elasticapm
import pytest


Expand Down Expand Up @@ -85,7 +85,9 @@ def validator(data_json):
@pytest.fixture
def apm():
record_factory = logging.getLogRecordFactory()
apm = elasticapm.Client({"SERVICE_NAME": "apm-service", "DISABLE_SEND": True})
apm = elasticapm.Client(
{"SERVICE_NAME": "apm-service", "ENVIRONMENT": "dev", "DISABLE_SEND": True}
)
yield apm
apm.close()
logging.setLogRecordFactory(record_factory)
18 changes: 9 additions & 9 deletions tests/test_apm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
# under the License.

import json
import sys
import logging
from io import StringIO

import elasticapm
import structlog
from elasticapm.handlers.logging import LoggingFilter
from elasticapm.handlers.structlog import structlog_processor

import ecs_logging
import logging
import structlog
import pytest
from io import StringIO


def test_elasticapm_structlog_log_correlation_ecs_fields(spec_validator, apm):
Expand Down Expand Up @@ -55,7 +55,7 @@ def test_elasticapm_structlog_log_correlation_ecs_fields(spec_validator, apm):
"span": {"id": span_id},
"trace": {"id": trace_id},
"transaction": {"id": transaction_id},
"service": {"name": "apm-service"},
"service": {"name": "apm-service", "environment": "dev"},
}


Expand Down Expand Up @@ -98,7 +98,7 @@ def test_elastic_apm_stdlib_no_filter_log_correlation_ecs_fields(apm):
"span": {"id": span_id},
"trace": {"id": trace_id},
"transaction": {"id": transaction_id},
"service": {"name": "apm-service"},
"service": {"name": "apm-service", "environment": "dev"},
}


Expand Down Expand Up @@ -142,7 +142,7 @@ def test_elastic_apm_stdlib_with_filter_log_correlation_ecs_fields(apm):
"span": {"id": span_id},
"trace": {"id": trace_id},
"transaction": {"id": transaction_id},
"service": {"name": "apm-service"},
"service": {"name": "apm-service", "environment": "dev"},
}


Expand Down Expand Up @@ -187,5 +187,5 @@ def test_elastic_apm_stdlib_exclude_fields(apm):
},
"message": "test message",
"trace": {"id": trace_id},
"service": {"name": "apm-service"},
"service": {"name": "apm-service", "environment": "dev"},
}