spandex-project/spandex

[Q] how to use spandex with absinthe telemetry events?

Opened this issue · 3 comments

I can't figure it out tow to properly finish my spans:
The start events have start time and id, or end time and the same id. I'm saving the id when starting a span but then how to tell spandex which span to finish in the stop handler ??

I think the telemetry handlers are running in a separate process so it's not that easy. The only thing that comes to my mind is a ets table or an agent that tracks the span_id created by the start event and correlates it with the stop event but I was wondering if someone has a better solution for that - telemetry is the defacto standard now on the beam.

Something we've previously done at work, if running this across processes (e.g., in our case poolboy workers), was pass the span ID to the worker processes so they can keep track of it. Your solution with ets table should also work!

    # calling side, where you'll run poolboy.transaction/3
    lambda = wrapped_lambda(message, @timeout)

    result =
      try do
        :poolboy.transaction(pool, lambda, @timeout)
      catch
        ...
      end
# defining wrapped_lambda/2
  defp wrapped_lambda(message, timeout) do
    fn pid ->
      try do
        :telemetry.span(
          [:safety, :set_span],
          %{},
          fn ->
            result = GenServer.call(pid, {:set_span, Tracer.current_span(), message})
            {result, %{message: elem(message, 0)}}
          end
        )

        :telemetry.span(
          [:safety, :call_worker],
          %{},
          fn ->
            result =
              GenServer.call(pid, message, timeout)
              |> worker_result()

            {result, %{message: elem(message, 0)}}
          end
        )
      catch
        :exit, {:timeout, _details} -> {:error, :worker_timeout}
      after
        if Process.alive?(pid), do: GenServer.call(pid, :clear_span)
      end
    end
  end

Then each worker would set_span, clean_span, etc.:

  def handle_call({:set_span, span, message}, _from, state) do
    function_name = elem(message, 0)
    Tracer.continue_span("#{__MODULE__}.#{function_name}", span)
    {:reply, :ok, state}
  end

  def handle_call(:clear_span, _from, state) do
    Tracer.finish_trace()
    {:reply, :ok, state}
  end