Issue
I have a python service that I am trying to add opentelemetry to. I am manually tracking user requests and subtasks and this seems to work ok: I am able to look at the spans in jaeger.
However, my system may decide to start a background asyncio task in the middle of a request. The request will end immediately, but the background task will go on forever. If I track code called by this task as spans, they end up being children of the request's span, although the request already ended. I'd rather prefer to show these spans as top level, like not children of any parent.
The documentation seems not to address this specific case, so I am trying to find the way
This is a simplified version of what I am attempting:
from opentelemetry import trace
from opentelemetry import context
tracer = trace.get_tracer(__name__)
ROOT_CONTEXT = context.get_current()
tasks = set()
async def background_task(some_req_info):
with tracer.start_as_current_span("background task", context=ROOT_CONTEXT):
# do stuff
...
async def request(some_req_info):
with tracer.start_as_current_span("request"):
# more logic
task = asyncio.create_task(background_task(some_req_info))
tasks.add(task)
task.add_done_callback(tasks.discard)
How can I achieve parent-less spans in background tasks?
Solution
OK, so I was actually measuring the wrong thing in jaeger. The code I posted works as expected. I am leaving this question here to save hours to the next developer wanting to solve this particular problem.
Answered By - chaos.ct
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.