From a37db08c46da9143a8e41f2030e1fa8c56f25a25 Mon Sep 17 00:00:00 2001 From: Artur Shiriev Date: Mon, 31 Mar 2025 12:05:36 +0300 Subject: [PATCH] exclude metrics and health checks path from OTL --- lite_bootstrap/bootstrappers/fastapi_bootstrapper.py | 9 ++++++++- lite_bootstrap/bootstrappers/litestar_bootstrapper.py | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py index 534343e..9e8fcd1 100644 --- a/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/fastapi_bootstrapper.py @@ -60,12 +60,19 @@ 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) + return excluded_urls + def bootstrap(self) -> None: super().bootstrap() FastAPIInstrumentor.instrument_app( app=self.bootstrap_config.application, tracer_provider=get_tracer_provider(), - excluded_urls=",".join(self.bootstrap_config.opentelemetry_excluded_urls), + excluded_urls=",".join(self._build_excluded_urls()), ) def teardown(self) -> None: diff --git a/lite_bootstrap/bootstrappers/litestar_bootstrapper.py b/lite_bootstrap/bootstrappers/litestar_bootstrapper.py index 8514c15..2c5d931 100644 --- a/lite_bootstrap/bootstrappers/litestar_bootstrapper.py +++ b/lite_bootstrap/bootstrappers/litestar_bootstrapper.py @@ -66,12 +66,19 @@ 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) + return excluded_urls + def bootstrap(self) -> None: super().bootstrap() self.bootstrap_config.application_config.middleware.append( OpenTelemetryConfig( tracer_provider=get_tracer_provider(), - exclude=self.bootstrap_config.opentelemetry_excluded_urls, + exclude=self._build_excluded_urls(), ).middleware, )