otel-utils 0.1.12__py3-none-any.whl → 0.1.14__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.

@@ -1,4 +1,5 @@
1
1
  import logging
2
+ import platform
2
3
  from dataclasses import dataclass
3
4
  from typing import Optional, Dict, Any, Literal
4
5
 
@@ -85,6 +86,7 @@ class OtelConfigurator:
85
86
  resource_attributes = {
86
87
  "service.name": self.config.service_name,
87
88
  "deployment.environment": self.config.environment,
89
+ "host.name": platform.node(),
88
90
  }
89
91
  if self.config.additional_resources:
90
92
  resource_attributes.update(self.config.additional_resources)
@@ -139,18 +141,22 @@ class OtelConfigurator:
139
141
 
140
142
  set_logger_provider(logger_provider)
141
143
 
142
- console_handler = logging.StreamHandler()
143
- formatter = logging.Formatter(
144
- '%(asctime)s %(levelname)s [%(name)s] %(message)s'
145
- )
146
- console_handler.setFormatter(formatter)
147
- root_logger.addHandler(console_handler)
148
- root_logger.setLevel(self.config.log_level)
144
+ if self.config.enable_console_logging:
145
+ console_handler = logging.StreamHandler()
146
+ formatter = logging.Formatter(
147
+ '[%(asctime)s] %(levelname)s [%(name)s] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s] - %(message)s',
148
+ datefmt='%Y-%m-%d %H:%M:%S'
149
+ )
150
+ console_handler.setFormatter(formatter)
151
+ root_logger.addHandler(console_handler)
152
+ root_logger.setLevel(self.config.log_level)
149
153
 
150
154
  LoggingInstrumentor().instrument(
151
155
  logger_provider=logger_provider,
152
156
  set_logging_format=True,
153
- log_level=self.config.log_level
157
+ log_level=self.config.log_level,
158
+ tracer_provider=trace.get_tracer_provider(),
159
+ meter_provider=metrics.get_meter_provider(),
154
160
  )
155
161
 
156
162
  self.logger = logging.getLogger(self.config.service_name)
otel_utils/logging.py CHANGED
@@ -1,9 +1,9 @@
1
1
  import logging
2
- import json
3
- from datetime import datetime
2
+ import platform
3
+ from contextlib import contextmanager
4
4
  from typing import Any, Dict, Optional
5
+
5
6
  from opentelemetry import trace
6
- from contextlib import contextmanager
7
7
 
8
8
 
9
9
  class StructuredLogger:
@@ -19,6 +19,7 @@ class StructuredLogger:
19
19
  self.logger = logging.getLogger(service_name)
20
20
  self.default_attributes = {
21
21
  "service.name": service_name,
22
+ "host.name": platform.node(),
22
23
  **(default_attributes or {})
23
24
  }
24
25
 
@@ -42,23 +43,36 @@ class StructuredLogger:
42
43
  *args,
43
44
  **kwargs
44
45
  ):
45
- """
46
- Create a structured log with tracing context.
47
- """
48
- extra = kwargs.pop("extra", {})
49
46
  trace_context = self._get_trace_context()
50
47
 
48
+ log_attributes = {
49
+ **self.default_attributes,
50
+ "timestamp": datetime.utcnow().isoformat(),
51
+ "severity": logging.getLevelName(level),
52
+ "logger.name": self.logger.name,
53
+ **trace_context
54
+ }
55
+
51
56
  additional_info = []
52
57
  if kwargs.get("error_type"):
53
58
  additional_info.append(f"type={kwargs['error_type']}")
59
+ log_attributes["error.type"] = kwargs["error_type"]
54
60
  if kwargs.get("error_message"):
55
61
  additional_info.append(f"message={kwargs['error_message']}")
62
+ log_attributes["error.message"] = kwargs["error_message"]
56
63
  if kwargs.get("operation"):
57
64
  additional_info.append(f"operation={kwargs['operation']}")
65
+ log_attributes["operation"] = kwargs["operation"]
66
+
67
+ remaining_attrs = {
68
+ k: v for k, v in kwargs.items()
69
+ if k not in ["error_type", "error_message", "operation"]
70
+ }
71
+ if remaining_attrs:
72
+ log_attributes["attributes"] = remaining_attrs
58
73
 
59
74
  additional_str = f" - {', '.join(additional_info)}" if additional_info else ""
60
75
  trace_str = f"[trace_id={trace_context.get('trace_id', '0')}]" if trace_context else ""
61
-
62
76
  log_message = f"{trace_str} {message}{additional_str}"
63
77
 
64
78
  self.logger.log(
@@ -66,9 +80,11 @@ class StructuredLogger:
66
80
  log_message,
67
81
  extra={
68
82
  "structured": True,
69
- "trace_context": trace_context,
70
- **extra,
71
- **kwargs
83
+ "otel.name": "log",
84
+ "otel.kind": "event",
85
+ "otelTraceID": trace_context.get("trace_id", ""),
86
+ "otelSpanID": trace_context.get("span_id", ""),
87
+ **log_attributes
72
88
  }
73
89
  )
74
90
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: otel-utils
3
- Version: 0.1.12
3
+ Version: 0.1.14
4
4
  Summary: Utilidades simplificadas para instrumentación con OpenTelemetry
5
5
  License: Proprietary
6
6
  Author: Harold Portocarrero
@@ -0,0 +1,8 @@
1
+ otel_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ otel_utils/configurator.py,sha256=uUrBbAYA0FHyJNy4ap50223ZPj0nhQh3EaGULuGCphQ,6572
3
+ otel_utils/logging.py,sha256=tLjqrDYM41Vw0LYCDkVIsQnE-GpN5fAsqZkGfWK8Mqc,4126
4
+ otel_utils/metrics.py,sha256=XD-t9V3peZJs97hN2hR2rwJKrcCJHqx2cldNOTCpzoA,3664
5
+ otel_utils/tracing.py,sha256=PtowQ7MvYld_xJlVAV4pBQuDBQIqPeP1FQPrzgZx9_Q,2625
6
+ otel_utils-0.1.14.dist-info/METADATA,sha256=mH8DwnRLinga1mzd1KPjocZ7vcRUIS07TdoDJwTv3HY,5279
7
+ otel_utils-0.1.14.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
8
+ otel_utils-0.1.14.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,,