instrument_test (instrument v1.0.0)
View SourceTest helpers for validating instrumentation in EUnit and Common Test.
This module provides collector exporters and assertion helpers for testing spans, metrics, and logs in your instrumented code.
Quick Start
my_test() ->
instrument_test:setup(),
try
%% Your instrumented code
instrument_tracer:with_span(<<"my_op">>, fun() ->
do_work()
end),
%% Assert span was created
instrument_test:assert_span_exists(<<"my_op">>)
after
instrument_test:cleanup()
end.Common Test Setup
init_per_testcase(_TestCase, Config) ->
instrument_test:setup(),
Config.
end_per_testcase(_TestCase, _Config) ->
instrument_test:cleanup(),
ok.
Summary
Functions
Adds a log record directly for testing purposes. Ensures the log table exists before inserting.
Asserts a counter has the expected value.
Asserts a counter has the expected value with specific attributes.
Asserts a gauge has the expected value.
Asserts a gauge has the expected value with specific attributes.
Asserts a histogram has the expected observation count.
Asserts a histogram has the expected sum.
Asserts a log record with expected properties. Expected can contain: body, severity_text, severity_number, attributes
Asserts that a log with the given body pattern exists.
Asserts that a log has trace context (trace_id and span_id).
Asserts that no span with the given name exists.
Asserts that ParentName is the parent of ChildName.
Asserts a span with expected properties. Expected can contain: attributes, status, kind, parent
Asserts a specific attribute value on a span.
Asserts that a span has an event with the given name.
Asserts that a span with the given name exists.
Asserts the status of a span.
Cleans up the test environment. Stops collectors and clears all state.
Clears all captured logs.
Clears collected metrics.
Clears all captured spans.
Triggers metrics collection and returns the results.
Returns all captured logs.
Returns all collected metrics.
Returns a span by name.
Returns all captured spans.
Resets collectors between test cases without full cleanup. Use this when you want to keep the application running but clear collected data. Also ensures all collector tables exist.
Sets up the test environment. Starts the instrument application and all collectors.
Starts the log collector.
Starts the metrics collector.
Starts the span collector. Registers an ETS-based exporter that captures all completed spans.
Stops the log collector.
Stops the metrics collector.
Stops the span collector.
Waits for a specific number of logs with timeout.
Waits for a specific number of spans with timeout.
Functions
-spec add_test_log(#log_record{timestamp :: integer() | undefined, observed_timestamp :: integer() | undefined, severity_number :: integer() | undefined, severity_text :: binary() | undefined, body :: term(), attributes :: map(), trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1 | undefined, resource :: #resource{attributes :: map(), schema_url :: binary() | undefined} | undefined, scope :: #scope{name :: binary(), version :: binary() | undefined, attributes :: map(), schema_url :: binary() | undefined} | undefined}) -> ok.
Adds a log record directly for testing purposes. Ensures the log table exists before inserting.
Asserts a counter has the expected value.
Asserts a counter has the expected value with specific attributes.
Asserts a gauge has the expected value.
Asserts a gauge has the expected value with specific attributes.
-spec assert_histogram_count(binary() | atom(), non_neg_integer()) -> ok.
Asserts a histogram has the expected observation count.
Asserts a histogram has the expected sum.
Asserts a log record with expected properties. Expected can contain: body, severity_text, severity_number, attributes
-spec assert_log_exists(term()) -> ok.
Asserts that a log with the given body pattern exists.
-spec assert_log_trace_context(term()) -> ok.
Asserts that a log has trace context (trace_id and span_id).
Asserts that no span with the given name exists.
Asserts that ParentName is the parent of ChildName.
Asserts a span with expected properties. Expected can contain: attributes, status, kind, parent
Asserts a specific attribute value on a span.
Asserts that a span has an event with the given name.
Asserts that a span with the given name exists.
Asserts the status of a span.
-spec cleanup() -> ok.
Cleans up the test environment. Stops collectors and clears all state.
-spec clear_logs() -> ok.
Clears all captured logs.
-spec clear_metrics() -> ok.
Clears collected metrics.
-spec clear_spans() -> ok.
Clears all captured spans.
-spec collect_metrics() -> [map()].
Triggers metrics collection and returns the results.
-spec get_logs() -> [#log_record{timestamp :: integer() | undefined, observed_timestamp :: integer() | undefined, severity_number :: integer() | undefined, severity_text :: binary() | undefined, body :: term(), attributes :: map(), trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1 | undefined, resource :: #resource{attributes :: map(), schema_url :: binary() | undefined} | undefined, scope :: #scope{name :: binary(), version :: binary() | undefined, attributes :: map(), schema_url :: binary() | undefined} | undefined}].
Returns all captured logs.
-spec get_metrics() -> [map()].
Returns all collected metrics.
-spec get_span(binary()) -> {ok, #span{name :: binary(), ctx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()}, parent_ctx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | undefined, tracer :: #tracer{name :: binary(), version :: binary() | undefined, schema_url :: binary() | undefined, resource :: #resource{attributes :: map(), schema_url :: binary() | undefined} | undefined} | undefined, kind :: client | server | producer | consumer | internal, start_time :: integer(), end_time :: integer() | undefined, attributes :: map(), events :: [#span_event{name :: binary(), timestamp :: integer(), attributes :: map(), dropped_attributes_count :: non_neg_integer()}], links :: [#span_link{ctx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()}, attributes :: map(), dropped_attributes_count :: non_neg_integer()}], status :: unset | ok | {error, binary()}, is_recording :: boolean(), dropped_attributes_count :: non_neg_integer(), dropped_events_count :: non_neg_integer(), dropped_links_count :: non_neg_integer()}} | {error, not_found}.
Returns a span by name.
-spec get_spans() -> [#span{name :: binary(), ctx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()}, parent_ctx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | undefined, tracer :: #tracer{name :: binary(), version :: binary() | undefined, schema_url :: binary() | undefined, resource :: #resource{attributes :: map(), schema_url :: binary() | undefined} | undefined} | undefined, kind :: client | server | producer | consumer | internal, start_time :: integer(), end_time :: integer() | undefined, attributes :: map(), events :: [#span_event{name :: binary(), timestamp :: integer(), attributes :: map(), dropped_attributes_count :: non_neg_integer()}], links :: [#span_link{ctx :: #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()}, attributes :: map(), dropped_attributes_count :: non_neg_integer()}], status :: unset | ok | {error, binary()}, is_recording :: boolean(), dropped_attributes_count :: non_neg_integer(), dropped_events_count :: non_neg_integer(), dropped_links_count :: non_neg_integer()}].
Returns all captured spans.
-spec reset() -> ok.
Resets collectors between test cases without full cleanup. Use this when you want to keep the application running but clear collected data. Also ensures all collector tables exist.
-spec setup() -> ok.
Sets up the test environment. Starts the instrument application and all collectors.
-spec start_log_collector() -> ok.
Starts the log collector.
-spec start_metrics_collector() -> ok.
Starts the metrics collector.
-spec start_span_collector() -> ok.
Starts the span collector. Registers an ETS-based exporter that captures all completed spans.
-spec stop_log_collector() -> ok.
Stops the log collector.
-spec stop_metrics_collector() -> ok.
Stops the metrics collector.
-spec stop_span_collector() -> ok.
Stops the span collector.
-spec wait_for_logs(pos_integer(), pos_integer()) -> ok | {error, timeout}.
Waits for a specific number of logs with timeout.
-spec wait_for_spans(pos_integer(), pos_integer()) -> ok | {error, timeout}.
Waits for a specific number of spans with timeout.