Skip to content

Observability#

OpenTelemetry#

SqsTelemetryMiddleware traces and measures publishes and deliveries. Install the otel extra.

service.py
from faststream import FastStream
from opentelemetry.sdk.trace import TracerProvider

from faststream_sqs_sns import CapoSqsBroker
from faststream_sqs_sns.opentelemetry import SqsTelemetryMiddleware

tracer_provider = TracerProvider()

broker = CapoSqsBroker(
    region="eu-central-1",
    middlewares=(SqsTelemetryMiddleware(tracer_provider=tracer_provider),),
)
app = FastStream(broker)


@broker.subscriber("orders")
async def bill(order: dict[str, int]) -> None: ...

The spans carry messaging.system set to aws_sqs, messaging.destination.name (the queue or topic name), messaging.message.id, messaging.message.conversation_id (the correlation id), the body size, and the message count for batch subscribers and publish_batch.

The middleware takes tracer_provider, meter_provider, meter and include_messages_counters, as FastStream's TelemetryMiddleware does.

Prometheus#

SqsPrometheusMiddleware exports the metrics of publishes and deliveries to a Prometheus registry. Install the prometheus extra.

service.py
from faststream import FastStream
from prometheus_client import CollectorRegistry, make_asgi_app

from faststream_sqs_sns import CapoSqsBroker
from faststream_sqs_sns.prometheus import SqsPrometheusMiddleware

registry = CollectorRegistry()

broker = CapoSqsBroker(
    region="eu-central-1",
    middlewares=(SqsPrometheusMiddleware(registry=registry, app_name="billing"),),
)
app = FastStream(broker)

# serve the metrics on any ASGI server
metrics_app = make_asgi_app(registry)


@broker.subscriber("orders")
async def bill(order: dict[str, int]) -> None: ...

The middleware takes registry, app_name, metrics_prefix, received_messages_size_buckets and custom_labels, as FastStream's PrometheusMiddleware does. Destinations carry the same names as in the traces.