airtai / faststream

FastStream is a powerful and easy-to-use Python framework for building asynchronous services interacting with event streams such as Apache Kafka, RabbitMQ, NATS and Redis.
https://faststream.airt.ai/latest/
Apache License 2.0
2.12k stars 99 forks source link

Feature: faststream should only require opentelemetry-api #1540

Open florianmutter opened 1 week ago

florianmutter commented 1 week ago

To make it easier to switch the opentelemetry implementation it should be up to the user to install an implementation. The documentation can suggest opentelemetry-sdk but it should not be installed by default. This would allow a user to replace opentelemetry-sdk with a different implementation (e.g. Datadogs ddtrace that also implements the opentelemetry-api: https://ddtrace.readthedocs.io/en/stable/api.html#opentelemetry-api)

This is also recommended by OpenTelemetry here: https://opentelemetry.io/docs/concepts/instrumentation/libraries/#opentelemetry-api

Lancetnik commented 1 week ago

As I can see, ddtrace can be integrated this way (from the doc)

import os
# Must be set before ddtrace is imported!
os.environ["DD_TRACE_OTEL_ENABLED"] = "true"

from opentelemetry.trace import set_tracer_provider
from ddtrace.opentelemetry import TracerProvider

set_tracer_provider(TracerProvider())

You can adopt this example to FastStream the following way

import os
# Must be set before ddtrace is imported!
os.environ["DD_TRACE_OTEL_ENABLED"] = "true"

from opentelemetry import trace
from ddtrace.opentelemetry import TracerProvider

tracer_provider = TracerProvider()
trace.set_tracer_provider(tracer_provider)

from faststream import FastStream
from faststream.nats import NatsBroker
from faststream.nats.opentelemetry import NatsTelemetryMiddleware

broker = NatsBroker(
    middlewares=(
        NatsTelemetryMiddleware(tracer_provider=tracer_provider),
    )
)
app = FastStream(broker)

I am not experted enough to tell that is the thing you want, but all look fine for me

@draincoder what do you think?

draincoder commented 11 hours ago

It's a good idea, I'll look at which other libraries have an opentelemetry-api implementation and analyze this proposal.