instrument_span_processor behaviour (instrument v0.6.1)

View Source

OpenTelemetry Span Processor behavior and chain manager.

Span processors are invoked at span start and end to perform additional processing such as batching and exporting.

Built-in Processors

- instrument_span_processor_simple - Immediate export - instrument_span_processor_batch - Batched export

Example Usage

  %% Register a processor
  instrument_span_processor:register(instrument_span_processor_batch, #{
    exporter => instrument_exporter_console,
    max_queue_size => 2048
  }).
 
  %% Unregister a processor
  instrument_span_processor:unregister(instrument_span_processor_batch).

Callback Restrictions

WARNING: Processor callbacks (on_start/2, on_end/1) execute within the span processor gen_server. These callbacks must NOT call back into the span processor system, as this will cause a deadlock.

Avoid calling these functions from processor callbacks: - instrument_span_processor:register/2 - instrument_span_processor:unregister/1 - instrument_span_processor:list/0 - instrument_span_processor:shutdown/0 - instrument_span_processor:force_flush/0

Safe patterns for async work in callbacks: - Spawn a new process for external calls - Store in ETS for later batch processing - Use async message passing

Summary

Functions

Forces all processors to flush.

Lists all registered processors.

Called when a span ends.

Called when a span ends - inline version without gen_server hop. Uses cached processor list from persistent_term for O(1) access.

Called when a span starts. Returns potentially modified span.

Called when a span starts - inline version without gen_server hop. Uses cached processor list from persistent_term for O(1) access.

Registers a span processor with configuration.

Shuts down all processors.

Starts the span processor manager.

Unregisters a span processor.

Callbacks

force_flush/0

-callback force_flush() -> ok.

on_end/1

-callback on_end(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()}) ->
                    ok.

on_start/2

-callback on_start(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()},
                   ParentCtx ::
                       #span_ctx{trace_id :: <<_:128>> | undefined,
                                 span_id :: <<_:64>> | undefined,
                                 trace_flags :: 0 | 1,
                                 trace_state :: [{binary(), binary()}],
                                 is_remote :: boolean()} |
                       undefined) ->
                      #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()}.

shutdown/0

-callback shutdown() -> ok.

Functions

code_change(OldVsn, State, Extra)

force_flush()

-spec force_flush() -> ok.

Forces all processors to flush.

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(_)

list()

-spec list() -> [module()].

Lists all registered processors.

on_end(Span)

-spec on_end(#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()}) ->
                ok.

Called when a span ends.

on_end_inline(Span)

-spec on_end_inline(#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()}) ->
                       ok.

Called when a span ends - inline version without gen_server hop. Uses cached processor list from persistent_term for O(1) access.

on_start(Span, ParentCtx)

-spec on_start(#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()},
               #span_ctx{trace_id :: <<_:128>> | undefined,
                         span_id :: <<_:64>> | undefined,
                         trace_flags :: 0 | 1,
                         trace_state :: [{binary(), binary()}],
                         is_remote :: boolean()} |
               undefined) ->
                  #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()}.

Called when a span starts. Returns potentially modified span.

on_start_inline(Span, ParentCtx)

-spec on_start_inline(#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()},
                      #span_ctx{trace_id :: <<_:128>> | undefined,
                                span_id :: <<_:64>> | undefined,
                                trace_flags :: 0 | 1,
                                trace_state :: [{binary(), binary()}],
                                is_remote :: boolean()} |
                      undefined) ->
                         #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()}.

Called when a span starts - inline version without gen_server hop. Uses cached processor list from persistent_term for O(1) access.

register(ProcessorModule, Config)

-spec register(module(), map()) -> ok | {error, term()}.

Registers a span processor with configuration.

shutdown()

-spec shutdown() -> ok.

Shuts down all processors.

start_link()

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

Starts the span processor manager.

terminate(Reason, State)

unregister(ProcessorModule)

-spec unregister(module()) -> ok.

Unregisters a span processor.