Skip to content

Commit

Permalink
Add event.dataset for ECS logging (elastic#1061)
Browse files Browse the repository at this point in the history
Also use global client for better service.name handling
  • Loading branch information
basepi authored and beniwohli committed Sep 14, 2021
1 parent 436b63a commit be7c270
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
11 changes: 9 additions & 2 deletions elasticapm/handlers/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import traceback
import warnings

from elasticapm import get_client
from elasticapm.base import Client
from elasticapm.traces import execution_context
from elasticapm.utils import compat, wrapt
Expand Down Expand Up @@ -160,7 +161,7 @@ def _emit(self, record, **kwargs):
exception=exception,
level=record.levelno,
logger_name=record.name,
**kwargs
**kwargs,
)


Expand Down Expand Up @@ -227,14 +228,18 @@ def _add_attributes_to_log_record(record):
span_id = span.id if span else None
record.elasticapm_span_id = span_id

service_name = transaction.tracer.config.service_name if transaction else None
client = get_client()
service_name = client.config.service_name if client else None
record.elasticapm_service_name = service_name
event_dataset = f"{client.config.service_name}.log" if client else None
record.elasticapm_event_dataset = event_dataset

record.elasticapm_labels = {
"transaction.id": transaction_id,
"trace.id": trace_id,
"span.id": span_id,
"service.name": service_name,
"event.dataset": event_dataset,
}

return record
Expand Down Expand Up @@ -274,6 +279,7 @@ def format(self, record):
record.elasticapm_trace_id = None
record.elasticapm_span_id = None
record.elasticapm_service_name = None
record.elasticapm_event_dataset = None
return super(Formatter, self).format(record=record)

def formatTime(self, record, datefmt=None):
Expand All @@ -282,4 +288,5 @@ def formatTime(self, record, datefmt=None):
record.elasticapm_trace_id = None
record.elasticapm_span_id = None
record.elasticapm_service_name = None
record.elasticapm_event_dataset = None
return super(Formatter, self).formatTime(record=record, datefmt=datefmt)
6 changes: 5 additions & 1 deletion elasticapm/handlers/structlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from __future__ import absolute_import

from elasticapm import get_client
from elasticapm.traces import execution_context


Expand All @@ -53,7 +54,10 @@ def structlog_processor(logger, method_name, event_dict):
transaction = execution_context.get_transaction()
if transaction:
event_dict["transaction.id"] = transaction.id
event_dict["service.name"] = transaction.tracer.config.service_name
client = get_client()
if client:
event_dict["service.name"] = client.config.service_name
event_dict["event.dataset"] = f"{client.config.service_name}.log"
if transaction and transaction.trace_parent:
event_dict["trace.id"] = transaction.trace_parent.trace_id
span = execution_context.get_span()
Expand Down

0 comments on commit be7c270

Please sign in to comment.