Foundation.Telemetry (foundation v0.2.1)

Copy Markdown View Source

Lightweight telemetry helpers with optional TelemetryReporter integration.

This module is intentionally thin: it emits :telemetry events and provides convenience wrappers for timing code. If the optional telemetry_reporter dependency is available, it can also start a reporter and attach handlers that forward events.

Summary

Functions

Attach TelemetryReporter handlers for forwarding :telemetry events.

Detach a TelemetryReporter handler.

Log a telemetry event via TelemetryReporter if available.

Measure execution time and emit :stop or :exception events.

Start a TelemetryReporter instance if the dependency is available.

Types

event_name()

@type event_name() :: [atom()]

measurements()

@type measurements() :: map()

metadata()

@type metadata() :: map()

reporter()

@type reporter() :: GenServer.server()

Functions

attach_reporter(opts)

@spec attach_reporter(keyword()) :: {:ok, term()} | {:error, :missing_dependency}

Attach TelemetryReporter handlers for forwarding :telemetry events.

Returns the handler id.

detach_reporter(handler_id)

@spec detach_reporter(term()) ::
  :ok | {:error, :not_found} | {:error, :missing_dependency}

Detach a TelemetryReporter handler.

execute(event_name, measurements \\ %{}, metadata \\ %{})

@spec execute(event_name(), measurements(), metadata()) :: :ok

Emit a telemetry event.

log(reporter, name, data \\ %{}, severity \\ :info)

@spec log(reporter(), String.t() | [atom()], map(), atom()) ::
  :ok
  | {:error, :overloaded}
  | {:error, :not_running}
  | {:error, :missing_dependency}

Log a telemetry event via TelemetryReporter if available.

measure(event_name, metadata, fun, opts \\ [])

@spec measure(event_name(), metadata(), (-> result), keyword()) :: result
when result: term()

Measure execution time and emit :stop or :exception events.

Options:

  • :time_unit - unit for duration (default: :microsecond)

start_reporter(opts)

@spec start_reporter(keyword()) ::
  GenServer.on_start() | {:error, :missing_dependency}

Start a TelemetryReporter instance if the dependency is available.