telemetry.ex (2368B)
1 defmodule Absinthe.Phase.Telemetry do 2 @moduledoc """ 3 Gather and report telemetry about an operation. 4 """ 5 @operation_start [:absinthe, :execute, :operation, :start] 6 @operation_stop [:absinthe, :execute, :operation, :stop] 7 8 @subscription_start [:absinthe, :subscription, :publish, :start] 9 @subscription_stop [:absinthe, :subscription, :publish, :stop] 10 11 use Absinthe.Phase 12 13 def run(blueprint, options) do 14 event = Keyword.fetch!(options, :event) 15 do_run(blueprint, event, options) 16 end 17 18 defp do_run(blueprint, [:execute, :operation, :start], options) do 19 id = :erlang.unique_integer() 20 system_time = System.system_time() 21 start_time_mono = System.monotonic_time() 22 23 :telemetry.execute( 24 @operation_start, 25 %{system_time: system_time}, 26 %{id: id, telemetry_span_context: id, blueprint: blueprint, options: options} 27 ) 28 29 {:ok, 30 %{ 31 blueprint 32 | source: blueprint.input, 33 telemetry: %{id: id, start_time_mono: start_time_mono} 34 }} 35 end 36 37 defp do_run(blueprint, [:subscription, :publish, :start], options) do 38 id = :erlang.unique_integer() 39 system_time = System.system_time() 40 start_time_mono = System.monotonic_time() 41 42 :telemetry.execute( 43 @subscription_start, 44 %{system_time: system_time}, 45 %{id: id, telemetry_span_context: id, blueprint: blueprint, options: options} 46 ) 47 48 {:ok, 49 %{ 50 blueprint 51 | telemetry: %{id: id, start_time_mono: start_time_mono} 52 }} 53 end 54 55 defp do_run(blueprint, [:subscription, :publish, :stop], options) do 56 end_time_mono = System.monotonic_time() 57 58 with %{id: id, start_time_mono: start_time_mono} <- blueprint.telemetry do 59 :telemetry.execute( 60 @subscription_stop, 61 %{duration: end_time_mono - start_time_mono}, 62 %{id: id, telemetry_span_context: id, blueprint: blueprint, options: options} 63 ) 64 end 65 66 {:ok, blueprint} 67 end 68 69 defp do_run(blueprint, [:execute, :operation, :stop], options) do 70 end_time_mono = System.monotonic_time() 71 72 with %{id: id, start_time_mono: start_time_mono} <- blueprint.telemetry do 73 :telemetry.execute( 74 @operation_stop, 75 %{duration: end_time_mono - start_time_mono}, 76 %{id: id, telemetry_span_context: id, blueprint: blueprint, options: options} 77 ) 78 end 79 80 {:ok, blueprint} 81 end 82 end