henry3260 commented on code in PR #64523: URL: https://github.com/apache/airflow/pull/64523#discussion_r3380848623
########## airflow-core/src/airflow/api_fastapi/common/http_metrics.py: ########## @@ -0,0 +1,168 @@ +# 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 +from airflow.api_fastapi.common.http_paths import HEALTH_PATHS + +if TYPE_CHECKING: + from starlette.types import ASGIApp, Message, Receive, Scope, Send + +logger = structlog.get_logger(logger_name="http.metrics") + +_API_PATH_PREFIX_TO_SURFACE = ( + ("/api/v2", "public"), + ("/ui", "ui"), +) +_ROUTE_PATHS_BY_ROUTER_ID: dict[int, dict[object, str]] = {} Review Comment: > I just learned from claude that this is a ideal case for using `WeakKeyDictionary`. > > More context: > > ```python > from weakref import WeakKeyDictionary > > _ROUTE_PATHS_BY_ROUTER: WeakKeyDictionary = WeakKeyDictionary() > > def _route_paths_for(router) -> dict[object, str]: > cached = _ROUTE_PATHS_BY_ROUTER.get(router) > if cached is None: > cached = { > ep: route.path > for route in getattr(router, "routes", ()) > if (ep := getattr(route, "endpoint", None)) is not None > and isinstance(getattr(route, "path", None), str) > and route.path > } > _ROUTE_PATHS_BY_ROUTER[router] = cached > return cached > ``` > > Why this is better than id(router): > > * id() is recycled after an object is GC'd, so a stale entry could be returned for a different router that happens to land at the same address. A weak key holds the actual object > identity, so that confusion can't happen. > * A plain dict[int, ...] keyed by id() never drops entries — it grows one per app instance forever (negligible in a long-lived server, but it accumulates across the many apps created > in the test suite). WeakKeyDictionary drops the entry automatically when the router dies. That's a good call. Unfortunately, `WeakKeyDictionary `cannot be used directly here because Starlette Router instances are unhashable, resulting in `TypeError: unhashable type: 'Router'`. -- 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]
