BCUtils.Telemetry (bc_utils v0.10.0)

Telemetry integration for BCUtils modules.

This module provides helper functions to set up telemetry events for monitoring and performance analysis within the BCUtils library.

Usage

Event Setup

:telemetry.attach(
  "my-event-handler",
  [:bc_utils, :module, :function, :start],
  &MyModule.handle_event/4,
  %{some: "metadata"}
)

Event Emission

def my_function(arg1, arg2) do
  start_time = System.monotonic_time()
  :telemetry.execute(
    [:bc_utils, :my_module, :my_function, :start],
    %{system_time: System.system_time(), arg1: arg1, arg2: arg2},
    %{start_time: start_time}
  )
  
  # Perform operation...
  
  :telemetry.execute(
    [:bc_utils, :my_module, :my_function, :stop],
    %{duration: System.monotonic_time() - start_time},
    %{arg1: arg1, arg2: arg2}
  )
end

Best Practices

  • Use descriptive event names
  • Tag events with function/module names
  • Include relevant metadata and measurements
  • Consider performance impact of telemetry hooks

Summary

Functions

Detaches a telemetry handler.

Publishes a start event for telemetry.

Publishes a stop event for telemetry.

Functions

attach_handler(handler_id, event_name, handler_fn, handler_config \\ %{})

@spec attach_handler(String.t(), [atom()], function(), map()) :: :ok | {:error, any()}

Attaches a telemetry handler.

Parameters

  • handler_id - Unique identifier for the event handler
  • event_name - The name (list) of the telemetry event to handle
  • handler_fn - The function to call when the event occurs
  • handler_config - Additional handler configuration

detach_handler(handler_id)

@spec detach_handler(String.t()) :: :ok | {:error, any()}

Detaches a telemetry handler.

Parameters

  • handler_id - Unique identifier for the event handler

start_event(name, metadata \\ %{})

@spec start_event([atom()], map()) :: :ok

Publishes a start event for telemetry.

Parameters

  • name - The name (list) identifying the telemetry event
  • metadata - Metadata map for additional context

stop_event(name, metadata, start_time)

@spec stop_event([atom()], map(), integer()) :: :ok

Publishes a stop event for telemetry.

Parameters

  • name - The name (list) identifying the telemetry event
  • metadata - Metadata map for additional context
  • start_time - The start time of the operation for duration calculation