[][src]Module opentelemetry::sdk::trace::span_processor

Span Processor

Span processor is an interface which allows hooks for span start and end method invocations. Span processors are invoked only when is_recording is true. Built-in span processors are responsible for batching and conversion of spans to exportable representation and passing batches to exporters. Span processors can be registered directly on SDK Provider and they are invoked in the same order as they were registered. All Tracer instances created by a Provider share the same span processors. Changes to this collection reflect in all Tracer instances. The following diagram shows SpanProcessor's relationship to other components in the SDK:

+-----+--------------+   +-------------------------+   +-------------------+
|     |              |   |                         |   |                   |
|     |              |   | BatchExporterProcessor  |   |    SpanExporter   |
|     |              +---> SimpleExporterProcessor +--->  (JaegerExporter) |
|     |              |   |                         |   |                   |
| SDK | Span.start() |   +-------------------------+   +-------------------+
|     | Span.end()   |
|     |              |   +---------------------+
|     |              |   |                     |
|     |              +---> ZPagesProcessor     |
|     |              |   |                     |
+-----+--------------+   +---------------------+

Examples

Exporting spans with a simple exporter:

Note that the simple processor exports synchronously every time a span is ended. If you find this limiting, consider the batch processor instead.

use opentelemetry::{api, sdk, global};

// Configure your preferred exporter
let exporter = api::NoopSpanExporter {};

// Then use the `with_simple_exporter` method to have the provider export when spans finish.
let provider = sdk::Provider::builder()
    .with_simple_exporter(exporter)
    .build();

global::set_provider(provider);

Exporting spans asynchronously in batches:

This processor can be configured with an executor of your choice to batch and upload spans asynchronously when they end. If you have added a library like tokio or async-std, you can pass in their respective spawn and interval functions to have batching performed in those contexts.

use futures::{stream};
use opentelemetry::{api, sdk, global};
use std::time::Duration;

#[tokio::main]
async fn main() {
    // Configure your preferred exporter
    let exporter = api::NoopSpanExporter {};

    // Then build a batch processor. You can use whichever executor you have available, for
    // example if you are using `async-std` instead of `tokio` you can replace the spawn and
    // interval functions with `async_std::task::spawn` and `async_std::stream::interval`.
    let batch = sdk::BatchSpanProcessor::builder(exporter, tokio::spawn, tokio::time::interval)
        .with_max_queue_size(4096)
        .build();

    // Then use the `with_batch_exporter` method to have the provider export spans in batches.
    let provider = sdk::Provider::builder()
        .with_batch_exporter(batch)
        .build();

    global::set_provider(provider);
}

Structs

BatchConfig

Batch span processor configuration

BatchSpanProcessor

A SpanProcessor that asynchronously buffers finished spans and reports them at a preconfigured interval.

BatchSpanProcessorBuilder

A builder for creating BatchSpanProcessor instances.

BatchSpanProcessorWorker

A worker process that batches and processes spans as they are reported.

SimpleSpanProcessor

A SpanProcessor that exports synchronously when spans are finished.