Use spans to capture discrete events
mainTo capture the start and end of a discrete operation, use telemetry:span/3. This function automatically manages the lifecycle of an event by emitting three distinct events:
EventPrefix ++ [:start]: Emitted when the function begins. Measurements includesystem_time(fromerlang:system_time/0).EventPrefix ++ [:stop]: Emitted if the function completes successfully. Measurements includeduration(monotonic time difference) andmonotonic_time.EventPrefix ++ [:exception]: Emitted if the function raises an error. The error is re-raised after the event is emitted. Measurements includedurationandmonotonic_time.
To listen to all lifecycle events of a span, use attach_many/4 with the start, stop, and exception suffixes.
# Elixir: Creating a span
:telemetry.span(
[:worker, :processing],
%{message: message},
fn ->
# ... logic ...
{result, %{metadata: "info"}}
end
)
# Elixir: Attaching to all span events
:telemetry.attach_many(
"handler-id",
[[:worker, :processing, :start], [:worker, :processing, :stop], [:worker, :processing, :exception]],
&LogResponseHandler.handle_event/4,
nil
)