Otel.SDK.Trace.SpanExporter behaviour (otel v0.2.0)

Copy Markdown View Source

Behaviour for span exporters (trace/sdk.md §Span Exporter L1120-L1209).

Exporters receive batches of completed spans and transmit them to a backend. Protocol-specific logic (encoding, transport) lives in the exporter implementation.

export/3 MUST NOT be called concurrently for the same instance — the SpanProcessor serialises calls per spec L1146-L1147. shutdown/1 is invoked once at provider shutdown.

Spec trace/sdk.md L1135 — "The exporter MUST support three functions: Export, Shutdown, and ForceFlush". All three are required callbacks of this behaviour. Most exporters implement force_flush/1 as a no-op because they don't internally buffer (the Batch SpanProcessor does the buffering and calls export/3 synchronously); the callback exists so the TracerProvider's force-flush path can propagate cleanly through the processor → exporter chain.

Public API

CallbackRole
init/1SDK (lifecycle)
export/3SDK (OTel API MUST) — trace/sdk.md §Export L1139-L1164
shutdown/1SDK (OTel API MUST) — trace/sdk.md §Shutdown L1182-L1206
force_flush/1SDK (OTel API MUST) — trace/sdk.md §ForceFlush L1167-L1180

References

  • OTel Trace SDK §Span Exporter: opentelemetry-specification/specification/trace/sdk.md L1120-L1209

Summary

Callbacks

Exports a batch of spans. MUST NOT block indefinitely.

Flushes any pending spans. For exporters that don't internally buffer, this is a no-op returning :ok.

Initializes the exporter. Returns {:ok, state} or :ignore.

Shuts down the exporter.

Types

state()

@type state() :: term()

Callbacks

export(spans, resource, state)

@callback export(
  spans :: [Otel.SDK.Trace.Span.t()],
  resource :: Otel.SDK.Resource.t(),
  state :: state()
) :: :ok | :error

Exports a batch of spans. MUST NOT block indefinitely.

force_flush(state)

@callback force_flush(state :: state()) :: :ok | :error

Flushes any pending spans. For exporters that don't internally buffer, this is a no-op returning :ok.

init(config)

@callback init(config :: term()) :: {:ok, state()} | :ignore

Initializes the exporter. Returns {:ok, state} or :ignore.

shutdown(state)

@callback shutdown(state :: state()) :: :ok

Shuts down the exporter.