otel-utils 0.8.0__tar.gz → 1.1.0__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.8.0
3
+ Version: 1.1.0
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.8.0"
3
+ version = "1.1.0"
4
4
  description = "Utilidades simplificadas para instrumentación con OpenTelemetry"
5
5
  authors = ["Harold Portocarrero <harold@getcometa.com>"]
6
6
  readme = "README.md"
@@ -43,6 +43,18 @@ class JsonFormatter(logging.Formatter):
43
43
  return json.dumps(log_record)
44
44
 
45
45
 
46
+ class DefaultAttributesFilter(logging.Filter):
47
+ def __init__(self, default_attributes):
48
+ super().__init__()
49
+ self.default_attributes = default_attributes
50
+
51
+ def filter(self, record):
52
+ for k, v in self.default_attributes.items():
53
+ if not hasattr(record, k):
54
+ setattr(record, k, v)
55
+ return True
56
+
57
+
46
58
  class StructuredLogger:
47
59
  """
48
60
  Logger that produces structured logs with tracing context.
@@ -65,11 +77,9 @@ class StructuredLogger:
65
77
  handler = logging.StreamHandler()
66
78
  handler.setFormatter(JsonFormatter())
67
79
  self.logger.addHandler(handler)
80
+ self.logger.addFilter(DefaultAttributesFilter(self.default_attributes))
68
81
 
69
82
  def _get_trace_context(self) -> Dict[str, str]:
70
- """
71
- Gets the current tracing context if it exists.
72
- """
73
83
  span = trace.get_current_span()
74
84
  if span.is_recording():
75
85
  ctx = span.get_span_context()
@@ -88,23 +98,15 @@ class StructuredLogger:
88
98
  ):
89
99
  operation = kwargs.pop("operation", None)
90
100
  status = kwargs.pop("status", None)
91
-
92
101
  context = kwargs.copy() if kwargs else None
93
-
94
- attributes = dict(self.default_attributes)
95
- attributes.update(kwargs)
96
- extra_data = attributes.copy()
102
+ extra_data = {}
97
103
  extra_data['service_name'] = self.service_name
98
-
99
104
  if operation:
100
105
  extra_data["operation"] = operation
101
-
102
106
  if status:
103
107
  extra_data["status"] = status
104
-
105
108
  if context:
106
109
  extra_data["context"] = context
107
-
108
110
  trace_ctx = self._get_trace_context()
109
111
  if trace_ctx and not hasattr(logging.getLogRecordFactory(), "otelTraceID"):
110
112
  trace_id_hex = trace_ctx.get("trace_id")
@@ -120,7 +122,6 @@ class StructuredLogger:
120
122
  extra_data["dd.span_id"] = span_id_hex
121
123
  extra_data["trace_id"] = trace_id_hex
122
124
  extra_data["span_id"] = span_id_hex
123
-
124
125
  self.logger.log(level, message, extra=extra_data)
125
126
 
126
127
  def debug(self, message: str, *args, **kwargs):
@@ -141,9 +142,6 @@ class StructuredLogger:
141
142
  operation_name: str,
142
143
  **context
143
144
  ):
144
- """
145
- Provides context for an operation, recording its beginning and end.
146
- """
147
145
  try:
148
146
  self.info(
149
147
  f"Iniciando {operation_name}",
@@ -1,7 +1,7 @@
1
1
  from contextlib import contextmanager
2
2
  from typing import Optional, Dict, Any, List
3
3
  from opentelemetry import metrics
4
- from opentelemetry.metrics import Observation, CallbackOptions
4
+ from opentelemetry.metrics import Observation, CallbackOptions, Counter, Histogram, _Gauge as Gauge
5
5
  import time
6
6
 
7
7
 
@@ -13,9 +13,9 @@ class Metrics:
13
13
 
14
14
  def __init__(self, service_name: str):
15
15
  self._meter = metrics.get_meter(service_name)
16
- self._gauges = {}
17
- self._histograms = {}
18
- self._counters = {}
16
+ self._gauges: Dict[str, Gauge] = {}
17
+ self._histograms: Dict[str, Histogram] = {}
18
+ self._counters: Dict[str, Counter] = {}
19
19
 
20
20
  def create_histogram(
21
21
  self,
@@ -109,4 +109,14 @@ class Metrics:
109
109
  description=description,
110
110
  unit=unit
111
111
  )
112
- return self._histograms[name]
112
+ return self._histograms[name]
113
+
114
+ def get_gauge(self, name: str, description: str = "", unit: str = "1"):
115
+ """Gets or creates a gauge."""
116
+ if name not in self._gauges:
117
+ self._gauges[name] = self._meter.create_gauge(
118
+ name=name,
119
+ description=description,
120
+ unit=unit
121
+ )
122
+ return self._gauges[name]
File without changes