Skip to content

BaseTelemetryMiddleware

faststream.opentelemetry.middleware.BaseTelemetryMiddleware #

BaseTelemetryMiddleware(
    msg: Any | None,
    /,
    *,
    tracer: Tracer,
    settings_provider_factory: Callable[
        [Any],
        Optional[
            TelemetrySettingsProvider[
                Any, PublishCommandType
            ]
        ],
    ],
    metrics_container: _MetricsContainer,
    context: ContextRepo,
)

Bases: BaseMiddleware[PublishCommandType]

Source code in faststream/opentelemetry/middleware.py
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
def __init__(
    self,
    msg: Any | None,
    /,
    *,
    tracer: "Tracer",
    settings_provider_factory: Callable[
        [Any],
        Optional["TelemetrySettingsProvider[Any, PublishCommandType]"],
    ],
    metrics_container: _MetricsContainer,
    context: "ContextRepo",
) -> None:
    super().__init__(msg, context=context)

    self._tracer = tracer
    self._metrics = metrics_container
    self._current_span: Span | None = None
    self._origin_context: Context | None = None
    self._scope_tokens_stack = ExitStack()
    self.__settings_provider = settings_provider_factory(msg)

msg instance-attribute #

msg = msg

context instance-attribute #

context = context

publish_scope async #

publish_scope(
    call_next: AsyncFunc, msg: PublishCommandType
) -> Any
Source code in faststream/opentelemetry/middleware.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
async def publish_scope(
    self,
    call_next: "AsyncFunc",
    msg: "PublishCommandType",
) -> Any:
    if (provider := self.__settings_provider) is None:
        return await call_next(msg)

    headers = msg.headers
    current_context = context.get_current()
    destination_name = provider.get_publish_destination_name(msg)

    current_baggage: Baggage | None = self.context.get_local("baggage")
    if current_baggage:
        headers.update(current_baggage.to_headers())

    trace_attributes = provider.get_publish_attrs_from_cmd(msg)
    metrics_attributes = {
        SpanAttributes.MESSAGING_SYSTEM: provider.messaging_system,
        SpanAttributes.MESSAGING_DESTINATION_NAME: destination_name,
    }

    # NOTE: if batch with single message?
    if (msg_count := len(msg.batch_bodies)) > 1:
        trace_attributes[SpanAttributes.MESSAGING_BATCH_MESSAGE_COUNT] = msg_count
        current_context = _BAGGAGE_PROPAGATOR.extract(headers, current_context)
        _BAGGAGE_PROPAGATOR.inject(
            headers,
            baggage.set_baggage(WITH_BATCH, True, context=current_context),
        )

    if self._current_span and self._current_span.is_recording():
        current_context = trace.set_span_in_context(
            self._current_span,
            current_context,
        )
        _TRACE_PROPAGATOR.inject(headers, context=self._origin_context)

    else:
        create_span = self._tracer.start_span(
            name=_create_span_name(destination_name, MessageAction.CREATE),
            kind=trace.SpanKind.PRODUCER,
            attributes=trace_attributes,
        )
        current_context = trace.set_span_in_context(create_span)
        _TRACE_PROPAGATOR.inject(headers, context=current_context)
        create_span.end()

    start_time = time.perf_counter()

    try:
        with self._tracer.start_as_current_span(
            name=_create_span_name(destination_name, MessageAction.PUBLISH),
            kind=trace.SpanKind.PRODUCER,
            attributes=trace_attributes,
            context=current_context,
        ) as span:
            span.set_attribute(
                SpanAttributes.MESSAGING_OPERATION,
                MessageAction.PUBLISH,
            )
            msg.headers = headers
            result = await call_next(msg)

    except Exception as e:
        metrics_attributes[ERROR_TYPE] = type(e).__name__
        raise

    finally:
        duration = time.perf_counter() - start_time
        self._metrics.observe_publish(metrics_attributes, duration, msg_count)

    self._scope_tokens_stack.close()

    return result

consume_scope async #

consume_scope(
    call_next: AsyncFuncAny, msg: StreamMessage[Any]
) -> Any
Source code in faststream/opentelemetry/middleware.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
async def consume_scope(
    self,
    call_next: "AsyncFuncAny",
    msg: "StreamMessage[Any]",
) -> Any:
    if (provider := self.__settings_provider) is None:
        return await call_next(msg)

    if _is_batch_message(msg):
        links = _get_msg_links(msg)
        current_context = Context()
    else:
        links = None
        current_context = _TRACE_PROPAGATOR.extract(msg.headers)

    destination_name = provider.get_consume_destination_name(msg)
    trace_attributes = provider.get_consume_attrs_from_message(msg)
    metrics_attributes = {
        SpanAttributes.MESSAGING_SYSTEM: provider.messaging_system,
        MESSAGING_DESTINATION_PUBLISH_NAME: destination_name,
    }

    if not len(current_context):
        create_span = self._tracer.start_span(
            name=_create_span_name(destination_name, MessageAction.CREATE),
            kind=trace.SpanKind.CONSUMER,
            attributes=trace_attributes,
            links=links,
        )
        current_context = trace.set_span_in_context(create_span)
        create_span.end()

    self._origin_context = current_context
    start_time = time.perf_counter()

    try:
        with self._tracer.start_as_current_span(
            name=_create_span_name(destination_name, MessageAction.PROCESS),
            kind=trace.SpanKind.CONSUMER,
            context=current_context,
            attributes=trace_attributes,
            end_on_exit=False,
        ) as span:
            span.set_attribute(
                SpanAttributes.MESSAGING_OPERATION,
                MessageAction.PROCESS,
            )
            self._current_span = span

            self._scope_tokens_stack.enter_context(self.context.scope("span", span))
            self._scope_tokens_stack.enter_context(
                self.context.scope("baggage", Baggage.from_msg(msg))
            )

            new_context = trace.set_span_in_context(span, current_context)
            token = context.attach(new_context)
            result = await call_next(msg)
            context.detach(token)

    except Exception as e:
        metrics_attributes[ERROR_TYPE] = type(e).__name__
        raise

    finally:
        duration = time.perf_counter() - start_time
        msg_count = trace_attributes.get(
            SpanAttributes.MESSAGING_BATCH_MESSAGE_COUNT,
            1,
        )
        self._metrics.observe_consume(metrics_attributes, duration, msg_count)

    return result

after_processed async #

after_processed(
    exc_type: type[BaseException] | None = None,
    exc_val: BaseException | None = None,
    exc_tb: Optional[TracebackType] = None,
) -> bool | None
Source code in faststream/opentelemetry/middleware.py
340
341
342
343
344
345
346
347
348
async def after_processed(
    self,
    exc_type: type[BaseException] | None = None,
    exc_val: BaseException | None = None,
    exc_tb: Optional["TracebackType"] = None,
) -> bool | None:
    if self._current_span and self._current_span.is_recording():
        self._current_span.end()
    return False

on_receive async #

on_receive() -> None

Hook to call on message receive.

Source code in faststream/_internal/middlewares.py
31
32
async def on_receive(self) -> None:
    """Hook to call on message receive."""