How to write a custom trace exporter

Kong bundled OpenTelemetry plugin in core with a implementation of OTLP/HTTP, but you can still write your own exporter at scale.

Gathering the spans

The spans are stored in the tracer’s buffer. The buffer is a queue of spans that are awaiting to be sent to the backend.

You can access the buffer and process the span using the span_processor function.

  1. -- Use the global tracer
  2. local tracer = kong.tracing
  3. -- Process the span
  4. local span_processor = function(span)
  5. -- clone the span so it can be processed after the original one is cleared
  6. local span_dup = table.clone(span)
  7. -- you can transform the span, add tags, etc. to other specific data structures
  8. end

The span_processor function should be called in the log phase of the plugin.

Full example

Refer to Github to see the example of a custom trace exporter.