Setting a span's parent after creation (or, tracing aggregation operations) #2000
-
I'm looking for a little insight into how I could trace the following scenario -- please let me know if this kind of question is better asked elsewhere (e.g., gitter).
The trace diagram I'm looking to build would be something like this: I'm unclear on how to correctly set up spans for the aggregation portion... Aggregation works roughly like this:
async def aggregate(qin: asyncio.Queue[bytes], aggregator: Aggregator, qout: asyncio.Queue[bytes]):
while True:
payload = await qin.get()
report = deserialize(payload)
# schedule the handling of this report so we don't block this coroutine: the aggregator will fire
# after X seconds with whatever reports have arrived, we just need to keep shoving reports
# into the aggregator
asyncio.create_task(aggregator.handle_report(report), qout) The tricky bit is that I won't know what event a report corresponds to until I deserialize the message. What I'd need is some way to set the parent after deserialization. I'd imagine that might look something like: async def aggregate(qin: asyncio.Queue[bytes], aggregator: Aggregator, qout: asyncio.Queue[bytes]):
while True:
payload = await qin.get()
# start_unbound_span is an imaginary function that creates a span to which we can attach a
# parent later
with tracer.start_unbound_span("deserialize") as deserialize_span:
report = deserialize(payload)
# some custom function where we create a root-level `event` span -- this will be propagated to
# e.g., the `process` service
event_span = get_or_create_event_span_for(report.event_id)
# some custom function where we keep a list of active aggregation spans for explicit propagation
# between asyncio tasks -- we'll end this span after aggregation
aggregate_span = get_or_create_aggregate_span_for(report.event_id, parent=event_span)
deserialize_span.set_parent(aggregate_span)
# etc... but AFAIK there's no way to set a parent span after the fact. I'd love to hear if: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hello, sorry for the late reply If I am understanding your issue correctly, then this may be a solution: You can create a span, then pass it as an argument to this function, the returned context object can be used then as an argument here. |
Beta Was this translation helpful? Give feedback.
Hello, sorry for the late reply
If I am understanding your issue correctly, then this may be a solution:
You can create a span, then pass it as an argument to this function, the returned context object can be used then as an argument here.