Skip to content

Commit

Permalink
Add service.environment to stdlib formatter (#96)
Browse files Browse the repository at this point in the history
* Add service.environment to stdlib formatter

* CHANGELOG
  • Loading branch information
basepi committed Jun 6, 2023
1 parent bdce799 commit 1193b91
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -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
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
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
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"},
}

0 comments on commit 1193b91

Please sign in to comment.