Skip to content

Commit 80258e3

Browse files
committed
Make logger setup compatible with OTEL logs
1 parent ec9b7ff commit 80258e3

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

local.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ ENVIRONMENT: "local"
22
AUTH__JWKS_URL: "http://oathkeeper:4456/.well-known/jwks.json"
33
#DRAMATIQ__REDIS_URL: "redis://redis:6379/0"
44
OTEL_EXPORTER_OTLP_ENDPOINT: "http://otel-collector:4317"
5+
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED: "true"

otel-collector-config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ processors:
1717

1818
service:
1919
pipelines:
20+
logs:
21+
receivers: [otlp]
22+
processors: []
23+
exporters: [debug]
2024
metrics:
2125
receivers: [otlp]
26+
processors: []
2227
exporters: [debug]
2328
traces:
2429
receivers: [otlp]

src/common/logs/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from typing import List
33

44
import structlog
5+
from opentelemetry.sdk._logs import LoggingHandler
56
from structlog.typing import Processor
67

78
from ..config import AppConfig
@@ -76,9 +77,18 @@ def init_logger(config: AppConfig) -> None:
7677
)
7778
)
7879

79-
# Use structlog to format logs coming from stdlib logger
80+
# Get root logger
8081
stdlib_logger = logging.getLogger()
81-
stdlib_logger.handlers.clear()
82+
83+
# In case there's a OTEL handler we keep it but remove all the others,
84+
# in case this function is called multiple times.
85+
# NOTE all the processors are not applied to OTEL logs!
86+
for l in stdlib_logger.handlers:
87+
if not isinstance(l, LoggingHandler):
88+
stdlib_logger.removeHandler(l)
89+
90+
91+
# Use structlog to format logs coming from stdlib logger
8292
stdlib_logger.addHandler(stdlib_handler)
8393
stdlib_logger.setLevel(log_level)
8494

0 commit comments

Comments
 (0)