Skip to content

Commit da1aa2e

Browse files
committed
Add span decorator example in real world
1 parent 33cb701 commit da1aa2e

File tree

1 file changed

+19
-0
lines changed
  • examples/real_world/app/routes

1 file changed

+19
-0
lines changed

examples/real_world/app/routes/slow.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import asyncio
22
import logging
3+
import random
34

45
from fastapi import APIRouter, status
56
from opentelemetry import trace
67

8+
from asgi_monitor.tracing import span
9+
710
slow_router = APIRouter(
811
prefix="/slow",
912
tags=["Slow"],
@@ -28,3 +31,19 @@ async def get_1000ms() -> dict:
2831
await asyncio.sleep(0.4)
2932
logger.info("full energy")
3033
return {"message": "ok", "status": "success"}
34+
35+
36+
@span
37+
def nested_func() -> int:
38+
num = random.randint(1, 10) # noqa: S311
39+
current_span = trace.get_current_span()
40+
current_span.set_attribute("num", num)
41+
current_span.add_event("num rendered")
42+
return num
43+
44+
45+
@slow_router.get("/span", status_code=status.HTTP_200_OK)
46+
@span(name="span handler", attributes={"foo": "bar"})
47+
async def get_span() -> dict:
48+
num = nested_func()
49+
return {"message": "ok", "status": "success", "num": num}

0 commit comments

Comments
 (0)