Skip to content

Commit e2d24ac

Browse files
authored
Merge pull request #19 from modern-python/18-feature-exclude-health-and-metrics-routes-from-instruments-by-default
exclude metrics and health checks path from OTL
2 parents 2934ace + a37db08 commit e2d24ac

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lite_bootstrap/bootstrappers/fastapi_bootstrapper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ class FastAPILoggingInstrument(LoggingInstrument):
6060
class FastAPIOpenTelemetryInstrument(OpenTelemetryInstrument):
6161
bootstrap_config: FastAPIConfig
6262

63+
def _build_excluded_urls(self) -> list[str]:
64+
excluded_urls = [*self.bootstrap_config.opentelemetry_excluded_urls]
65+
for one_url in (self.bootstrap_config.health_checks_path, self.bootstrap_config.prometheus_metrics_path):
66+
if one_url and one_url not in excluded_urls:
67+
excluded_urls.append(one_url)
68+
return excluded_urls
69+
6370
def bootstrap(self) -> None:
6471
super().bootstrap()
6572
FastAPIInstrumentor.instrument_app(
6673
app=self.bootstrap_config.application,
6774
tracer_provider=get_tracer_provider(),
68-
excluded_urls=",".join(self.bootstrap_config.opentelemetry_excluded_urls),
75+
excluded_urls=",".join(self._build_excluded_urls()),
6976
)
7077

7178
def teardown(self) -> None:

lite_bootstrap/bootstrappers/litestar_bootstrapper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,19 @@ class LitestarLoggingInstrument(LoggingInstrument):
6666
class LitestarOpenTelemetryInstrument(OpenTelemetryInstrument):
6767
bootstrap_config: LitestarConfig
6868

69+
def _build_excluded_urls(self) -> list[str]:
70+
excluded_urls = [*self.bootstrap_config.opentelemetry_excluded_urls]
71+
for one_url in (self.bootstrap_config.health_checks_path, self.bootstrap_config.prometheus_metrics_path):
72+
if one_url and one_url not in excluded_urls:
73+
excluded_urls.append(one_url)
74+
return excluded_urls
75+
6976
def bootstrap(self) -> None:
7077
super().bootstrap()
7178
self.bootstrap_config.application_config.middleware.append(
7279
OpenTelemetryConfig(
7380
tracer_provider=get_tracer_provider(),
74-
exclude=self.bootstrap_config.opentelemetry_excluded_urls,
81+
exclude=self._build_excluded_urls(),
7582
).middleware,
7683
)
7784

0 commit comments

Comments
 (0)