henry3260 commented on code in PR #64523: URL: https://github.com/apache/airflow/pull/64523#discussion_r3879705435
########## airflow-core/src/airflow/api_fastapi/common/http_metrics.py: ########## @@ -0,0 +1,149 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""HTTP API metrics middleware.""" + +from __future__ import annotations + +import time +from typing import TYPE_CHECKING + +import structlog + +from airflow._shared.observability.metrics.stats import Stats + +if TYPE_CHECKING: + from starlette.types import ASGIApp, Message, Receive, Scope, Send + +logger = structlog.get_logger(logger_name="http.metrics") + +_ROUTE_PATHS_BY_ROUTER_ID: dict[int, dict[object, str]] = {} +_API_METRICS_PATH_PREFIXES = ("/api/v2", "/ui") + + +def _is_api_metrics_path(path: str) -> bool: + return any(path == prefix or path.startswith(f"{prefix}/") for prefix in _API_METRICS_PATH_PREFIXES) + + +def _get_status_family(status_code: int) -> str: + return f"{status_code // 100}xx" + + +def _get_route_template(scope: Scope) -> str: + route = scope.get("route") Review Comment: > Heads up — I think the tests and production take different branches here. FastAPI sets `scope["route"]`, so in the real app this returns on the `scope.get("route")` line and the `router.routes` fallback below never runs. But `test_http_metrics.py` builds a plain Starlette app, which doesn't set `scope["route"]` — so the tests only ever hit the fallback, and the path that actually runs in production is untested. Could we build the test app with FastAPI (or add an integration test against the core_api app) so route extraction is covered as it really behaves? And if `scope["route"]` is always set under FastAPI, the fallback + `_ROUTE_PATHS_BY_ROUTER_ID` cache may be removable. Removed the fallback. `scope["route"]` is populated by FastAPI's `APIRoute.matches()`, and every route under `/api/v2` and `/ui` is an `APIRoute` — they're registered with `include_router` on the same app the middleware wraps (151 routes, all `APIRoute`). So the fallback was unreachable: when a route matches, the first branch returns; when nothing matches (404, `redirect_slashes`), `scope["endpoint"]` is absent too and we fall through to `"unmatched"` either way. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
