|
| 1 | +defmodule FunWithFlags.Telemetry do |
| 2 | + @moduledoc """ |
| 3 | + Telemetry events for FunWithFlags. |
| 4 | +
|
| 5 | + This module centralizes the emission of all [Telemetry](https://hexdocs.pm/telemetry/readme.html) |
| 6 | + events for the package. |
| 7 | +
|
| 8 | + ## Events |
| 9 | +
|
| 10 | + The common prefix for all events is `:fun_with_flags`, followed by a logical |
| 11 | + scope (e.g. `:persistence`) and the event name. |
| 12 | +
|
| 13 | + Events are simple "point in time" events rather than span events (that is, |
| 14 | + there is no distinct `:start` and `:stop` events with a duration measurement). |
| 15 | +
|
| 16 | + ### Persistence |
| 17 | +
|
| 18 | + Events for CRUD operations on the persistent datastore. |
| 19 | +
|
| 20 | + All events contain the same measurement: |
| 21 | + * `system_time` (integer), which is the current system time in the |
| 22 | + `:native` time unit. See `:erlang.system_time/0`. |
| 23 | +
|
| 24 | + Events: |
| 25 | +
|
| 26 | + * `[:fun_with_flags, :persistence, :read]`, emitted when a flag is read from |
| 27 | + the DB. Crucially, this event is not emitted when the cache is enabled and |
| 28 | + there is a cache hit, and it's emitted only when retrieving a flag reads |
| 29 | + from the persistent datastore. Therefore, when the cache is disabled, this |
| 30 | + event is always emitted every time a flag is queried. |
| 31 | +
|
| 32 | + Metadata: |
| 33 | + * `flag_name` (string), the name of the flag being read. |
| 34 | +
|
| 35 | + * `[:fun_with_flags, :persistence, :read_all_flags]`, emitted when all flags |
| 36 | + are read from the DB. No extra metadata. |
| 37 | +
|
| 38 | + * `[:fun_with_flags, :persistence, :read_all_flag_names]`, emitted when all |
| 39 | + flags names are read from the DB. No extra metadata. |
| 40 | +
|
| 41 | + * `[:fun_with_flags, :persistence, :write]`, emitted when writing a flag to |
| 42 | + the DB. In practive, what is written is one of the gates of the flag, which |
| 43 | + is always upserted. |
| 44 | +
|
| 45 | + Metadata: |
| 46 | + * `flag_name` (string), the name of the flag being written. |
| 47 | + * `gate` (`FunWithFlags.Gate`), the gate being upserted. |
| 48 | +
|
| 49 | + * `[:fun_with_flags, :persistence, :delete_flag]`, emitted when an entire flag |
| 50 | + is deleted from the DB. |
| 51 | +
|
| 52 | + Metadata: |
| 53 | + * `flag_name` (string), the name of the flag being deleted. |
| 54 | +
|
| 55 | + * `[:fun_with_flags, :persistence, :delete_gate]`, emitted when one of the flag's |
| 56 | + gates is deleted from the DB. |
| 57 | +
|
| 58 | + Metadata: |
| 59 | + * `flag_name` (string), the name of the flag whose gate is being deleted. |
| 60 | + * `gate` (`FunWithFlags.Gate`), the gate being deleted. |
| 61 | +
|
| 62 | + * `[:fun_with_flags, :persistence, :reload]`, emitted when a flag is reloaded |
| 63 | + from the DB. This typically happens when the node has received a change |
| 64 | + notification for a flag, which results in the cache being invalidated and |
| 65 | + the flag being reloaded from the DB. |
| 66 | +
|
| 67 | + Metadata: |
| 68 | + * `flag_name` (string), the name of the flag being reloaded. |
| 69 | +
|
| 70 | + * `[:fun_with_flags, :persistence, :error]`, emitted for erorrs in any of the |
| 71 | + above operations. |
| 72 | +
|
| 73 | + Metadata: |
| 74 | + * `error` (any), the error that occurred. This is typically a string or any |
| 75 | + appropriate error term returned by the underlying persistence adapters. |
| 76 | + * `original_event` (atom), the name of the original event that failed, e.g. |
| 77 | + `:read`, `:write`, `:delete_gate`, etc. |
| 78 | + * `flag_name` (string), the name of the flag being operated on, if supported |
| 79 | + by the original event. |
| 80 | + * `gate` (`FunWithFlags.Gate`), the gate being operated on, if supported by |
| 81 | + the original event. |
| 82 | + """ |
| 83 | + |
| 84 | + require Logger |
| 85 | + |
| 86 | + @typedoc false |
| 87 | + @type pipelining_value :: {:ok, any()} | {:error, any()} |
| 88 | + |
| 89 | + # Receive the flag name as an explicit parameter rather than pattern matching |
| 90 | + # it from the `{:ok, _}` tuple, because: |
| 91 | + # |
| 92 | + # * That tuple is only available on success, and it's therefore not available |
| 93 | + # when pipelining on an error. |
| 94 | + # * It makes it possible to use this function even when the :ok result does |
| 95 | + # not contain a flag. |
| 96 | + # |
| 97 | + @doc false |
| 98 | + @spec emit_persistence_event( |
| 99 | + pipelining_value(), |
| 100 | + event_name :: atom(), |
| 101 | + flag_name :: (atom() | nil), |
| 102 | + gate :: (FunWithFlags.Gate.t | nil) |
| 103 | + ) :: pipelining_value() |
| 104 | + def emit_persistence_event(result = {:ok, _}, event_name, flag_name, gate) do |
| 105 | + metadata = %{ |
| 106 | + flag_name: flag_name, |
| 107 | + gate: gate, |
| 108 | + } |
| 109 | + |
| 110 | + do_send_event([:fun_with_flags, :persistence, event_name], metadata) |
| 111 | + result |
| 112 | + end |
| 113 | + |
| 114 | + def emit_persistence_event(result = {:error, reason}, event_name, flag_name, gate) do |
| 115 | + metadata = %{ |
| 116 | + flag_name: flag_name, |
| 117 | + gate: gate, |
| 118 | + error: reason, |
| 119 | + original_event: event_name |
| 120 | + } |
| 121 | + |
| 122 | + do_send_event([:fun_with_flags, :persistence, :error], metadata) |
| 123 | + result |
| 124 | + end |
| 125 | + |
| 126 | + @doc false |
| 127 | + @spec do_send_event([atom], :telemetry.event_metadata()) :: :ok |
| 128 | + def do_send_event(event_name, metadata) do |
| 129 | + measurements = %{ |
| 130 | + system_time: :erlang.system_time() |
| 131 | + } |
| 132 | + |
| 133 | + Logger.debug(fn -> |
| 134 | + "Telemetry event: #{inspect(event_name)}, metadata: #{inspect(metadata)}, measurements: #{inspect(measurements)}" |
| 135 | + end) |
| 136 | + |
| 137 | + :telemetry.execute(event_name, measurements, metadata) |
| 138 | + end |
| 139 | + |
| 140 | + |
| 141 | + @doc """ |
| 142 | + Attach a debug handler to FunWithFlags telemetry events. |
| 143 | +
|
| 144 | + Attach a Telemetry handler that logs all events at the `:alert` level. |
| 145 | + It uses the `:alert` level rather than `:debug` or `:info` simply to make it |
| 146 | + more convenient to eyeball these logs and to print them while running the tests. |
| 147 | + """ |
| 148 | + @spec attach_debug_handler() :: :ok | {:error, :already_exists} |
| 149 | + def attach_debug_handler do |
| 150 | + events = [ |
| 151 | + [:fun_with_flags, :persistence, :read], |
| 152 | + [:fun_with_flags, :persistence, :read_all_flags], |
| 153 | + [:fun_with_flags, :persistence, :read_all_flag_names], |
| 154 | + [:fun_with_flags, :persistence, :write], |
| 155 | + [:fun_with_flags, :persistence, :delete_flag], |
| 156 | + [:fun_with_flags, :persistence, :delete_gate], |
| 157 | + [:fun_with_flags, :persistence, :reload], |
| 158 | + [:fun_with_flags, :persistence, :error], |
| 159 | + ] |
| 160 | + |
| 161 | + :telemetry.attach_many("local-debug-handler", events, &__MODULE__.debug_event_handler/4, %{}) |
| 162 | + end |
| 163 | + |
| 164 | + @doc false |
| 165 | + def debug_event_handler([:fun_with_flags, :persistence, event], %{system_time: system_time}, metadata, _config) do |
| 166 | + dt = DateTime.from_unix!(system_time, :native) |> DateTime.to_iso8601() |
| 167 | + |
| 168 | + Logger.alert(fn -> |
| 169 | + "FunWithFlags telemetry event: #{event}, system_time: #{dt}, metadata: #{inspect(metadata)}" |
| 170 | + end) |
| 171 | + |
| 172 | + :ok |
| 173 | + end |
| 174 | +end |
0 commit comments