telemetry.ex (1421B)
1 defmodule Absinthe.Middleware.Telemetry do 2 @moduledoc """ 3 Gather and report telemetry about an individual field resolution 4 """ 5 @field_start [:absinthe, :resolve, :field, :start] 6 @field_stop [:absinthe, :resolve, :field, :stop] 7 8 @behaviour Absinthe.Middleware 9 10 @impl Absinthe.Middleware 11 def call(resolution, _) do 12 id = :erlang.unique_integer() 13 system_time = System.system_time() 14 start_time_mono = System.monotonic_time() 15 16 :telemetry.execute( 17 @field_start, 18 %{system_time: system_time}, 19 %{id: id, telemetry_span_context: id, resolution: resolution} 20 ) 21 22 %{ 23 resolution 24 | middleware: 25 resolution.middleware ++ 26 [ 27 {{__MODULE__, :on_complete}, 28 %{ 29 id: id, 30 start_time_mono: start_time_mono, 31 middleware: resolution.middleware 32 }} 33 ] 34 } 35 end 36 37 def on_complete( 38 %{state: :resolved} = resolution, 39 %{ 40 id: id, 41 start_time_mono: start_time_mono, 42 middleware: middleware 43 } 44 ) do 45 end_time_mono = System.monotonic_time() 46 47 :telemetry.execute( 48 @field_stop, 49 %{duration: end_time_mono - start_time_mono}, 50 %{ 51 id: id, 52 telemetry_span_context: id, 53 middleware: middleware, 54 resolution: resolution 55 } 56 ) 57 58 resolution 59 end 60 end