|
| 1 | +"""Bootstrap smoke at the declared dependency floors. |
| 2 | +
|
| 3 | +Run against an environment resolved with `uv pip install --resolution lowest-direct`, |
| 4 | +so every direct dependency sits at the floor `pyproject.toml` declares. Takes one |
| 5 | +target naming the bootstrapper to exercise. Exits non-zero on failure. |
| 6 | +
|
| 7 | +An import check is not enough: half the floors this repo carries were found by a call |
| 8 | +made during bootstrap or teardown, not by a missing module. So each target builds a |
| 9 | +config with every instrument its extras can supply, bootstraps, exercises the calls |
| 10 | +that set a floor, and tears down. |
| 11 | +
|
| 12 | +`InstrumentSkippedWarning` is escalated to an error, because an instrument that |
| 13 | +degrades to a silent skip would let a too-low floor pass as success. |
| 14 | +
|
| 15 | +Not a pytest test: conftest.py hard-imports opentelemetry, sentry_sdk and structlog, |
| 16 | +which most of these targets do not install. |
| 17 | +""" |
| 18 | + |
| 19 | +import asyncio |
| 20 | +import sys |
| 21 | +import typing |
| 22 | +import warnings |
| 23 | + |
| 24 | +from lite_bootstrap import ( |
| 25 | + FastAPIBootstrapper, |
| 26 | + FastAPIConfig, |
| 27 | + FastMcpBootstrapper, |
| 28 | + FastMcpConfig, |
| 29 | + FastStreamBootstrapper, |
| 30 | + FastStreamConfig, |
| 31 | + FreeBootstrapper, |
| 32 | + FreeConfig, |
| 33 | + InstrumentSkippedWarning, |
| 34 | + LitestarBootstrapper, |
| 35 | + LitestarConfig, |
| 36 | + import_checker, |
| 37 | +) |
| 38 | +from lite_bootstrap.instruments.logging_factory import StructuredLogPayload, _serialize_log_to_string |
| 39 | + |
| 40 | + |
| 41 | +if import_checker.is_opentelemetry_installed: |
| 42 | + from opentelemetry import trace |
| 43 | + |
| 44 | +if import_checker.is_sentry_installed: |
| 45 | + import sentry_sdk |
| 46 | + |
| 47 | + class DroppingTransport(sentry_sdk.Transport): |
| 48 | + """Keep sentry configured without a server: teardown's flush would otherwise retry for minutes.""" |
| 49 | + |
| 50 | + def capture_envelope(self, envelope: object) -> None: |
| 51 | + """Drop the envelope.""" |
| 52 | + |
| 53 | + |
| 54 | +OTLP_ENDPOINT: typing.Final = "localhost:4317" |
| 55 | +PYROSCOPE_ENDPOINT: typing.Final = "http://localhost:4040" |
| 56 | +SENTRY_DSN: typing.Final = "https://testdsn@localhost/1" |
| 57 | +SENTRY_PARAMS: typing.Final = {"transport": DroppingTransport()} if import_checker.is_sentry_installed else {} |
| 58 | +HEALTH_PATH: typing.Final = "/floor-health/" |
| 59 | + |
| 60 | + |
| 61 | +def _emit_span() -> None: |
| 62 | + """Force _format_span -> ReadableSpan.to_json(indent=None) through the console exporter.""" |
| 63 | + with trace.get_tracer("floor-smoke").start_as_current_span("floor-smoke-span"): |
| 64 | + pass |
| 65 | + |
| 66 | + |
| 67 | +def _check_orjson_serializer() -> None: |
| 68 | + payload = StructuredLogPayload.parse(_serialize_log_to_string({"event": "floor ok", "level": "info", "n": 1})) |
| 69 | + assert payload is not None |
| 70 | + assert payload.message == "floor ok" |
| 71 | + assert payload.extra == {"n": 1} |
| 72 | + |
| 73 | + |
| 74 | +async def _drive_lifespan(application: typing.Any) -> None: # noqa: ANN401 |
| 75 | + """Run the ASGI lifespan through startup and shutdown without a test client.""" |
| 76 | + queue: asyncio.Queue[dict] = asyncio.Queue() |
| 77 | + await queue.put({"type": "lifespan.startup"}) |
| 78 | + |
| 79 | + async def receive() -> dict: |
| 80 | + return await queue.get() |
| 81 | + |
| 82 | + async def send(message: dict) -> None: |
| 83 | + if message["type"] == "lifespan.startup.complete": |
| 84 | + await queue.put({"type": "lifespan.shutdown"}) |
| 85 | + elif message["type"].endswith(".failed"): |
| 86 | + raise RuntimeError(message) |
| 87 | + |
| 88 | + await application({"type": "lifespan", "state": {}}, receive, send) |
| 89 | + |
| 90 | + |
| 91 | +def _free() -> None: |
| 92 | + bootstrapper = FreeBootstrapper( |
| 93 | + bootstrap_config=FreeConfig( |
| 94 | + service_name="floor-smoke", |
| 95 | + service_version="1.0.0", |
| 96 | + service_environment="test", |
| 97 | + logging_buffer_capacity=0, |
| 98 | + sentry_dsn=SENTRY_DSN, |
| 99 | + sentry_additional_params=SENTRY_PARAMS, |
| 100 | + sentry_tags={"floor": "smoke"}, |
| 101 | + pyroscope_endpoint=PYROSCOPE_ENDPOINT, |
| 102 | + opentelemetry_endpoint=OTLP_ENDPOINT, |
| 103 | + opentelemetry_log_traces=True, |
| 104 | + ) |
| 105 | + ) |
| 106 | + bootstrapper.bootstrap() |
| 107 | + try: |
| 108 | + _emit_span() |
| 109 | + if import_checker.is_orjson_installed: |
| 110 | + _check_orjson_serializer() |
| 111 | + finally: |
| 112 | + bootstrapper.teardown() |
| 113 | + |
| 114 | + |
| 115 | +def _fastapi() -> None: |
| 116 | + bootstrapper = FastAPIBootstrapper( |
| 117 | + bootstrap_config=FastAPIConfig( |
| 118 | + service_name="floor-smoke", |
| 119 | + service_version="1.0.0", |
| 120 | + service_debug=False, |
| 121 | + logging_buffer_capacity=0, |
| 122 | + cors_allowed_origins=["http://test"], |
| 123 | + health_checks_path=HEALTH_PATH, |
| 124 | + health_checks_include_in_schema=True, |
| 125 | + swagger_offline_docs=True, |
| 126 | + sentry_dsn=SENTRY_DSN, |
| 127 | + sentry_additional_params=SENTRY_PARAMS, |
| 128 | + pyroscope_endpoint=PYROSCOPE_ENDPOINT, |
| 129 | + opentelemetry_endpoint=OTLP_ENDPOINT, |
| 130 | + opentelemetry_log_traces=True, |
| 131 | + ) |
| 132 | + ) |
| 133 | + application = bootstrapper.bootstrap() |
| 134 | + # Generating the schema puts HealthCheckTypedDict through pydantic as a response model, |
| 135 | + # which is the typing-extensions use ADR-0007 records. |
| 136 | + assert application.openapi()["paths"] |
| 137 | + assert application.url_path_for("health_check_handler") == HEALTH_PATH |
| 138 | + _emit_span() |
| 139 | + # Teardown runs inside the lifespan the bootstrapper chained onto the application. |
| 140 | + asyncio.run(_drive_lifespan(application)) |
| 141 | + |
| 142 | + |
| 143 | +def _litestar() -> None: |
| 144 | + bootstrapper = LitestarBootstrapper( |
| 145 | + bootstrap_config=LitestarConfig( |
| 146 | + service_name="floor-smoke", |
| 147 | + service_version="1.0.0", |
| 148 | + service_debug=False, |
| 149 | + logging_buffer_capacity=0, |
| 150 | + cors_allowed_origins=["http://test"], |
| 151 | + health_checks_path=HEALTH_PATH, |
| 152 | + sentry_dsn=SENTRY_DSN, |
| 153 | + sentry_additional_params=SENTRY_PARAMS, |
| 154 | + pyroscope_endpoint=PYROSCOPE_ENDPOINT, |
| 155 | + opentelemetry_endpoint=OTLP_ENDPOINT, |
| 156 | + opentelemetry_log_traces=True, |
| 157 | + ) |
| 158 | + ) |
| 159 | + bootstrapper.bootstrap() |
| 160 | + try: |
| 161 | + _emit_span() |
| 162 | + finally: |
| 163 | + bootstrapper.teardown() |
| 164 | + |
| 165 | + |
| 166 | +def _faststream() -> None: |
| 167 | + bootstrapper = FastStreamBootstrapper( |
| 168 | + bootstrap_config=FastStreamConfig( |
| 169 | + service_name="floor-smoke", |
| 170 | + service_version="1.0.0", |
| 171 | + logging_buffer_capacity=0, |
| 172 | + health_checks_path=HEALTH_PATH, |
| 173 | + sentry_dsn=SENTRY_DSN, |
| 174 | + sentry_additional_params=SENTRY_PARAMS, |
| 175 | + pyroscope_endpoint=PYROSCOPE_ENDPOINT, |
| 176 | + opentelemetry_endpoint=OTLP_ENDPOINT, |
| 177 | + opentelemetry_log_traces=True, |
| 178 | + ) |
| 179 | + ) |
| 180 | + bootstrapper.bootstrap() |
| 181 | + try: |
| 182 | + _emit_span() |
| 183 | + finally: |
| 184 | + bootstrapper.teardown() |
| 185 | + |
| 186 | + |
| 187 | +def _fastmcp() -> None: |
| 188 | + bootstrapper = FastMcpBootstrapper( |
| 189 | + bootstrap_config=FastMcpConfig( |
| 190 | + service_name="floor-smoke", |
| 191 | + service_version="1.0.0", |
| 192 | + logging_buffer_capacity=0, |
| 193 | + health_checks_path=HEALTH_PATH, |
| 194 | + sentry_dsn=SENTRY_DSN, |
| 195 | + sentry_additional_params=SENTRY_PARAMS, |
| 196 | + pyroscope_endpoint=PYROSCOPE_ENDPOINT, |
| 197 | + ) |
| 198 | + ) |
| 199 | + bootstrapper.bootstrap() |
| 200 | + bootstrapper.teardown() |
| 201 | + |
| 202 | + |
| 203 | +TARGETS: typing.Final = { |
| 204 | + "free": _free, |
| 205 | + "fastapi": _fastapi, |
| 206 | + "litestar": _litestar, |
| 207 | + "faststream": _faststream, |
| 208 | + "fastmcp": _fastmcp, |
| 209 | +} |
| 210 | + |
| 211 | + |
| 212 | +def main() -> None: |
| 213 | + expected_argv_len = 2 |
| 214 | + if len(sys.argv) != expected_argv_len or sys.argv[1] not in TARGETS: |
| 215 | + sys.exit(f"usage: floor_smoke.py {{{'|'.join(TARGETS)}}}") |
| 216 | + warnings.filterwarnings("error", category=InstrumentSkippedWarning) |
| 217 | + target = sys.argv[1] |
| 218 | + TARGETS[target]() |
| 219 | + print(f"floor smoke OK: {target} on {sys.version}") # noqa: T201 |
| 220 | + |
| 221 | + |
| 222 | +if __name__ == "__main__": |
| 223 | + main() |
0 commit comments