From 86f617a197f208f35f32e3ecead6754fb1a1c7a2 Mon Sep 17 00:00:00 2001 From: Dinesh Yeduguru Date: Thu, 20 Mar 2025 14:22:19 -0700 Subject: [PATCH] fix: tracing middleware to not start for lifespan events (#1730) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # What does this PR do? Tracing middleware should not start tracing for lifespan events. Lifespan event happens at server startup and shutdown and if we start tracing for them, we will have an active trace for the lifetime of the server, which messes up with regular tracing since we always expect the traces to be never nested. We started hitting this issue since https://github.com/meta-llama/llama-stack/pull/1495. ## Test Plan * llama stack run ~/.llama/distributions/fireworks/fireworks-run.yaml * Verify in sqlite store that the trace now has non null span id ![Screenshot 2025-03-20 at 1 49 47 PM](https://github.com/user-attachments/assets/d77354a7-d5f1-4b53-a946-6adbd7a4f772) --- llama_stack/distribution/server/server.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/llama_stack/distribution/server/server.py b/llama_stack/distribution/server/server.py index 3bdeeef7c..dea56b1b2 100644 --- a/llama_stack/distribution/server/server.py +++ b/llama_stack/distribution/server/server.py @@ -226,6 +226,8 @@ class TracingMiddleware: self.app = app async def __call__(self, scope, receive, send): + if scope.get("type") == "lifespan": + return await self.app(scope, receive, send) path = scope.get("path", "") await start_trace(path, {"__location__": "server"}) try: