otel-utils 0.1.12__py3-none-any.whl → 0.1.13__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of otel-utils might be problematic. Click here for more details.
otel_utils/configurator.py
CHANGED
|
@@ -85,6 +85,7 @@ class OtelConfigurator:
|
|
|
85
85
|
resource_attributes = {
|
|
86
86
|
"service.name": self.config.service_name,
|
|
87
87
|
"deployment.environment": self.config.environment,
|
|
88
|
+
"host.name": platform.node(),
|
|
88
89
|
}
|
|
89
90
|
if self.config.additional_resources:
|
|
90
91
|
resource_attributes.update(self.config.additional_resources)
|
|
@@ -139,18 +140,22 @@ class OtelConfigurator:
|
|
|
139
140
|
|
|
140
141
|
set_logger_provider(logger_provider)
|
|
141
142
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
143
|
+
if self.config.enable_console_logging:
|
|
144
|
+
console_handler = logging.StreamHandler()
|
|
145
|
+
formatter = logging.Formatter(
|
|
146
|
+
'[%(asctime)s] %(levelname)s [%(name)s] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s] - %(message)s',
|
|
147
|
+
datefmt='%Y-%m-%d %H:%M:%S'
|
|
148
|
+
)
|
|
149
|
+
console_handler.setFormatter(formatter)
|
|
150
|
+
root_logger.addHandler(console_handler)
|
|
151
|
+
root_logger.setLevel(self.config.log_level)
|
|
149
152
|
|
|
150
153
|
LoggingInstrumentor().instrument(
|
|
151
154
|
logger_provider=logger_provider,
|
|
152
155
|
set_logging_format=True,
|
|
153
|
-
log_level=self.config.log_level
|
|
156
|
+
log_level=self.config.log_level,
|
|
157
|
+
tracer_provider=trace.get_tracer_provider(),
|
|
158
|
+
meter_provider=metrics.get_meter_provider(),
|
|
154
159
|
)
|
|
155
160
|
|
|
156
161
|
self.logger = logging.getLogger(self.config.service_name)
|
otel_utils/logging.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import logging
|
|
2
|
-
import
|
|
3
|
-
from datetime import datetime
|
|
2
|
+
from contextlib import contextmanager
|
|
4
3
|
from typing import Any, Dict, Optional
|
|
4
|
+
|
|
5
5
|
from opentelemetry import trace
|
|
6
|
-
from contextlib import contextmanager
|
|
7
6
|
|
|
8
7
|
|
|
9
8
|
class StructuredLogger:
|
|
@@ -19,6 +18,7 @@ class StructuredLogger:
|
|
|
19
18
|
self.logger = logging.getLogger(service_name)
|
|
20
19
|
self.default_attributes = {
|
|
21
20
|
"service.name": service_name,
|
|
21
|
+
"host.name": platform.node(),
|
|
22
22
|
**(default_attributes or {})
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -42,23 +42,36 @@ class StructuredLogger:
|
|
|
42
42
|
*args,
|
|
43
43
|
**kwargs
|
|
44
44
|
):
|
|
45
|
-
"""
|
|
46
|
-
Create a structured log with tracing context.
|
|
47
|
-
"""
|
|
48
|
-
extra = kwargs.pop("extra", {})
|
|
49
45
|
trace_context = self._get_trace_context()
|
|
50
46
|
|
|
47
|
+
log_attributes = {
|
|
48
|
+
**self.default_attributes,
|
|
49
|
+
"timestamp": datetime.utcnow().isoformat(),
|
|
50
|
+
"severity": logging.getLevelName(level),
|
|
51
|
+
"logger.name": self.logger.name,
|
|
52
|
+
**trace_context
|
|
53
|
+
}
|
|
54
|
+
|
|
51
55
|
additional_info = []
|
|
52
56
|
if kwargs.get("error_type"):
|
|
53
57
|
additional_info.append(f"type={kwargs['error_type']}")
|
|
58
|
+
log_attributes["error.type"] = kwargs["error_type"]
|
|
54
59
|
if kwargs.get("error_message"):
|
|
55
60
|
additional_info.append(f"message={kwargs['error_message']}")
|
|
61
|
+
log_attributes["error.message"] = kwargs["error_message"]
|
|
56
62
|
if kwargs.get("operation"):
|
|
57
63
|
additional_info.append(f"operation={kwargs['operation']}")
|
|
64
|
+
log_attributes["operation"] = kwargs["operation"]
|
|
65
|
+
|
|
66
|
+
remaining_attrs = {
|
|
67
|
+
k: v for k, v in kwargs.items()
|
|
68
|
+
if k not in ["error_type", "error_message", "operation"]
|
|
69
|
+
}
|
|
70
|
+
if remaining_attrs:
|
|
71
|
+
log_attributes["attributes"] = remaining_attrs
|
|
58
72
|
|
|
59
73
|
additional_str = f" - {', '.join(additional_info)}" if additional_info else ""
|
|
60
74
|
trace_str = f"[trace_id={trace_context.get('trace_id', '0')}]" if trace_context else ""
|
|
61
|
-
|
|
62
75
|
log_message = f"{trace_str} {message}{additional_str}"
|
|
63
76
|
|
|
64
77
|
self.logger.log(
|
|
@@ -66,9 +79,11 @@ class StructuredLogger:
|
|
|
66
79
|
log_message,
|
|
67
80
|
extra={
|
|
68
81
|
"structured": True,
|
|
69
|
-
"
|
|
70
|
-
|
|
71
|
-
|
|
82
|
+
"otel.name": "log",
|
|
83
|
+
"otel.kind": "event",
|
|
84
|
+
"otelTraceID": trace_context.get("trace_id", ""),
|
|
85
|
+
"otelSpanID": trace_context.get("span_id", ""),
|
|
86
|
+
**log_attributes
|
|
72
87
|
}
|
|
73
88
|
)
|
|
74
89
|
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
otel_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
otel_utils/configurator.py,sha256=XsVWyoDLNfplkAwQUB24ZNi_cBmvePCPmqdg1KisrkM,6556
|
|
3
|
+
otel_utils/logging.py,sha256=RIkwwkVHrWGmkwKCvfiJJWj-jM--CV0TpLoBfm_Cql4,4110
|
|
4
|
+
otel_utils/metrics.py,sha256=XD-t9V3peZJs97hN2hR2rwJKrcCJHqx2cldNOTCpzoA,3664
|
|
5
|
+
otel_utils/tracing.py,sha256=PtowQ7MvYld_xJlVAV4pBQuDBQIqPeP1FQPrzgZx9_Q,2625
|
|
6
|
+
otel_utils-0.1.13.dist-info/METADATA,sha256=Mf4pv-uu2rGMNxQ_1p-OPlAmyWxxpLCaocK2ZCobAb4,5279
|
|
7
|
+
otel_utils-0.1.13.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
|
|
8
|
+
otel_utils-0.1.13.dist-info/RECORD,,
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
otel_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
otel_utils/configurator.py,sha256=vQhEzt5L857re1mk_BBW8zjZ20458lvwDDe55cccSCE,6225
|
|
3
|
-
otel_utils/logging.py,sha256=ZsvZsjrRGoTLD1F035-Haq2hPuEeEYXYiAMU8B_aPXQ,3395
|
|
4
|
-
otel_utils/metrics.py,sha256=XD-t9V3peZJs97hN2hR2rwJKrcCJHqx2cldNOTCpzoA,3664
|
|
5
|
-
otel_utils/tracing.py,sha256=PtowQ7MvYld_xJlVAV4pBQuDBQIqPeP1FQPrzgZx9_Q,2625
|
|
6
|
-
otel_utils-0.1.12.dist-info/METADATA,sha256=cz-ZR0Er6-KdxBHtulXNwAKdJ1FCn1bsV7J6vLkPOLw,5279
|
|
7
|
-
otel_utils-0.1.12.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
|
|
8
|
-
otel_utils-0.1.12.dist-info/RECORD,,
|
|
File without changes
|