Telemetry integration for the Fred library.
Fred emits telemetry events using :telemetry.span/3 for every API request.
Each span produces three possible events:
Events
[:fred, :request, :start]
Emitted when a request begins.
Measurements:
:system_time- System time at the start of the request (in native units)
Metadata:
:url- The requested URL:params- The query parameters map (with:api_keyredacted)
[:fred, :request, :stop]
Emitted when a request completes successfully (the HTTP round-trip finished, regardless of API-level errors like 404s).
Measurements:
:duration- Total wall-clock duration in native time units. Convert withSystem.convert_time_unit(duration, :native, :millisecond).
Metadata:
:url- The requested URL:params- The query parameters map (with:api_keyredacted):status- HTTP status code (integer) ornilif the request never completed:result-:okor:error:error- The%Fred.Error{}struct (only present when:resultis:error)
[:fred, :request, :exception]
Emitted when the request raises an unexpected exception.
Measurements:
:duration- Wall-clock duration until the exception
Metadata:
:url- The requested URL:params- The query parameters map (with:api_keyredacted):kind- The exception kind (:throw,:error,:exit):reason- The exception or thrown value:stacktrace- The stacktrace
Attaching Handlers
You can attach your own handlers to any of these events:
:telemetry.attach(
"my-fred-handler",
[:fred, :request, :stop],
&MyApp.handle_fred_event/4,
nil
)Or use the built-in logger for quick request observability:
# In your Application.start/2:
Fred.Telemetry.Logger.attach()