otel-utils 0.1.12__tar.gz → 0.1.13__tar.gz

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,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: otel-utils
3
- Version: 0.1.12
3
+ Version: 0.1.13
4
4
  Summary: Utilidades simplificadas para instrumentación con OpenTelemetry
5
5
  License: Proprietary
6
6
  Author: Harold Portocarrero
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "otel-utils"
3
- version = "0.1.12"
3
+ version = "0.1.13"
4
4
  description = "Utilidades simplificadas para instrumentación con OpenTelemetry"
5
5
  authors = ["Harold Portocarrero <harold@getcometa.com>"]
6
6
  readme = "README.md"
@@ -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
- 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)
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)
@@ -1,9 +1,8 @@
1
1
  import logging
2
- import json
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
- "trace_context": trace_context,
70
- **extra,
71
- **kwargs
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
 
File without changes