Skip to content

Commit

Permalink
Add OpenTelemetry trace id to Logstash entries (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
andmat900 authored Jun 3, 2024
1 parent b10f662 commit 4b16b52
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/etos_lib/logging/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
"""ETOS filter."""
import logging

from opentelemetry import trace


def get_current_otel_trace_id() -> str:
"""Get current OpenTelemetry trace id.
The OpenTelemetry trace id is a big integer by default, which is out of range
of the type 'long' in Elastic. For this reason the trace id is returned as string.
If OpenTelemetry is not enabled, this function will return "0".
"""
current_span = trace.get_current_span()
return str(current_span.get_span_context().trace_id)


class EtosFilter(logging.Filter): # pylint:disable=too-few-public-methods
"""Filter for adding extra application specific data to log messages."""
Expand Down Expand Up @@ -49,6 +63,7 @@ def filter(self, record):
record.application = self.application
record.version = self.version
record.environment = self.environment
record.opentelemetry_trace_id = get_current_otel_trace_id()

# Add each thread-local attribute to record.
for attr in dir(self.local):
Expand Down

0 comments on commit 4b16b52

Please sign in to comment.