instrument_tracer (instrument v0.6.1)
View SourceOpenTelemetry-compatible Tracer API.
This module provides a native span implementation with W3C TraceContext format support. Spans are stored in the process dictionary via the context system.
Example Usage
instrument_tracer:with_span(<<"process_request">>, fun() ->
instrument_tracer:set_attributes(#{<<"user.id">> => UserId}),
Result = process(),
instrument_tracer:set_status(ok),
Result
end).
Summary
Functions
Adds an event to the current span.
Adds an event with attributes to the current span. Enforces span event count limit per OTel spec.
Adds a link to the current span.
Gets the current span.
Ends the current span.
Ends a specific span. The span parameter is used to identify which span to end via its span_id. The actual span data is retrieved from context to capture any modifications.
Gets or creates a Tracer with the given name.
Gets or creates a Tracer with the given name and options.
Checks if the current span is recording.
Checks if the current span is sampled.
Records an exception on the current span.
Records an exception with attributes on the current span.
Registers a span exporter. The exporter must be a function that takes a span record. Thread-safe: uses ETS for atomic operations.
Sets a single attribute on the current span.
Sets multiple attributes on the current span. Enforces span attribute count limit per OTel spec.
Sets the status of the current span to ok.
Sets the status of the current span with description.
Gets the span context of the current span.
Gets the span context of a specific span.
Gets the span ID of the current span.
Gets the span ID of a specific span.
Starts a new span with the given name.
Starts a new span with the given name and options.
Gets the trace ID of the current span.
Gets the trace ID of a specific span.
Unregisters a span exporter. Thread-safe: uses ETS for atomic operations.
Updates the name of the current span.
Executes a function within a span.
Executes a function within a span with options.
Types
-type span() :: #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()}], 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()}], 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()}.
-type span_opts() :: #{kind => client | server | producer | consumer | internal, attributes => map(), 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()}], start_time => integer(), parent => #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | undefined, span_id => binary(), tracer => #tracer{name :: binary(), version :: binary() | undefined, schema_url :: binary() | undefined, resource :: #resource{attributes :: map(), schema_url :: binary() | undefined} | undefined} | undefined}.
Functions
-spec add_event(binary()) -> ok.
Adds an event to the current span.
Adds an event with attributes to the current span. Enforces span event count limit per OTel spec.
-spec add_link(#span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | #{}) -> ok.
Adds a link to the current span.
-spec current_span() -> span() | undefined.
Gets the current span.
-spec end_span() -> ok.
Ends the current span.
-spec end_span(span()) -> ok.
Ends a specific span. The span parameter is used to identify which span to end via its span_id. The actual span data is retrieved from context to capture any modifications.
Gets or creates a Tracer with the given name.
Gets or creates a Tracer with the given name and options.
-spec is_recording() -> boolean().
Checks if the current span is recording.
-spec is_sampled() -> boolean().
Checks if the current span is sampled.
-spec record_exception(term()) -> ok.
Records an exception on the current span.
Records an exception with attributes on the current span.
-spec register_exporter(fun((span()) -> ok)) -> ok.
Registers a span exporter. The exporter must be a function that takes a span record. Thread-safe: uses ETS for atomic operations.
Sets a single attribute on the current span.
-spec set_attributes(map()) -> ok.
Sets multiple attributes on the current span. Enforces span attribute count limit per OTel spec.
-spec set_status(ok | error) -> ok.
Sets the status of the current span to ok.
-spec set_status(ok | error, binary()) -> ok.
Sets the status of the current span with description.
-spec span_ctx() -> #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()} | undefined.
Gets the span context of the current span.
-spec span_ctx(span()) -> #span_ctx{trace_id :: <<_:128>> | undefined, span_id :: <<_:64>> | undefined, trace_flags :: 0 | 1, trace_state :: [{binary(), binary()}], is_remote :: boolean()}.
Gets the span context of a specific span.
-spec span_id() -> binary() | undefined.
Gets the span ID of the current span.
Gets the span ID of a specific span.
Starts a new span with the given name.
Starts a new span with the given name and options.
-spec trace_id() -> binary() | undefined.
Gets the trace ID of the current span.
Gets the trace ID of a specific span.
-spec unregister_exporter(fun((span()) -> ok)) -> ok.
Unregisters a span exporter. Thread-safe: uses ETS for atomic operations.
-spec update_name(binary()) -> ok.
Updates the name of the current span.
Executes a function within a span.
Executes a function within a span with options.