|
25 | 25 | import httpx |
26 | 26 | import rich.pretty |
27 | 27 | import yaml |
28 | | -from aiohttp import hdrs |
29 | 28 | from fastapi import Body, FastAPI, HTTPException, Request, Response |
30 | 29 | from fastapi import Path as FastapiPath |
31 | 30 | from fastapi.exceptions import RequestValidationError |
|
45 | 44 | process_cors_config, |
46 | 45 | ) |
47 | 46 | from llama_stack.core.distribution import builtin_automatically_routed_apis |
48 | | -from llama_stack.core.external import ExternalApiSpec, load_external_apis |
| 47 | +from llama_stack.core.external import load_external_apis |
49 | 48 | from llama_stack.core.request_headers import ( |
50 | 49 | PROVIDER_DATA_VAR, |
51 | 50 | request_provider_data_context, |
52 | 51 | user_from_scope, |
53 | 52 | ) |
54 | | -from llama_stack.core.server.routes import ( |
55 | | - find_matching_route, |
56 | | - get_all_api_routes, |
57 | | - initialize_route_impls, |
58 | | -) |
| 53 | +from llama_stack.core.server.routes import get_all_api_routes |
59 | 54 | from llama_stack.core.stack import ( |
60 | 55 | Stack, |
61 | 56 | cast_image_name_to_string, |
|
73 | 68 | ) |
74 | 69 | from llama_stack.providers.utils.telemetry.tracing import ( |
75 | 70 | CURRENT_TRACE_CONTEXT, |
76 | | - end_trace, |
77 | 71 | setup_logger, |
78 | | - start_trace, |
79 | 72 | ) |
80 | 73 |
|
81 | 74 | from .auth import AuthenticationMiddleware |
82 | 75 | from .quota import QuotaMiddleware |
| 76 | +from .tracing import TracingMiddleware |
83 | 77 |
|
84 | 78 | REPO_ROOT = Path(__file__).parent.parent.parent.parent |
85 | 79 |
|
@@ -299,65 +293,6 @@ async def route_handler(request: Request, **kwargs): |
299 | 293 | return route_handler |
300 | 294 |
|
301 | 295 |
|
302 | | -class TracingMiddleware: |
303 | | - def __init__(self, app, impls, external_apis: dict[str, ExternalApiSpec]): |
304 | | - self.app = app |
305 | | - self.impls = impls |
306 | | - self.external_apis = external_apis |
307 | | - # FastAPI built-in paths that should bypass custom routing |
308 | | - self.fastapi_paths = ("/docs", "/redoc", "/openapi.json", "/favicon.ico", "/static") |
309 | | - |
310 | | - async def __call__(self, scope, receive, send): |
311 | | - if scope.get("type") == "lifespan": |
312 | | - return await self.app(scope, receive, send) |
313 | | - |
314 | | - path = scope.get("path", "") |
315 | | - |
316 | | - # Check if the path is a FastAPI built-in path |
317 | | - if path.startswith(self.fastapi_paths): |
318 | | - # Pass through to FastAPI's built-in handlers |
319 | | - logger.debug(f"Bypassing custom routing for FastAPI built-in path: {path}") |
320 | | - return await self.app(scope, receive, send) |
321 | | - |
322 | | - if not hasattr(self, "route_impls"): |
323 | | - self.route_impls = initialize_route_impls(self.impls, self.external_apis) |
324 | | - |
325 | | - try: |
326 | | - _, _, route_path, webmethod = find_matching_route( |
327 | | - scope.get("method", hdrs.METH_GET), path, self.route_impls |
328 | | - ) |
329 | | - except ValueError: |
330 | | - # If no matching endpoint is found, pass through to FastAPI |
331 | | - logger.debug(f"No matching route found for path: {path}, falling back to FastAPI") |
332 | | - return await self.app(scope, receive, send) |
333 | | - |
334 | | - trace_attributes = {"__location__": "server", "raw_path": path} |
335 | | - |
336 | | - # Extract W3C trace context headers and store as trace attributes |
337 | | - headers = dict(scope.get("headers", [])) |
338 | | - traceparent = headers.get(b"traceparent", b"").decode() |
339 | | - if traceparent: |
340 | | - trace_attributes["traceparent"] = traceparent |
341 | | - tracestate = headers.get(b"tracestate", b"").decode() |
342 | | - if tracestate: |
343 | | - trace_attributes["tracestate"] = tracestate |
344 | | - |
345 | | - trace_path = webmethod.descriptive_name or route_path |
346 | | - trace_context = await start_trace(trace_path, trace_attributes) |
347 | | - |
348 | | - async def send_with_trace_id(message): |
349 | | - if message["type"] == "http.response.start": |
350 | | - headers = message.get("headers", []) |
351 | | - headers.append([b"x-trace-id", str(trace_context.trace_id).encode()]) |
352 | | - message["headers"] = headers |
353 | | - await send(message) |
354 | | - |
355 | | - try: |
356 | | - return await self.app(scope, receive, send_with_trace_id) |
357 | | - finally: |
358 | | - await end_trace() |
359 | | - |
360 | | - |
361 | 296 | class ClientVersionMiddleware: |
362 | 297 | def __init__(self, app): |
363 | 298 | self.app = app |
|
0 commit comments