Supertester.TelemetryHelpers (Supertester v0.6.0)

Copy Markdown View Source

Async-safe telemetry testing helpers with per-test event isolation.

Summary

Functions

Assert exactly expected_count telemetry events were received.

Attach a telemetry handler that only receives events matching the current test ID.

Return metadata map containing the current test's telemetry ID.

Merge the current test's telemetry metadata into an existing map.

Detach a telemetry handler attached via attach_isolated/2 and release any associated buffered state.

Emit a telemetry event with the current test's context.

Flush buffered telemetry messages for a handler created with buffer: true.

Flush telemetry messages from the mailbox.

Retrieve the current test's telemetry ID.

Retrieve the current test's telemetry ID or raise if missing.

Initialize telemetry isolation for the current process.

Initialize telemetry isolation and update the isolation context.

Execute a function and capture emitted telemetry events.

Types

assert_opts()

@type assert_opts() :: [{:timeout, pos_integer()}]

attach_opts()

@type attach_opts() :: [
  filter_key: atom(),
  passthrough: boolean(),
  buffer: boolean(),
  transform: (telemetry_message() -> term())
]

event()

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

handler_id()

@type handler_id() :: String.t()

measurements()

@type measurements() :: map()

metadata()

@type metadata() :: map()

telemetry_message()

@type telemetry_message() :: {:telemetry, event(), measurements(), metadata()}

test_id()

@type test_id() :: integer()

Functions

assert_telemetry_count(event_pattern, expected_count, opts \\ [])

@spec assert_telemetry_count(
  event() | (event() -> boolean()),
  pos_integer(),
  assert_opts()
) :: [
  telemetry_message()
]

Assert exactly expected_count telemetry events were received.

attach_isolated(events, opts \\ [])

@spec attach_isolated(event() | [event()], attach_opts()) :: {:ok, handler_id()}

Attach a telemetry handler that only receives events matching the current test ID.

current_test_metadata()

@spec current_test_metadata() :: map()

Return metadata map containing the current test's telemetry ID.

current_test_metadata(existing)

@spec current_test_metadata(map()) :: map()

Merge the current test's telemetry metadata into an existing map.

detach_isolated(handler_id)

@spec detach_isolated(handler_id()) :: :ok

Detach a telemetry handler attached via attach_isolated/2 and release any associated buffered state.

emit_with_context(event, measurements \\ %{}, metadata \\ %{})

@spec emit_with_context(event(), measurements(), metadata()) :: :ok

Emit a telemetry event with the current test's context.

flush_buffered_telemetry(handler_id)

@spec flush_buffered_telemetry(handler_id()) :: [telemetry_message()]

Flush buffered telemetry messages for a handler created with buffer: true.

flush_telemetry(event_pattern)

@spec flush_telemetry(:all | event()) :: [telemetry_message()]

Flush telemetry messages from the mailbox.

get_test_id()

@spec get_test_id() :: test_id() | nil

Retrieve the current test's telemetry ID.

get_test_id!()

@spec get_test_id!() :: test_id()

Retrieve the current test's telemetry ID or raise if missing.

setup_telemetry_isolation()

@spec setup_telemetry_isolation() :: {:ok, test_id()}

Initialize telemetry isolation for the current process.

setup_telemetry_isolation(ctx)

@spec setup_telemetry_isolation(Supertester.IsolationContext.t()) ::
  {:ok, test_id(), Supertester.IsolationContext.t()}

Initialize telemetry isolation and update the isolation context.

with_telemetry(events, fun, opts \\ [])

@spec with_telemetry(event() | [event()], (-> result), attach_opts()) ::
  {result, [telemetry_message()]}
when result: term()

Execute a function and capture emitted telemetry events.