TestcontainerEx.Telemetry (testcontainer_ex v0.5.0)

Copy Markdown View Source

Telemetry integration for TestcontainerEx.

Emits structured events at key lifecycle points so users can hook into monitoring, metrics, or logging via :telemetry.

Events

EventMeasurementsMetadata
[:testcontainer_ex, :container, :start]duration, monotonic_timecontainer_id, image
[:testcontainer_ex, :container, :stop]duration, monotonic_timecontainer_id
[:testcontainer_ex, :image, :pull]duration, monotonic_timeimage
[:testcontainer_ex, :network, :create]duration, monotonic_timenetwork_name
[:testcontainer_ex, :network, :remove]duration, monotonic_timenetwork_name
[:testcontainer_ex, :ryuk, :start]duration, monotonic_timesession_id
[:testcontainer_ex, :wait, :strategy]duration, monotonic_timestrategy, container_id

Usage

Attach a handler to measure container start times:

:telemetry.attach(
  "my-handler",
  [:testcontainer_ex, :container, :start],
  fn _name, measurements, _meta, _config ->
    :io.format("Container started in ~p us~n", [measurements[:duration]])
  end,
  nil
)

With telemetry_metrics

Metrics.summary("testcontainer_ex.container.start.duration",
  unit: {:microsecond, :millisecond}
)

Summary

Functions

Emits a telemetry event without timing a function.

Returns the list of all event names this module can emit. Useful for documentation or for attaching handlers programmatically.

Executes the given function and emits a telemetry event with timing.

Functions

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

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

Emits a telemetry event without timing a function.

Useful for instantaneous events like "container stopped".

events()

@spec events() :: [[atom()]]

Returns the list of all event names this module can emit. Useful for documentation or for attaching handlers programmatically.

with_telemetry(event_name, metadata, fun)

@spec with_telemetry([atom()], map(), (-> result)) :: result when result: var

Executes the given function and emits a telemetry event with timing.

Returns the result of fun. on success, or re-raises on error (still emitting the event with error: true in metadata).

Parameters

  • event_name — list of atoms, e.g. [:testcontainer_ex, :container, :start]
  • metadata — map of extra metadata to include
  • fun — zero-arity function to time

Examples

with_telemetry(
  [:testcontainer_ex, :container, :start],
  %{image: "redis:7"},
  fn -> Lifecycle.start_container(builder, conn, state) end
)