Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lite_bootstrap/bootstrappers/faststream_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def is_configured(cls, bootstrap_config: "FastStreamConfig") -> bool: # ty: ign
return super().is_configured(bootstrap_config) and bool(bootstrap_config.opentelemetry_middleware_cls)

def bootstrap(self) -> None:
super().bootstrap()
config = self.bootstrap_config
if config.opentelemetry_middleware_cls and config.application.broker:
config.application.broker.add_middleware(
Expand Down
41 changes: 41 additions & 0 deletions tests/test_faststream_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from faststream.redis import RedisBroker, TestRedisBroker
from faststream.redis.opentelemetry import RedisTelemetryMiddleware
from faststream.redis.prometheus import RedisPrometheusMiddleware
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.sdk import resources
from starlette import status
from starlette.testclient import TestClient

Expand Down Expand Up @@ -315,3 +317,42 @@ def test_second_faststream_bootstrapper_bootstrap_raises(broker: RedisBroker) ->
second.bootstrap()
finally:
first.teardown()


def test_faststream_bootstrap_builds_its_own_tracer_provider(broker: RedisBroker) -> None:
bootstrapper = FastStreamBootstrapper(bootstrap_config=build_faststream_config(broker=broker))
bootstrapper.bootstrap()
try:
instruments = [one for one in bootstrapper.instruments if isinstance(one, FastStreamOpenTelemetryInstrument)]
assert len(instruments) == 1
tracer_provider = instruments[0]._tracer_provider # noqa: SLF001
assert tracer_provider is not None
assert tracer_provider.resource.attributes[resources.SERVICE_NAME] == "microservice"
finally:
bootstrapper.teardown()


def test_faststream_bootstrap_applies_opentelemetry_instrumentors(broker: RedisBroker) -> None:
recorded_tracer_providers: list[object] = []

class RecordingInstrumentor(BaseInstrumentor):
def instrumentation_dependencies(self) -> typing.Collection[str]:
return []

def _instrument(self, **kwargs: object) -> None:
recorded_tracer_providers.append(kwargs["tracer_provider"])

def _uninstrument(self, **_kwargs: object) -> None: ...

bootstrap_config = dataclasses.replace(
build_faststream_config(broker=broker),
opentelemetry_instrumentors=[RecordingInstrumentor()],
)
bootstrapper = FastStreamBootstrapper(bootstrap_config=bootstrap_config)
bootstrapper.bootstrap()
try:
instruments = [one for one in bootstrapper.instruments if isinstance(one, FastStreamOpenTelemetryInstrument)]
assert len(instruments) == 1
assert recorded_tracer_providers == [instruments[0]._tracer_provider] # noqa: SLF001
finally:
bootstrapper.teardown()
Loading