otel-utils 0.1.15__py3-none-any.whl → 0.1.17__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/logging.py CHANGED
@@ -1,3 +1,4 @@
1
+ import json
1
2
  import logging
2
3
  import platform
3
4
  from contextlib import contextmanager
@@ -7,6 +8,30 @@ from typing import Any, Dict, Optional
7
8
  from opentelemetry import trace
8
9
 
9
10
 
11
+ class JsonFormatter(logging.Formatter):
12
+ def format(self, record):
13
+ log_data = {}
14
+
15
+ if hasattr(record, 'structured') and record.structured:
16
+ for key, value in record.__dict__.items():
17
+ if key not in (
18
+ 'args', 'exc_info', 'exc_text', 'msg', 'message', 'levelname', 'levelno', 'pathname',
19
+ 'filename',
20
+ 'module', 'stack_info', 'lineno', 'funcName', 'created', 'msecs', 'relativeCreated', 'name',
21
+ 'thread',
22
+ 'threadName', 'processName', 'process'):
23
+ log_data[key] = value
24
+
25
+ if 'attributes' in log_data:
26
+ for key, value in log_data['attributes'].items():
27
+ log_data[key] = value
28
+ del log_data['attributes']
29
+
30
+ log_data['message'] = record.getMessage()
31
+
32
+ return json.dumps(log_data)
33
+
34
+
10
35
  class StructuredLogger:
11
36
  """
12
37
  Logger that produces structured logs with tracing context.
@@ -24,6 +49,11 @@ class StructuredLogger:
24
49
  **(default_attributes or {})
25
50
  }
26
51
 
52
+ if not self.logger.handlers or not any(isinstance(h.formatter, JsonFormatter) for h in self.logger.handlers):
53
+ handler = logging.StreamHandler()
54
+ handler.setFormatter(JsonFormatter())
55
+ self.logger.addHandler(handler)
56
+
27
57
  def _get_trace_context(self) -> Dict[str, str]:
28
58
  """
29
59
  Gets the current tracing context if it exists.
@@ -48,43 +78,22 @@ class StructuredLogger:
48
78
 
49
79
  log_attributes = {
50
80
  **self.default_attributes,
81
+ **trace_context,
51
82
  "timestamp": datetime.utcnow().isoformat(),
52
83
  "severity": logging.getLevelName(level),
53
- "logger.name": self.logger.name,
54
- **trace_context
55
- }
56
-
57
- additional_info = []
58
- if kwargs.get("error_type"):
59
- additional_info.append(f"type={kwargs['error_type']}")
60
- log_attributes["error.type"] = kwargs["error_type"]
61
- if kwargs.get("error_message"):
62
- additional_info.append(f"message={kwargs['error_message']}")
63
- log_attributes["error.message"] = kwargs["error_message"]
64
- if kwargs.get("operation"):
65
- additional_info.append(f"operation={kwargs['operation']}")
66
- log_attributes["operation"] = kwargs["operation"]
67
-
68
- remaining_attrs = {
69
- k: v for k, v in kwargs.items()
70
- if k not in ["error_type", "error_message", "operation"]
84
+ "logger.name": self.logger.name
71
85
  }
72
- if remaining_attrs:
73
- log_attributes["attributes"] = remaining_attrs
74
86
 
75
- additional_str = f" - {', '.join(additional_info)}" if additional_info else ""
76
- trace_str = f"[trace_id={trace_context.get('trace_id', '0')}]" if trace_context else ""
77
- log_message = f"{trace_str} {message}{additional_str}"
87
+ for key, value in kwargs.items():
88
+ log_attributes[key] = value
78
89
 
79
90
  self.logger.log(
80
91
  level,
81
- log_message,
92
+ message,
82
93
  extra={
83
94
  "structured": True,
84
95
  "otel.name": "log",
85
96
  "otel.kind": "event",
86
- "otelTraceID": trace_context.get("trace_id", ""),
87
- "otelSpanID": trace_context.get("span_id", ""),
88
97
  **log_attributes
89
98
  }
90
99
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: otel-utils
3
- Version: 0.1.15
3
+ Version: 0.1.17
4
4
  Summary: Utilidades simplificadas para instrumentación con OpenTelemetry
5
5
  License: Proprietary
6
6
  Author: Harold Portocarrero
@@ -1,8 +1,8 @@
1
1
  otel_utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  otel_utils/configurator.py,sha256=uUrBbAYA0FHyJNy4ap50223ZPj0nhQh3EaGULuGCphQ,6572
3
- otel_utils/logging.py,sha256=RqO40kXgwxaUvjwdYCqJCKv0EBEgrEYrNaBJS8XhCV4,4156
3
+ otel_utils/logging.py,sha256=-F80xi301GmGBG2UlHuGfoqncbs88WNJnJI8Bh5B4dA,4271
4
4
  otel_utils/metrics.py,sha256=XD-t9V3peZJs97hN2hR2rwJKrcCJHqx2cldNOTCpzoA,3664
5
5
  otel_utils/tracing.py,sha256=PtowQ7MvYld_xJlVAV4pBQuDBQIqPeP1FQPrzgZx9_Q,2625
6
- otel_utils-0.1.15.dist-info/METADATA,sha256=FsBdvO1xs9LMbk2bYDlalfYXwT0xm95dWRsqYv0whLo,5279
7
- otel_utils-0.1.15.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
8
- otel_utils-0.1.15.dist-info/RECORD,,
6
+ otel_utils-0.1.17.dist-info/METADATA,sha256=FttkFUeg3nFRZ-w4g3BUXNRtpX4gZ0wbk84NzNyjWPk,5279
7
+ otel_utils-0.1.17.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
8
+ otel_utils-0.1.17.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.1
2
+ Generator: poetry-core 2.1.2
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any