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
15 changes: 8 additions & 7 deletions docs/introduction/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ To bootstrap Opentelemetry, you must provide at least:

Additional parameters:

- `opentelemetry_service_name`
- `opentelemetry_container_name`
- `opentelemetry_endpoint`
- `opentelemetry_namespace`
- `opentelemetry_insecure`
- `opentelemetry_instrumentors`
- `opentelemetry_log_traces`
- `opentelemetry_service_name` - if provided, will be passed to the `Resource` instead of `service_name`.
- `opentelemetry_container_name` - will be passed to the `Resource`.
- `opentelemetry_endpoint` - will be passed to `OTLPSpanExporter` as endpoint.
- `opentelemetry_namespace` - will be passed to the `Resource`.
- `opentelemetry_insecure` - is opentelemetry connection secure.
- `opentelemetry_instrumentors` - a list of extra instrumentors.
- `opentelemetry_log_traces` - traces will be logged to stdout.
- `opentelemetry_generate_health_check_spans` - generate spans for health check handlers if `True`.

Additional parameters for Litestar and FastAPI:

Expand Down
11 changes: 6 additions & 5 deletions lite_bootstrap/bootstrappers/fastapi_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ class FastAPILoggingInstrument(LoggingInstrument):
class FastAPIOpenTelemetryInstrument(OpenTelemetryInstrument):
bootstrap_config: FastAPIConfig

def _build_excluded_urls(self) -> list[str]:
excluded_urls = [*self.bootstrap_config.opentelemetry_excluded_urls]
for one_url in (self.bootstrap_config.health_checks_path, self.bootstrap_config.prometheus_metrics_path):
if one_url and one_url not in excluded_urls:
excluded_urls.append(one_url)
def _build_excluded_urls(self) -> set[str]:
excluded_urls = set(self.bootstrap_config.opentelemetry_excluded_urls)
excluded_urls.add(self.bootstrap_config.prometheus_metrics_path)
if not self.bootstrap_config.opentelemetry_generate_health_check_spans:
excluded_urls.add(self.bootstrap_config.health_checks_path)

return excluded_urls

def bootstrap(self) -> None:
Expand Down
8 changes: 8 additions & 0 deletions lite_bootstrap/bootstrappers/faststream_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import prometheus_client

if import_checker.is_opentelemetry_installed:
from opentelemetry import trace
from opentelemetry.metrics import Meter, MeterProvider
from opentelemetry.trace import TracerProvider, get_tracer_provider

tracer: typing.Final = trace.get_tracer(__name__)


@typing.runtime_checkable
class FastStreamTelemetryMiddlewareProtocol(typing.Protocol):
Expand Down Expand Up @@ -70,6 +73,11 @@ async def check_health(_: object) -> "AsgiResponse":
else AsgiResponse(b"Service is unhealthy", 500, headers={"content-type": "application/json"})
)

if self.bootstrap_config.opentelemetry_generate_health_check_spans:
check_health = tracer.start_as_current_span(f"GET {self.bootstrap_config.health_checks_path}")(
check_health,
)

self.bootstrap_config.application.mount(self.bootstrap_config.health_checks_path, check_health)

async def _define_health_status(self) -> bool:
Expand Down
13 changes: 7 additions & 6 deletions lite_bootstrap/bootstrappers/litestar_bootstrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,20 @@ class LitestarLoggingInstrument(LoggingInstrument):
class LitestarOpenTelemetryInstrument(OpenTelemetryInstrument):
bootstrap_config: LitestarConfig

def _build_excluded_urls(self) -> list[str]:
excluded_urls = [*self.bootstrap_config.opentelemetry_excluded_urls]
for one_url in (self.bootstrap_config.health_checks_path, self.bootstrap_config.prometheus_metrics_path):
if one_url and one_url not in excluded_urls:
excluded_urls.append(one_url)
def _build_excluded_urls(self) -> set[str]:
excluded_urls = set(self.bootstrap_config.opentelemetry_excluded_urls)
excluded_urls.add(self.bootstrap_config.prometheus_metrics_path)
if not self.bootstrap_config.opentelemetry_generate_health_check_spans:
excluded_urls.add(self.bootstrap_config.health_checks_path)

return excluded_urls

def bootstrap(self) -> None:
super().bootstrap()
self.bootstrap_config.application_config.middleware.append(
OpenTelemetryConfig(
tracer_provider=get_tracer_provider(),
exclude=self._build_excluded_urls(),
exclude=list(self._build_excluded_urls()),
).middleware,
)

Expand Down
1 change: 1 addition & 0 deletions lite_bootstrap/instruments/opentelemetry_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class OpentelemetryConfig(BaseConfig):
default_factory=list
)
opentelemetry_log_traces: bool = False
opentelemetry_generate_health_check_spans: bool = True


@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)
Expand Down
1 change: 1 addition & 0 deletions tests/test_fastapi_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def fastapi_config() -> FastAPIConfig:
opentelemetry_endpoint="otl",
opentelemetry_instrumentors=[CustomInstrumentor()],
opentelemetry_log_traces=True,
opentelemetry_generate_health_check_spans=False,
prometheus_metrics_path="/custom-metrics/",
sentry_dsn="https://testdsn@localhost/1",
swagger_offline_docs=True,
Expand Down
1 change: 1 addition & 0 deletions tests/test_litestar_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def litestar_config() -> LitestarConfig:
opentelemetry_endpoint="otl",
opentelemetry_instrumentors=[CustomInstrumentor()],
opentelemetry_log_traces=True,
opentelemetry_generate_health_check_spans=False,
prometheus_metrics_path="/custom-metrics/",
sentry_dsn="https://testdsn@localhost/1",
swagger_offline_docs=True,
Expand Down
Loading