Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

7.3 KB
Raw Blame
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""HTTP request profiling middleware helpers."""

from __future__ import annotations

import cProfile
import json
import site
import sysconfig
from pathlib import Path

from fastapi import Request
from fastapi.responses import JSONResponse
from starlette.datastructures import Headers, MutableHeaders
from starlette.types import ASGIApp, Message, Receive, Scope, Send

PROFILE_TRUE_VALUES = {"1", "true", "yes", "on"}
PROFILE_SORT_BY = "cumulative"
PROFILE_TOP_N = 100
PROFILE_MAX_CHARS = 16 * 1024
_PROJECT_ROOT = Path(__file__).resolve().parents[2]


def _build_profile_roots() -> tuple[Path, ...]:
    roots: list[Path] = [_PROJECT_ROOT]

    try:
        roots.extend(Path(path) for path in site.getsitepackages())
    except Exception:
        pass

    try:
        user_site = site.getusersitepackages()
        if user_site:
            roots.append(Path(user_site))
    except Exception:
        pass

    for key in ("stdlib", "platstdlib"):
        try:
            stdlib_path = sysconfig.get_paths().get(key)
            if stdlib_path:
                roots.append(Path(stdlib_path))
        except Exception:
            pass

    deduped: list[Path] = []
    seen: set[str] = set()
    for root in roots:
        raw = root.as_posix()
        resolved = root.resolve().as_posix()
        for candidate in (raw, resolved):
            if candidate and candidate not in seen:
                deduped.append(Path(candidate))
                seen.add(candidate)
    return tuple(deduped)


PROFILE_ROOTS = _build_profile_roots()


def profile_enabled(request: Request) -> bool:
    server_config = getattr(request.app.state, "config", None)
    if server_config is not None and not getattr(server_config, "profile_enabled", False):
        return False
    value = request.query_params.get("profile")
    if value is None:
        return False
    return value.strip().lower() in PROFILE_TRUE_VALUES


def _infer_module_suffix(path: Path) -> str:
    parts = list(path.parts)
    if not parts:
        return path.name

    suffix = [path.name]
    for part in reversed(parts[:-1]):
        if part.isidentifier():
            suffix.insert(0, part)
            continue
        break

    if len(suffix) >= 2:
        return "/".join(suffix)
    if len(parts) >= 2:
        return "/".join(parts[-2:])
    return path.name


def _sanitize_profile_path(path: str) -> str:
    raw_path = Path(path)
    candidates = [raw_path]
    try:
        resolved = raw_path.resolve()
    except Exception:
        resolved = None
    if resolved is not None and resolved != raw_path:
        candidates.append(resolved)

    for candidate in candidates:
        parts = candidate.parts

        for marker in ("site-packages", "dist-packages"):
            if marker in parts:
                idx = parts.index(marker)
                if idx + 1 < len(parts):
                    return "/".join(parts[idx + 1 :])

        for idx, part in enumerate(parts):
            if part.startswith("python") and part[6:7].isdigit() and idx + 1 < len(parts):
                suffix = parts[idx + 1 :]
                if suffix:
                    return "/".join(suffix)

        for root in PROFILE_ROOTS:
            try:
                return candidate.relative_to(root).as_posix()
            except ValueError:
                continue

    return _infer_module_suffix(raw_path)


def format_profile_output(profiler: cProfile.Profile) -> list[str]:
    import pstats

    stats = pstats.Stats(profiler)
    stats.sort_stats(PROFILE_SORT_BY)

    rows = list(stats.stats.items())
    if PROFILE_SORT_BY == "cumulative":
        rows.sort(key=lambda item: (item[1][3], item[1][2]), reverse=True)
    else:
        rows.sort(key=lambda item: item[1][2], reverse=True)

    total_calls = getattr(stats, "total_calls", 0)
    prim_calls = getattr(stats, "prim_calls", 0)
    total_tt = getattr(stats, "total_tt", 0.0)

    lines = [
        f"         {total_calls} function calls ({prim_calls} primitive calls) in {total_tt:.3f} seconds",
        "",
        f"   Ordered by: {PROFILE_SORT_BY} time",
        f"   List reduced from {len(rows)} to {min(len(rows), PROFILE_TOP_N)} due to restriction <{PROFILE_TOP_N}>",
        "",
        "   ncalls  tottime  percall  cumtime  percall filename:lineno(function)",
    ]

    for (filename, lineno, funcname), stat in rows[:PROFILE_TOP_N]:
        cc, nc, tt, ct, _callers = stat
        ncalls = f"{cc}/{nc}" if cc != nc else str(nc)
        percall_tt = tt / nc if nc else 0.0
        percall_ct = ct / cc if cc else 0.0
        location = f"{_sanitize_profile_path(filename)}:{lineno}({funcname})"
        lines.append(
            f"{ncalls:>9} {tt:>8.3f} {percall_tt:>8.3f} {ct:>8.3f} {percall_ct:>8.3f} {location}"
        )

    profile_text = "\n".join(lines)
    if len(profile_text) <= PROFILE_MAX_CHARS:
        return lines

    truncated = profile_text[: PROFILE_MAX_CHARS - len("\n... [truncated]")] + "\n... [truncated]"
    return truncated.splitlines()


class ProfileMiddleware:
    """Profile response generation and annotate ordinary JSON responses."""

    def __init__(self, app: ASGIApp) -> None:
        self.app = app

    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        if scope["type"] != "http" or not profile_enabled(Request(scope)):
            await self.app(scope, receive, send)
            return

        profiler = cProfile.Profile()
        response_start: Message | None = None

        async def send_profiled(message: Message) -> None:
            nonlocal response_start
            if message["type"] == "http.response.start":
                profiler.disable()
                headers = Headers(raw=message.get("headers", []))
                content_type = headers.get("content-type", "").lower()
                # JSONResponse sends a sized body in one message. StreamingResponse
                # omits its length; FileResponse advertises byte ranges/downloads.
                if (
                    scope["method"] != "HEAD"
                    and ("application/json" in content_type or "+json" in content_type)
                    and "content-length" in headers
                    and "content-disposition" not in headers
                    and "accept-ranges" not in headers
                ):
                    response_start = message
                    return
            elif response_start is not None:
                if message["type"] == "http.response.body" and not message.get("more_body", False):
                    try:
                        payload = json.loads(message.get("body", b""))
                    except (TypeError, ValueError):
                        payload = None
                    if isinstance(payload, dict):
                        payload["profile"] = format_profile_output(profiler)
                        body = JSONResponse(payload).body
                        MutableHeaders(scope=response_start)["content-length"] = str(len(body))
                        message = {**message, "body": body}
                # A chunked body or pathsend is passed through without buffering.
                await send(response_start)
                response_start = None
            await send(message)

        profiler.enable()
        try:
            await self.app(scope, receive, send_profiled)
        finally:
            profiler.disable()