Skip to content

Commit 92c8b08

Browse files
authored
Merge pull request #59 from modern-python/add-settings-for-traces-for-health-checks
allow managing health-checks spans
2 parents 51402ca + d3a3a09 commit 92c8b08

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

docs/introduction/configuration.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ To bootstrap Opentelemetry, you must provide at least:
6767

6868
Additional parameters:
6969

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

7879
Additional parameters for Litestar and FastAPI:
7980

lite_bootstrap/bootstrappers/fastapi_bootstrapper.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ class FastAPILoggingInstrument(LoggingInstrument):
104104
class FastAPIOpenTelemetryInstrument(OpenTelemetryInstrument):
105105
bootstrap_config: FastAPIConfig
106106

107-
def _build_excluded_urls(self) -> list[str]:
108-
excluded_urls = [*self.bootstrap_config.opentelemetry_excluded_urls]
109-
for one_url in (self.bootstrap_config.health_checks_path, self.bootstrap_config.prometheus_metrics_path):
110-
if one_url and one_url not in excluded_urls:
111-
excluded_urls.append(one_url)
107+
def _build_excluded_urls(self) -> set[str]:
108+
excluded_urls = set(self.bootstrap_config.opentelemetry_excluded_urls)
109+
excluded_urls.add(self.bootstrap_config.prometheus_metrics_path)
110+
if not self.bootstrap_config.opentelemetry_generate_health_check_spans:
111+
excluded_urls.add(self.bootstrap_config.health_checks_path)
112+
112113
return excluded_urls
113114

114115
def bootstrap(self) -> None:

lite_bootstrap/bootstrappers/faststream_bootstrapper.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
import prometheus_client
2020

2121
if import_checker.is_opentelemetry_installed:
22+
from opentelemetry import trace
2223
from opentelemetry.metrics import Meter, MeterProvider
2324
from opentelemetry.trace import TracerProvider, get_tracer_provider
2425

26+
tracer: typing.Final = trace.get_tracer(__name__)
27+
2528

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

76+
if self.bootstrap_config.opentelemetry_generate_health_check_spans:
77+
check_health = tracer.start_as_current_span(f"GET {self.bootstrap_config.health_checks_path}")(
78+
check_health,
79+
)
80+
7381
self.bootstrap_config.application.mount(self.bootstrap_config.health_checks_path, check_health)
7482

7583
async def _define_health_status(self) -> bool:

lite_bootstrap/bootstrappers/litestar_bootstrapper.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,20 @@ class LitestarLoggingInstrument(LoggingInstrument):
100100
class LitestarOpenTelemetryInstrument(OpenTelemetryInstrument):
101101
bootstrap_config: LitestarConfig
102102

103-
def _build_excluded_urls(self) -> list[str]:
104-
excluded_urls = [*self.bootstrap_config.opentelemetry_excluded_urls]
105-
for one_url in (self.bootstrap_config.health_checks_path, self.bootstrap_config.prometheus_metrics_path):
106-
if one_url and one_url not in excluded_urls:
107-
excluded_urls.append(one_url)
103+
def _build_excluded_urls(self) -> set[str]:
104+
excluded_urls = set(self.bootstrap_config.opentelemetry_excluded_urls)
105+
excluded_urls.add(self.bootstrap_config.prometheus_metrics_path)
106+
if not self.bootstrap_config.opentelemetry_generate_health_check_spans:
107+
excluded_urls.add(self.bootstrap_config.health_checks_path)
108+
108109
return excluded_urls
109110

110111
def bootstrap(self) -> None:
111112
super().bootstrap()
112113
self.bootstrap_config.application_config.middleware.append(
113114
OpenTelemetryConfig(
114115
tracer_provider=get_tracer_provider(),
115-
exclude=self._build_excluded_urls(),
116+
exclude=list(self._build_excluded_urls()),
116117
).middleware,
117118
)
118119

lite_bootstrap/instruments/opentelemetry_instrument.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class OpentelemetryConfig(BaseConfig):
3636
default_factory=list
3737
)
3838
opentelemetry_log_traces: bool = False
39+
opentelemetry_generate_health_check_spans: bool = True
3940

4041

4142
@dataclasses.dataclass(kw_only=True, slots=True, frozen=True)

tests/test_fastapi_bootstrap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def fastapi_config() -> FastAPIConfig:
2828
opentelemetry_endpoint="otl",
2929
opentelemetry_instrumentors=[CustomInstrumentor()],
3030
opentelemetry_log_traces=True,
31+
opentelemetry_generate_health_check_spans=False,
3132
prometheus_metrics_path="/custom-metrics/",
3233
sentry_dsn="https://testdsn@localhost/1",
3334
swagger_offline_docs=True,

tests/test_litestar_bootstrap.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def litestar_config() -> LitestarConfig:
2222
opentelemetry_endpoint="otl",
2323
opentelemetry_instrumentors=[CustomInstrumentor()],
2424
opentelemetry_log_traces=True,
25+
opentelemetry_generate_health_check_spans=False,
2526
prometheus_metrics_path="/custom-metrics/",
2627
sentry_dsn="https://testdsn@localhost/1",
2728
swagger_offline_docs=True,

0 commit comments

Comments
 (0)