nucliadb-telemetry 6.4.0.post4161__py3-none-any.whl → 6.4.0.post4167__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 nucliadb-telemetry might be problematic. Click here for more details.

@@ -31,7 +31,7 @@ from opentelemetry.propagate import extract, inject
31
31
  from opentelemetry.propagators.textmap import CarrierT, Setter
32
32
  from opentelemetry.sdk.trace import Span, TracerProvider
33
33
  from opentelemetry.semconv.trace import SpanAttributes
34
- from opentelemetry.trace import SpanKind, Tracer
34
+ from opentelemetry.trace import SpanKind, Tracer, set_span_in_context
35
35
  from opentelemetry.trace.status import Status, StatusCode
36
36
 
37
37
  from nucliadb_telemetry import grpc_metrics, logger
@@ -52,7 +52,11 @@ _carrier_setter = _CarrierSetter()
52
52
 
53
53
  def finish_span_grpc(span: Span, result):
54
54
  code = result._cython_call._status.code()
55
- if code != grpc.StatusCode.OK:
55
+
56
+ # grpc.StatusCode value is a tuple like:
57
+ # <StatusCode.OK: (0, 'ok')>
58
+ # so we cannot compare it with the code we get from the result directly
59
+ if code == grpc.StatusCode.OK.value[0]: # type: ignore
56
60
  span.set_status(
57
61
  Status(
58
62
  status_code=StatusCode.OK,
@@ -87,19 +91,25 @@ def start_span_client(
87
91
  SpanAttributes.RPC_SERVICE: service,
88
92
  }
89
93
 
90
- # add some attributes from the metadata
91
- if client_call_details.metadata is not None:
92
- mutable_metadata = OrderedDict(tuple(client_call_details.metadata))
93
- inject(mutable_metadata, setter=_carrier_setter) # type: ignore
94
- for key, value in mutable_metadata.items():
95
- client_call_details.metadata.add(key=key, value=value) # type: ignore
96
-
97
94
  span = tracer.start_span(
98
95
  name=method_name,
99
96
  kind=SpanKind.CLIENT,
100
97
  attributes=attributes,
101
98
  set_status_on_exception=set_status_on_exception,
102
99
  )
100
+
101
+ # Create a context containing the new span
102
+ span_context = set_span_in_context(span)
103
+
104
+ if client_call_details.metadata is not None:
105
+ mutable_metadata = OrderedDict(tuple(client_call_details.metadata))
106
+ else:
107
+ mutable_metadata = OrderedDict()
108
+ inject(mutable_metadata, context=span_context, setter=_carrier_setter)
109
+ if client_call_details.metadata is not None:
110
+ for key, value in mutable_metadata.items():
111
+ client_call_details.metadata.add(key=key, value=value) # type: ignore
112
+
103
113
  return span
104
114
 
105
115
 
@@ -20,6 +20,7 @@
20
20
  from datetime import datetime
21
21
  from functools import partial
22
22
  from typing import Any, Callable, Dict, List, Optional, Union
23
+ from urllib.parse import ParseResult
23
24
 
24
25
  import nats
25
26
  from nats.aio.client import Client
@@ -32,6 +33,7 @@ from opentelemetry.trace import SpanKind, Tracer
32
33
 
33
34
  from nucliadb_telemetry import logger, metrics
34
35
  from nucliadb_telemetry.common import set_span_exception
36
+ from nucliadb_telemetry.utils import get_telemetry
35
37
 
36
38
  msg_consume_time_histo = metrics.Histogram(
37
39
  # time it takes from when msg was queue to when it finished processing
@@ -238,17 +240,17 @@ class NatsClientTelemetry:
238
240
  async def publish(
239
241
  self,
240
242
  subject: str,
241
- body: bytes,
243
+ body: bytes = b"",
244
+ reply: str = "",
242
245
  headers: Optional[Dict[str, str]] = None,
243
- **kwargs,
244
- ):
246
+ ) -> None:
245
247
  tracer = self.tracer_provider.get_tracer(f"{self.service_name}_nc_publisher")
246
248
  headers = {} if headers is None else headers
247
249
  inject(headers)
248
250
 
249
251
  with start_span_message_publisher(tracer, subject) as span:
250
252
  try:
251
- await self.nc.publish(subject, body, headers=headers, **kwargs)
253
+ await self.nc.publish(subject, body, reply, headers)
252
254
  msg_sent_counter.inc({"subject": subject, "status": metrics.OK})
253
255
  except Exception as error:
254
256
  set_span_exception(span, error)
@@ -274,3 +276,46 @@ class NatsClientTelemetry:
274
276
  raise error
275
277
 
276
278
  return result
279
+
280
+ # Other methods we use but don't need telemetry
281
+
282
+ @property
283
+ def is_connected(self) -> bool:
284
+ return self.nc.is_connected
285
+
286
+ @property
287
+ def connected_url(self) -> Optional[ParseResult]:
288
+ return self.nc.connected_url
289
+
290
+ def jetstream(self, **opts) -> nats.js.JetStreamContext:
291
+ return self.nc.jetstream(**opts)
292
+
293
+ async def drain(self) -> None:
294
+ return await self.nc.drain()
295
+
296
+ async def flush(self, timeout: int = nats.aio.client.DEFAULT_FLUSH_TIMEOUT) -> None:
297
+ return await self.nc.flush(timeout)
298
+
299
+ async def close(self) -> None:
300
+ return await self.nc.close()
301
+
302
+
303
+ def get_traced_nats_client(nc: Client, service_name: str) -> Union[Client, NatsClientTelemetry]:
304
+ tracer_provider = get_telemetry(service_name)
305
+ if tracer_provider is not None:
306
+ return NatsClientTelemetry(nc, service_name, tracer_provider)
307
+ else:
308
+ return nc
309
+
310
+
311
+ def get_traced_jetstream(
312
+ nc: Union[Client, NatsClientTelemetry], service_name: str
313
+ ) -> Union[JetStreamContext, JetStreamContextTelemetry]:
314
+ jetstream = nc.jetstream()
315
+ tracer_provider = get_telemetry(service_name)
316
+
317
+ if tracer_provider is not None and jetstream is not None: # pragma: no cover
318
+ logger.info(f"Configuring {service_name} jetstream with telemetry")
319
+ return JetStreamContextTelemetry(jetstream, service_name, tracer_provider)
320
+ else:
321
+ return jetstream
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nucliadb_telemetry
3
- Version: 6.4.0.post4161
3
+ Version: 6.4.0.post4167
4
4
  Summary: NucliaDB Telemetry Library Python process
5
5
  Author-email: Nuclia <nucliadb@nuclia.com>
6
6
  License: AGPL
@@ -3,11 +3,11 @@ nucliadb_telemetry/batch_span.py,sha256=5cE7wt-H6wMXnJ5GUgjZyKvBWfaaZ-VWHS1m8d36
3
3
  nucliadb_telemetry/common.py,sha256=i5xxHlxeNZ1MPCoBvDDNkRDiYhvq6ghKwuaBnYGyhGU,1222
4
4
  nucliadb_telemetry/context.py,sha256=rhLs4NWGMjKzLjr6kTTjq-gaR0sAPJudkS-KjpW9LWc,2505
5
5
  nucliadb_telemetry/errors.py,sha256=QTKp-ErpsRb-xN3m6kMpq6tpOrOMQxsHVJAofKPo3Rk,4478
6
- nucliadb_telemetry/grpc.py,sha256=1CdueeJedxManSUQKwNOvqUuFiVhnMiZViXUJm81bIY,14806
6
+ nucliadb_telemetry/grpc.py,sha256=cNJRJLr0aRedKr5V_ufGqeidnMlBouML3QbFKkugTqg,15162
7
7
  nucliadb_telemetry/grpc_metrics.py,sha256=L2XMcJDxioNUYPX_IbKl_zvrG8uOFpF_a67c6yJq-Jw,5174
8
8
  nucliadb_telemetry/grpc_sentry.py,sha256=3eS_DVIX5MzvbJBkykf8JyZUf1b_b9w1KLhjoiW7oV0,2659
9
9
  nucliadb_telemetry/jaeger.py,sha256=HMQK8x_oaol5tCx3hCjaMG5Uzvsy_WWhKnD-FEZoYA4,7036
10
- nucliadb_telemetry/jetstream.py,sha256=E2_SY5-8ljhZAY0n_q-qlvewUmi2kF2En3mcqyA26kY,9977
10
+ nucliadb_telemetry/jetstream.py,sha256=UXQQdmnocgPGampxRe2Mv7t97qMrfGLO1hf-1pkEGa0,11492
11
11
  nucliadb_telemetry/logs.py,sha256=ag1e7tKEoNFgB1FuAmyTxdIHtTFwOJlGxPl2s1bhUic,9311
12
12
  nucliadb_telemetry/metrics.py,sha256=euexqkmlZip6QFbMfvJW1fSuha96J4ZHnMWixeVJnAs,8652
13
13
  nucliadb_telemetry/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -30,7 +30,7 @@ nucliadb_telemetry/tests/grpc/helloworld_pb2.py,sha256=ht4dmi0pAy6qDrwcjkbtSf_hO
30
30
  nucliadb_telemetry/tests/grpc/helloworld_pb2.pyi,sha256=mBZCQE6z7riQw6fOegJgZ5lHmXqO23_zxL8iQMqSqms,1191
31
31
  nucliadb_telemetry/tests/grpc/helloworld_pb2_grpc.py,sha256=_jxUNxl4Fx-JztK9RO5R6osjNP2sVNVPAxLnmczEYOc,2677
32
32
  nucliadb_telemetry/tests/grpc/helloworld_pb2_grpc.pyi,sha256=Y6teCx-PhPU-rI6w5ItLBKaTb34FLpngPnuDVWtNve4,958
33
- nucliadb_telemetry-6.4.0.post4161.dist-info/METADATA,sha256=_hP9pNf3JHE4uNMboO7ty3cg8Qlafjxq1Nb5u-u8DdY,11057
34
- nucliadb_telemetry-6.4.0.post4161.dist-info/WHEEL,sha256=ck4Vq1_RXyvS4Jt6SI0Vz6fyVs4GWg7AINwpsaGEgPE,91
35
- nucliadb_telemetry-6.4.0.post4161.dist-info/top_level.txt,sha256=3qEHI_5ttqQIL2gkNYwSlKsFyBBiEzDiIy9UKISzOaQ,19
36
- nucliadb_telemetry-6.4.0.post4161.dist-info/RECORD,,
33
+ nucliadb_telemetry-6.4.0.post4167.dist-info/METADATA,sha256=xK1l5D9NwFShKHDHAhr8m4Ts8JpNzNiGVQfVVzye2MY,11057
34
+ nucliadb_telemetry-6.4.0.post4167.dist-info/WHEEL,sha256=ooBFpIzZCPdw3uqIQsOo4qqbA4ZRPxHnOH7peeONza0,91
35
+ nucliadb_telemetry-6.4.0.post4167.dist-info/top_level.txt,sha256=3qEHI_5ttqQIL2gkNYwSlKsFyBBiEzDiIy9UKISzOaQ,19
36
+ nucliadb_telemetry-6.4.0.post4167.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.0.0)
2
+ Generator: setuptools (80.0.1)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5