Coverage for faststream/opentelemetry/middleware.py: 97%
149 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 21:20 +0000
1import time
2from collections import defaultdict
3from collections.abc import Callable
4from contextlib import ExitStack
5from copy import copy
6from typing import TYPE_CHECKING, Any, Optional, cast
8from opentelemetry import baggage, context, metrics, trace
9from opentelemetry.baggage.propagation import W3CBaggagePropagator
10from opentelemetry.context import Context
11from opentelemetry.semconv.trace import SpanAttributes
12from opentelemetry.trace import Link, Span
13from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
15from faststream._internal.middlewares import BaseMiddleware
16from faststream._internal.types import BrokerMiddleware, PublishCommandType
17from faststream.opentelemetry.baggage import Baggage
18from faststream.opentelemetry.consts import (
19 ERROR_TYPE,
20 INSTRUMENTING_LIBRARY_VERSION,
21 INSTRUMENTING_MODULE_NAME,
22 MESSAGING_DESTINATION_PUBLISH_NAME,
23 OTEL_SCHEMA,
24 WITH_BATCH,
25 MessageAction,
26)
28if TYPE_CHECKING:
29 from types import TracebackType
31 from opentelemetry.metrics import Meter, MeterProvider
32 from opentelemetry.trace import Tracer, TracerProvider
33 from opentelemetry.util.types import Attributes
35 from faststream._internal.basic_types import AsyncFunc, AsyncFuncAny
36 from faststream._internal.context.repository import ContextRepo
37 from faststream.message import StreamMessage
38 from faststream.opentelemetry.provider import TelemetrySettingsProvider
41_BAGGAGE_PROPAGATOR = W3CBaggagePropagator()
42_TRACE_PROPAGATOR = TraceContextTextMapPropagator()
45class TelemetryMiddleware(BrokerMiddleware[Any, PublishCommandType]):
46 __slots__ = (
47 "_meter",
48 "_metrics",
49 "_settings_provider_factory",
50 "_tracer",
51 )
53 def __init__(
54 self,
55 *,
56 settings_provider_factory: Callable[
57 [Any],
58 Optional["TelemetrySettingsProvider[Any, PublishCommandType]"],
59 ],
60 tracer_provider: Optional["TracerProvider"] = None,
61 meter_provider: Optional["MeterProvider"] = None,
62 meter: Optional["Meter"] = None,
63 include_messages_counters: bool = False,
64 ) -> None:
65 self._tracer = _get_tracer(tracer_provider)
66 self._meter = _get_meter(meter_provider, meter)
67 self._metrics = _MetricsContainer(self._meter, include_messages_counters)
68 self._settings_provider_factory = settings_provider_factory
70 def __call__(
71 self,
72 msg: Any | None,
73 /,
74 *,
75 context: "ContextRepo",
76 ) -> "BaseTelemetryMiddleware[PublishCommandType]":
77 return BaseTelemetryMiddleware[PublishCommandType](
78 msg,
79 tracer=self._tracer,
80 metrics_container=self._metrics,
81 settings_provider_factory=self._settings_provider_factory,
82 context=context,
83 )
86class _MetricsContainer:
87 __slots__ = (
88 "include_messages_counters",
89 "process_counter",
90 "process_duration",
91 "publish_counter",
92 "publish_duration",
93 )
95 def __init__(self, meter: "Meter", include_messages_counters: bool) -> None:
96 self.include_messages_counters = include_messages_counters
98 self.publish_duration = meter.create_histogram(
99 name="messaging.publish.duration",
100 unit="s",
101 description="Measures the duration of publish operation.",
102 )
103 self.process_duration = meter.create_histogram(
104 name="messaging.process.duration",
105 unit="s",
106 description="Measures the duration of process operation.",
107 )
109 if include_messages_counters:
110 self.process_counter = meter.create_counter(
111 name="messaging.process.messages",
112 unit="message",
113 description="Measures the number of processed messages.",
114 )
115 self.publish_counter = meter.create_counter(
116 name="messaging.publish.messages",
117 unit="message",
118 description="Measures the number of published messages.",
119 )
121 def observe_publish(
122 self,
123 attrs: dict[str, Any],
124 duration: float,
125 msg_count: int,
126 ) -> None:
127 self.publish_duration.record(
128 amount=duration,
129 attributes=attrs,
130 )
131 if self.include_messages_counters:
132 counter_attrs = copy(attrs)
133 counter_attrs.pop(ERROR_TYPE, None)
134 self.publish_counter.add(
135 amount=msg_count,
136 attributes=counter_attrs,
137 )
139 def observe_consume(
140 self,
141 attrs: dict[str, Any],
142 duration: float,
143 msg_count: int,
144 ) -> None:
145 self.process_duration.record(
146 amount=duration,
147 attributes=attrs,
148 )
149 if self.include_messages_counters:
150 counter_attrs = copy(attrs)
151 counter_attrs.pop(ERROR_TYPE, None)
152 self.process_counter.add(
153 amount=msg_count,
154 attributes=counter_attrs,
155 )
158class BaseTelemetryMiddleware(BaseMiddleware[PublishCommandType]):
159 # `__settings_provider` is name-mangled here exactly as the attribute is.
160 __slots__ = (
161 "__settings_provider",
162 "_current_span",
163 "_metrics",
164 "_origin_context",
165 "_scope_tokens_stack",
166 "_tracer",
167 )
169 def __init__(
170 self,
171 msg: Any | None,
172 /,
173 *,
174 tracer: "Tracer",
175 settings_provider_factory: Callable[
176 [Any],
177 Optional["TelemetrySettingsProvider[Any, PublishCommandType]"],
178 ],
179 metrics_container: _MetricsContainer,
180 context: "ContextRepo",
181 ) -> None:
182 super().__init__(msg, context=context)
184 self._tracer = tracer
185 self._metrics = metrics_container
186 self._current_span: Span | None = None
187 self._origin_context: Context | None = None
188 self._scope_tokens_stack = ExitStack()
189 self.__settings_provider = settings_provider_factory(msg)
191 async def publish_scope(
192 self,
193 call_next: "AsyncFunc",
194 msg: "PublishCommandType",
195 ) -> Any:
196 if (provider := self.__settings_provider) is None: 196 ↛ 197line 196 didn't jump to line 197 because the condition on line 196 was never true
197 return await call_next(msg)
199 headers = msg.headers
200 current_context = context.get_current()
201 destination_name = provider.get_publish_destination_name(msg)
203 current_baggage: Baggage | None = self.context.get_local("baggage")
204 if current_baggage:
205 headers.update(current_baggage.to_headers())
207 trace_attributes = provider.get_publish_attrs_from_cmd(msg)
208 metrics_attributes = {
209 SpanAttributes.MESSAGING_SYSTEM: provider.messaging_system,
210 SpanAttributes.MESSAGING_DESTINATION_NAME: destination_name,
211 }
213 # NOTE: if batch with single message?
214 if (msg_count := len(msg.batch_bodies)) > 1:
215 trace_attributes[SpanAttributes.MESSAGING_BATCH_MESSAGE_COUNT] = msg_count
216 current_context = _BAGGAGE_PROPAGATOR.extract(headers, current_context)
217 _BAGGAGE_PROPAGATOR.inject(
218 headers,
219 baggage.set_baggage(WITH_BATCH, True, context=current_context),
220 )
222 if self._current_span and self._current_span.is_recording():
223 current_context = trace.set_span_in_context(
224 self._current_span,
225 current_context,
226 )
227 _TRACE_PROPAGATOR.inject(headers, context=self._origin_context)
229 else:
230 create_span = self._tracer.start_span(
231 name=_create_span_name(destination_name, MessageAction.CREATE),
232 kind=trace.SpanKind.PRODUCER,
233 attributes=trace_attributes,
234 )
235 current_context = trace.set_span_in_context(create_span)
236 _TRACE_PROPAGATOR.inject(headers, context=current_context)
237 create_span.end()
239 start_time = time.perf_counter()
241 try:
242 with self._tracer.start_as_current_span(
243 name=_create_span_name(destination_name, MessageAction.PUBLISH),
244 kind=trace.SpanKind.PRODUCER,
245 attributes=trace_attributes,
246 context=current_context,
247 ) as span:
248 span.set_attribute(
249 SpanAttributes.MESSAGING_OPERATION,
250 MessageAction.PUBLISH,
251 )
252 msg.headers = headers
253 result = await call_next(msg)
255 except Exception as e:
256 metrics_attributes[ERROR_TYPE] = type(e).__name__
257 raise
259 finally:
260 duration = time.perf_counter() - start_time
261 self._metrics.observe_publish(metrics_attributes, duration, msg_count)
263 self._scope_tokens_stack.close()
265 return result
267 async def consume_scope(
268 self,
269 call_next: "AsyncFuncAny",
270 msg: "StreamMessage[Any]",
271 ) -> Any:
272 if (provider := self.__settings_provider) is None:
273 return await call_next(msg)
275 if _is_batch_message(msg):
276 links = _get_msg_links(msg)
277 current_context = Context()
278 else:
279 links = None
280 current_context = _TRACE_PROPAGATOR.extract(msg.headers)
282 destination_name = provider.get_consume_destination_name(msg)
283 trace_attributes = provider.get_consume_attrs_from_message(msg)
284 metrics_attributes = {
285 SpanAttributes.MESSAGING_SYSTEM: provider.messaging_system,
286 MESSAGING_DESTINATION_PUBLISH_NAME: destination_name,
287 }
289 if not len(current_context):
290 create_span = self._tracer.start_span(
291 name=_create_span_name(destination_name, MessageAction.CREATE),
292 kind=trace.SpanKind.CONSUMER,
293 attributes=trace_attributes,
294 links=links,
295 )
296 current_context = trace.set_span_in_context(create_span)
297 create_span.end()
299 self._origin_context = current_context
300 start_time = time.perf_counter()
302 try:
303 with self._tracer.start_as_current_span(
304 name=_create_span_name(destination_name, MessageAction.PROCESS),
305 kind=trace.SpanKind.CONSUMER,
306 context=current_context,
307 attributes=trace_attributes,
308 end_on_exit=False,
309 ) as span:
310 span.set_attribute(
311 SpanAttributes.MESSAGING_OPERATION,
312 MessageAction.PROCESS,
313 )
314 self._current_span = span
316 self._scope_tokens_stack.enter_context(self.context.scope("span", span))
317 self._scope_tokens_stack.enter_context(
318 self.context.scope("baggage", Baggage.from_msg(msg))
319 )
321 new_context = trace.set_span_in_context(span, current_context)
322 token = context.attach(new_context)
323 result = await call_next(msg)
324 context.detach(token)
326 except Exception as e:
327 metrics_attributes[ERROR_TYPE] = type(e).__name__
328 raise
330 finally:
331 duration = time.perf_counter() - start_time
332 msg_count = trace_attributes.get(
333 SpanAttributes.MESSAGING_BATCH_MESSAGE_COUNT,
334 1,
335 )
336 self._metrics.observe_consume(metrics_attributes, duration, msg_count)
338 return result
340 async def after_processed(
341 self,
342 exc_type: type[BaseException] | None = None,
343 exc_val: BaseException | None = None,
344 exc_tb: Optional["TracebackType"] = None,
345 ) -> bool | None:
346 if self._current_span and self._current_span.is_recording():
347 self._current_span.end()
348 return False
351def _get_meter(
352 meter_provider: Optional["MeterProvider"] = None,
353 meter: Optional["Meter"] = None,
354) -> "Meter":
355 if meter is None: 355 ↛ 361line 355 didn't jump to line 361 because the condition on line 355 was always true
356 return metrics.get_meter(
357 __name__,
358 meter_provider=meter_provider,
359 schema_url=OTEL_SCHEMA,
360 )
361 return meter
364def _get_tracer(tracer_provider: Optional["TracerProvider"] = None) -> "Tracer":
365 return trace.get_tracer(
366 instrumenting_module_name=INSTRUMENTING_MODULE_NAME,
367 instrumenting_library_version=INSTRUMENTING_LIBRARY_VERSION,
368 tracer_provider=tracer_provider,
369 schema_url=OTEL_SCHEMA,
370 )
373def _create_span_name(destination: str, action: str) -> str:
374 return f"{destination} {action}"
377def _is_batch_message(msg: "StreamMessage[Any]") -> bool:
378 with_batch = baggage.get_baggage(
379 WITH_BATCH,
380 _BAGGAGE_PROPAGATOR.extract(msg.headers),
381 )
382 return bool(msg.batch_headers or with_batch)
385def _get_msg_links(msg: "StreamMessage[Any]") -> list[Link]:
386 if not msg.batch_headers:
387 if (span := _get_span_from_headers(msg.headers)) is not None:
388 return [Link(span.get_span_context())]
389 return []
391 links = {}
392 counter: dict[str, int] = defaultdict(lambda: 0)
394 for headers in msg.batch_headers:
395 if (correlation_id := headers.get("correlation_id")) is None:
396 continue
398 counter[correlation_id] += 1
400 if (span := _get_span_from_headers(headers)) is None:
401 continue
403 attributes = _get_link_attributes(counter[correlation_id])
405 links[correlation_id] = Link(
406 span.get_span_context(),
407 attributes=attributes,
408 )
410 return list(links.values())
413def _get_span_from_headers(headers: dict[str, Any]) -> Span | None:
414 trace_context = _TRACE_PROPAGATOR.extract(headers)
415 if not len(trace_context):
416 return None
418 return cast(
419 "Span | None",
420 next(iter(trace_context.values())),
421 )
424def _get_link_attributes(message_count: int) -> "Attributes":
425 if message_count <= 1:
426 return {}
427 return {
428 SpanAttributes.MESSAGING_BATCH_MESSAGE_COUNT: message_count,
429 }