instrument_flight_recorder (instrument v1.0.0)

View Source

Flight recorder for low-overhead message tracing.

This module captures message passing events at VM level with minimal overhead, correlating them with OpenTelemetry spans via trace_id labels.

Uses erlang:trace with a custom erl_tracer NIF that distributes events across a pool of workers, avoiding the single-process bottleneck of seq_trace.

Example Usage

  %% Flight recorder starts enabled by default
  instrument_tracer:with_span(<<"request">>, fun() ->
    %% All message passing captured with trace_id label
    gen_server:call(some_server, request),
    another_process ! message
  end),
 
  %% On error or slow request, dump the trace
  TraceId = instrument_tracer:trace_id(),
  Events = instrument_flight_recorder:get_trace(TraceId).

Summary

Functions

Clears all events from the buffer.

Disables the flight recorder. Stops the worker pool. Buffer is retained.

Gets all events in the buffer.

Gets and clears all events for a trace ID.

Enables the flight recorder. Starts the worker pool and enables tracing.

Gets all events for a trace ID. Returns events as a list of {Timestamp, Event} tuples.

Checks if the flight recorder is enabled.

Adds a custom marker event with current trace context.

Adds a custom marker event with metadata.

Sets the buffer size (for testing). Takes effect immediately.

Starts the flight recorder with default options.

Starts the flight recorder with options.

Returns statistics about the flight recorder.

Gets the tracer state for use with erlang:trace.

Functions

clear()

-spec clear() -> ok.

Clears all events from the buffer.

code_change(OldVsn, State, Extra)

disable()

-spec disable() -> ok.

Disables the flight recorder. Stops the worker pool. Buffer is retained.

dump_all()

-spec dump_all() -> [{integer(), integer(), term()}].

Gets all events in the buffer.

dump_trace(TraceId)

-spec dump_trace(binary() | integer()) -> [{integer(), term()}].

Gets and clears all events for a trace ID.

enable()

-spec enable() -> ok.

Enables the flight recorder. Starts the worker pool and enables tracing.

get_trace(TraceId)

-spec get_trace(binary() | integer()) -> [{integer(), term()}].

Gets all events for a trace ID. Returns events as a list of {Timestamp, Event} tuples.

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(Args)

is_enabled()

-spec is_enabled() -> boolean().

Checks if the flight recorder is enabled.

mark(Name)

-spec mark(binary()) -> ok.

Adds a custom marker event with current trace context.

mark(Name, Meta)

-spec mark(binary(), map()) -> ok.

Adds a custom marker event with metadata.

set_buffer_size(Size)

-spec set_buffer_size(pos_integer()) -> ok.

Sets the buffer size (for testing). Takes effect immediately.

start_link()

-spec start_link() -> {ok, pid()} | ignore | {error, term()}.

Starts the flight recorder with default options.

start_link(Args)

-spec start_link(list()) -> {ok, pid()} | ignore | {error, term()}.

Starts the flight recorder with options.

stats()

-spec stats() -> map().

Returns statistics about the flight recorder.

terminate(Reason, State)

tracer_state(TraceId)

-spec tracer_state(binary()) -> map().

Gets the tracer state for use with erlang:trace.