faststream icon indicating copy to clipboard operation
faststream copied to clipboard

Feature: faststream should only require opentelemetry-api

Open florianmutter opened this issue 1 year ago • 2 comments

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

florianmutter avatar Jun 20 '24 13:06 florianmutter

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?

Lancetnik avatar Jun 20 '24 16:06 Lancetnik

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

draincoder avatar Jul 02 '24 21:07 draincoder