Closed pablo-meier closed 2 years ago
We do something similar on our code base that's working. Here's some pseudo code. Hope that helps
defp trace_aware_async(span_label, func) do
current_context = Tracer.current_context()
Task.async(fn ->
case current_context do
{:ok, context} ->
Tracer.continue_trace(span_label, context)
try do
func.()
after
Tracer.finish_trace()
end
{:error, _reason} ->
func.()
end
end)
end
I super appreciate this, the issue was forgetting to put Tracer.finish_trace()
. Thanks so much for pasting your example π @san650
Hi! Wrote it up over at ElixirForum, but maybe I could get more specialized π here? Appreciate any help if you have any π
My company is using Datadog APM to profile traces for our Phoenix app, and the Spandex family has been great for sending traces π
The trouble is that we lose spans on our traces when we use
Task.async
andTask.await
for multiple parallel DB calls. In code, it initially looked something like this:Here, the first DB call (outside the Task.async/await) traces correctly, and we can see how long it takes in the context of the toplevel request. But the ones in the other processes donβt trace correctly.
Now! I saw this tip from the Spandex README:
So I did something like this:
But still no dice :weary:
I started digging into the sources and may continue to do this, and this person seems to have printed logs to find/correct discrepancies in the trace_id, but Iβd rather not trouble my teammates with multiple PR reviews and monitor so many deploys. Do you have an idea what Iβm doing wrong?
Thanks so much! :raised_hands: