Skip to content

Commit b70a439

Browse files
Allow FastAPI 0.137+ by requiring cadwyn 8.1 (#75689)
* Allow FastAPI 0.137+ by requiring cadwyn 8.1 FastAPI was capped below 0.137 because its lazy router inclusion broke cadwyn 7's versioned router generation. cadwyn 8.1 supports it, so lift the cap and raise the cadwyn minimum. cadwyn 8.1 itself requires fastapi>=0.137.1; raise our floor to 0.137.2, the first release with fastapi.routing.iter_route_contexts, which the test helper relies on. Fix what the newer FastAPI exposed: use pydantic's Discriminator for the TIStateUpdate union, make Execution API trace-context injection reach routes inside included routers (it was silently a no-op), and update tests that relied on the removed get_flat_dependant or on flattened router.routes. related: #68562 * Stop FastAPI's native telemetry and lazy routers changing API behavior FastAPI 0.142 added native OpenTelemetry that is on by default and runs before routing and auth. With a tracer provider configured it extracted caller trace context on unauthenticated requests and exported query strings unredacted, bypassing [execution_api] otel_trace_propagation. Exclude every request on the apps Airflow builds, which also stops the apps mounted under them from tracing, and raise the FastAPI floor to 0.142.0, the first release with that option. Lazy router inclusion also broke code reading app.routes: HTTP metric route tags lost their router prefixes, the private UI OpenAPI spec regenerated without its /ui routes, and the edge3 worker API route tests saw no routes. related: #68562 * Detect Execution API auth from enclosing routers and drop cadwyn changelog Trace-context extraction in only-authenticated mode now checks each route's merged dependencies, so authentication added by an enclosing router or an include_router() call still counts. Walking routes through FastAPI's public iter_route_contexts also avoids relying on its private included-router type. Cadwyn 8.1 deprecates the changelog endpoint and warns whenever it is enabled; the Execution API never used it.
1 parent df03024 commit b70a439

20 files changed

Lines changed: 249 additions & 77 deletions

File tree

‎airflow-core/pyproject.toml‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ dependencies = [
8181
"asgiref>=2.3.0; python_version < '3.14'",
8282
"asgiref>=3.11.1; python_version >= '3.14'",
8383
"attrs>=22.1.0, !=25.2.0",
84-
"cadwyn>=6.1.1",
84+
"cadwyn>=8.1.0",
8585
"colorlog>=6.8.2",
8686
"cron-descriptor>=1.2.24",
8787
"croniter>=6.2.2",
@@ -92,11 +92,8 @@ dependencies = [
9292
"cryptography>=44.0.3",
9393
"deprecated>=1.2.13",
9494
"dill>=0.2.2",
95-
# Cap below 0.137.0: FastAPI 0.137 switched to lazy router inclusion, which breaks cadwyn's
96-
# versioned router generation (RouterGenerationError) and fails api-server / dag-processor
97-
# startup. Relax once cadwyn supports FastAPI 0.137. See
98-
# https://github.com/apache/airflow/issues/68562
99-
"fastapi[standard-no-fastapi-cloud-cli]>=0.129.0,<0.137.0",
95+
# 0.142.0 is the first release with the ``telemetry`` option used to turn off FastAPI's native OpenTelemetry.
96+
"fastapi[standard-no-fastapi-cloud-cli]>=0.142.0",
10097
"uvicorn>=0.37.0",
10198
# 1.3.1 is the first release whose urlencoded FormParser enforces max_fields/max_part_size
10299
# and reports them as a 400. Below it, an unauthenticated form body to POST /auth/token is

‎airflow-core/src/airflow/api_fastapi/app.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from airflow.api_fastapi.common.exceptions import init_error_handlers
3333
from airflow.api_fastapi.common.http_access_log import HttpAccessLogMiddleware
3434
from airflow.api_fastapi.common.http_metrics import HttpMetricsMiddleware
35+
from airflow.api_fastapi.common.telemetry import NATIVE_TELEMETRY_DISABLED
3536
from airflow.api_fastapi.core_api.app import (
3637
init_config,
3738
init_flask_plugins,
@@ -154,6 +155,7 @@ def create_app(apps: str = "all") -> FastAPI:
154155
"supported by these parameters; regex-capable endpoints expose a separate parameter.",
155156
lifespan=lifespan,
156157
root_path=API_ROOT_PATH.removesuffix("/"),
158+
telemetry=NATIVE_TELEMETRY_DISABLED,
157159
version="2",
158160
docs_url="/docs" if conf.getboolean("api", "enable_swagger_ui") else None,
159161
redoc_url="/redoc" if conf.getboolean("api", "enable_swagger_ui") else None,

‎airflow-core/src/airflow/api_fastapi/common/http_metrics.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
from __future__ import annotations
2020

2121
import time
22-
from typing import TYPE_CHECKING
22+
from typing import TYPE_CHECKING, Any
23+
from weakref import WeakKeyDictionary
2324

2425
import structlog
26+
from fastapi.routing import iter_route_contexts
2527

2628
from airflow._shared.observability.metrics.stats import Stats
2729

@@ -30,6 +32,8 @@
3032

3133
logger = structlog.get_logger(logger_name="http.metrics")
3234

35+
_ROUTE_PATHS: WeakKeyDictionary[Any, dict[int, str]] = WeakKeyDictionary()
36+
3337
# `/execution` is the Task SDK execution API, mounted as a sub-app. The middleware runs on the
3438
# outer app, so a request to it passes through here like any other.
3539
_API_METRICS_PATH_PREFIXES = ("/api/v2", "/ui", "/execution")
@@ -51,11 +55,28 @@ def _get_method_tag(method: str) -> str:
5155
return method if method in _SERVED_HTTP_METHODS else _OTHER_HTTP_METHOD
5256

5357

58+
def _get_route_paths(app: Any) -> dict[int, str]:
59+
"""Return the full path of each route in ``app``, keyed by ``id()`` of the route object."""
60+
paths = _ROUTE_PATHS.get(app)
61+
if paths is None:
62+
paths = {}
63+
for context in iter_route_contexts(app.routes):
64+
if context.path:
65+
paths.setdefault(id(context.original_route), context.path)
66+
_ROUTE_PATHS[app] = paths
67+
return paths
68+
69+
5470
def _get_route_template(scope: Scope) -> str:
5571
# FastAPI's APIRoute.matches() stores the matched route in the scope before the endpoint runs.
5672
# Requests matching no route (404s, redirects) share one bucket so tag cardinality stays bounded.
57-
route_path = getattr(scope.get("route"), "path", None)
73+
route = scope.get("route")
74+
route_path = getattr(route, "path", None)
5875
if isinstance(route_path, str) and route_path:
76+
# The route in the scope is the one declared on its own router, so its path lacks the
77+
# prefixes of the routers it is included through (`/dags` rather than `/api/v2/dags`).
78+
if (app := scope.get("app")) is not None:
79+
route_path = _get_route_paths(app).get(id(route), route_path)
5980
# A mounted sub-app declares its routes relative to the mount point, which Starlette keeps
6081
# in root_path. Without it, `/execution/task-instances/{id}` would be tagged
6182
# `/task-instances/{id}` and collide with core routes sharing that template.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from __future__ import annotations
18+
19+
from typing import TYPE_CHECKING, Any
20+
21+
if TYPE_CHECKING:
22+
from collections.abc import MutableMapping
23+
24+
from fastapi.telemetry import TelemetryConfig
25+
26+
27+
def _exclude_every_request(scope: MutableMapping[str, Any]) -> bool:
28+
return True
29+
30+
31+
# Airflow controls request tracing itself (``[execution_api] otel_trace_propagation``, query
32+
# string redaction), so FastAPI's native OpenTelemetry must stay out of the way. Excluding every
33+
# request, rather than turning the signals off, also keeps the FastAPI apps mounted underneath
34+
# (auth managers, plugins) from tracing it.
35+
NATIVE_TELEMETRY_DISABLED: TelemetryConfig = {
36+
"exclude": _exclude_every_request,
37+
"auto_configure": False,
38+
}

‎airflow-core/src/airflow/api_fastapi/execution_api/app.py‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
)
3131
from fastapi import Depends, FastAPI, Request, Response
3232
from fastapi.responses import JSONResponse
33-
from fastapi.routing import APIRoute
33+
from fastapi.routing import APIRoute, iter_route_contexts
3434
from opentelemetry import context as otel_context, propagate as otel_propagate
3535
from starlette.middleware.base import BaseHTTPMiddleware
3636

@@ -40,8 +40,9 @@
4040
get_sig_validation_args,
4141
get_signing_args,
4242
)
43+
from airflow.api_fastapi.common.telemetry import NATIVE_TELEMETRY_DISABLED
4344
from airflow.api_fastapi.execution_api.in_process import InProcessExecutionAPI
44-
from airflow.api_fastapi.execution_api.security import _REQUEST_SCOPE_TOKEN_KEY
45+
from airflow.api_fastapi.execution_api.security import _REQUEST_SCOPE_TOKEN_KEY, require_auth
4546

4647
if TYPE_CHECKING:
4748
from airflow.models.dagbag import DBDagBag
@@ -265,25 +266,30 @@ async def _extract_w3c_trace_context(
265266
otel_context.detach(token)
266267

267268

269+
def _is_trace_context_dep(dependency) -> bool:
270+
return getattr(dependency, "dependency", None) is _extract_w3c_trace_context
271+
272+
268273
def _inject_trace_context_dep(routes, mode: str) -> None:
269-
dep = Depends(_extract_w3c_trace_context)
270-
for route in routes:
274+
"""
275+
Add trace-context extraction to the authenticated routes in ``only-authenticated`` mode.
276+
277+
``unsafe-always`` mode is handled with an app-level dependency instead, so extraction runs
278+
before authentication declared on any enclosing router.
279+
"""
280+
for context in iter_route_contexts(routes):
281+
route = context.original_route
271282
if not isinstance(route, APIRoute):
272283
continue
273284
# Idempotent: create_task_execution_api_app() runs more than once per process
274285
# (cached_app + InProcessExecutionAPI), and execution_api_router is shared
275286
# module state, so strip any prior injection first.
276-
route.dependencies[:] = [
277-
d for d in route.dependencies if getattr(d, "dependency", None) is not _extract_w3c_trace_context
278-
]
279-
match mode:
280-
case "unsafe-always":
281-
route.dependencies.insert(0, dep)
282-
case "only-authenticated":
283-
from airflow.api_fastapi.execution_api.security import require_auth
284-
285-
if any(getattr(d, "dependency", None) is require_auth for d in route.dependencies):
286-
route.dependencies.append(dep)
287+
route.dependencies[:] = [d for d in route.dependencies if not _is_trace_context_dep(d)]
288+
if mode != "only-authenticated":
289+
continue
290+
# context.dependencies also holds those of the enclosing routers and include_router() calls.
291+
if any(getattr(d, "dependency", None) is require_auth for d in context.dependencies):
292+
route.dependencies.append(Depends(_extract_w3c_trace_context))
287293

288294

289295
def create_task_execution_api_app(
@@ -308,6 +314,7 @@ def custom_generate_unique_id(route: APIRoute):
308314
return route.name
309315

310316
# See https://docs.cadwyn.dev/concepts/version_changes/ for info about API versions
317+
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
311318
app = CadwynWithOpenAPICustomization(
312319
title="Airflow Task Execution API",
313320
description="The private Airflow Task Execution API.",
@@ -316,13 +323,15 @@ def custom_generate_unique_id(route: APIRoute):
316323
api_version_parameter_name="Airflow-API-Version",
317324
api_version_default_value=bundle.versions[0].value,
318325
versions=bundle,
326+
changelog_url=None,
327+
dependencies=[Depends(_extract_w3c_trace_context)] if mode == "unsafe-always" else None,
328+
telemetry=NATIVE_TELEMETRY_DISABLED,
319329
)
320330

321331
# Add correlation-id middleware for request tracing
322332
app.add_middleware(CorrelationIdMiddleware)
323333
app.add_middleware(JWTReissueMiddleware)
324334

325-
mode = conf.get("execution_api", "otel_trace_propagation", fallback="only-authenticated")
326335
_inject_trace_context_dep(execution_api_router.routes, mode)
327336

328337
app.generate_and_include_versioned_routers(execution_api_router)

‎airflow-core/src/airflow/api_fastapi/execution_api/datamodels/taskinstance.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
from pydantic import (
2626
AwareDatetime,
27+
Discriminator,
2728
Field,
2829
JsonValue,
2930
Tag,
@@ -297,7 +298,7 @@ def ti_state_discriminator(v: dict[str, str] | StrictBaseModel) -> str:
297298
| Annotated[TIRescheduleStatePayload, Tag("up_for_reschedule")]
298299
| Annotated[TIAwaitingInputStatePayload, Tag("awaiting_input")]
299300
| Annotated[TIRetryStatePayload, Tag("up_for_retry")],
300-
Field(discriminator=ti_state_discriminator),
301+
Discriminator(ti_state_discriminator),
301302
]
302303

303304

‎airflow-core/src/airflow/api_fastapi/execution_api/security.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@
7878
to *relax* the outer router's default execution-only restriction, but
7979
``SecurityScopes`` only accumulate additively — an outer dependency
8080
cannot see scopes declared by inner ones. ``ExecutionAPIRoute`` solves
81-
this by inspecting the **merged** dependency list at route registration
82-
time (after ``include_router`` has combined all parent and child
83-
dependencies) and precomputing the full ``allowed_token_types`` set.
81+
this by inspecting the route's own dependency list at route registration
82+
time and precomputing the full ``allowed_token_types`` set.
8483
``require_auth`` then reads this precomputed set from the matched route
85-
at request time, avoiding the ordering problem entirely.
84+
at request time, avoiding the ordering problem entirely. FastAPI includes
85+
routers lazily, so ``token:*`` scopes must be declared on the route
86+
itself: scopes on an enclosing router or ``include_router`` call are not
87+
seen, and the route falls back to execution-only.
8688
8789
Any router whose routes need non-default token type policies must use
8890
``route_class=ExecutionAPIRoute``. Routers that only need the default

‎airflow-core/src/airflow/utils/serve_logs/log_server.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
from airflow._shared.module_loading import import_string
3737
from airflow.api_fastapi.auth.tokens import JWTValidator, get_signing_key
38+
from airflow.api_fastapi.common.telemetry import NATIVE_TELEMETRY_DISABLED
3839
from airflow.configuration import conf
3940
from airflow.utils.docs import get_docs_url
4041

@@ -140,7 +141,7 @@ def create_app():
140141
except Exception as e:
141142
raise ImportError(f"Unable to load {log_config_class} due to error: {e}")
142143

143-
fastapi_app = FastAPI()
144+
fastapi_app = FastAPI(telemetry=NATIVE_TELEMETRY_DISABLED)
144145
fastapi_app.state.signer = JWTValidator(
145146
issuer=None,
146147
secret_key=get_signing_key("api", "secret_key"),

‎airflow-core/tests/unit/api_fastapi/common/test_http_metrics.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import pytest
2323
import structlog.testing
24-
from fastapi import FastAPI
24+
from fastapi import APIRouter, FastAPI
2525
from starlette.responses import PlainTextResponse
2626
from starlette.testclient import TestClient
2727

@@ -76,6 +76,16 @@ async def api_fail():
7676
async def health():
7777
return PlainTextResponse("healthy")
7878

79+
dags_router = APIRouter(prefix="/dags")
80+
81+
@dags_router.get("/{dag_id}")
82+
async def included_dag(dag_id: str):
83+
return PlainTextResponse("ok")
84+
85+
public_router = APIRouter(prefix="/api/v2")
86+
public_router.include_router(dags_router)
87+
app.include_router(public_router)
88+
7989
app.mount("/execution", _make_execution_app())
8090
app.add_middleware(HttpMetricsMiddleware)
8191
return app
@@ -160,6 +170,7 @@ def test_request_duration_is_emitted_in_milliseconds():
160170
id="route-template-with-parameters",
161171
),
162172
pytest.param("/ui/items/42", "/ui/items/{item_id}", id="ui"),
173+
pytest.param("/api/v2/dags/example", "/api/v2/dags/{dag_id}", id="included-router-keeps-prefixes"),
163174
pytest.param(
164175
"/execution/task-instances/abc/run",
165176
"/execution/task-instances/{task_instance_id}/run",

‎airflow-core/tests/unit/api_fastapi/core_api/routes/public/test_assets.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
from airflow.utils.state import DagRunState, DagSchedulingState
6060
from airflow.utils.types import DagRunType
6161

62+
from tests_common.test_utils.api_fastapi import iter_api_routes
6263
from tests_common.test_utils.asserts import assert_queries_count
6364
from tests_common.test_utils.config import conf_vars
6465
from tests_common.test_utils.db import (
@@ -2062,7 +2063,9 @@ def _dag_axis_methods(route) -> list[str]:
20622063
@pytest.fixture
20632064
def routes_by_path(self, test_client):
20642065
return {
2065-
(r.path, tuple(sorted(r.methods))): r for r in test_client.app.routes if hasattr(r, "dependant")
2066+
(r.path, tuple(sorted(r.methods))): r
2067+
for r in iter_api_routes(test_client.app.routes)
2068+
if hasattr(r, "dependant")
20662069
}
20672070

20682071
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)