fix: tracing middleware to not start for lifespan events (#1730)

# 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)
This commit is contained in:
Dinesh Yeduguru 2025-03-20 14:22:19 -07:00 committed by GitHub
parent 029e4fc64d
commit 86f617a197
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -226,6 +226,8 @@ class TracingMiddleware:
self.app = app self.app = app
async def __call__(self, scope, receive, send): async def __call__(self, scope, receive, send):
if scope.get("type") == "lifespan":
return await self.app(scope, receive, send)
path = scope.get("path", "") path = scope.get("path", "")
await start_trace(path, {"__location__": "server"}) await start_trace(path, {"__location__": "server"})
try: try: