Skip to main content

SpanProcessor

Trait SpanProcessor 

pub trait SpanProcessor:
    Send
    + Sync
    + Debug {
    // Required methods
    fn on_start(&self, span: &mut Span, cx: &Context);
    fn on_end(&self, span: SpanData);
    fn force_flush(&self) -> Result<(), OTelSdkError>;
    fn shutdown_with_timeout(
        &self,
        timeout: Duration,
    ) -> Result<(), OTelSdkError>;

    // Provided methods
    fn shutdown(&self) -> Result<(), OTelSdkError> { ... }
    fn set_resource(&mut self, _resource: &Resource) { ... }
}
Available on crate features opentelemetry and trace only.
Expand description

SpanProcessor is an interface which allows hooks for span start and end method invocations. The span processors are invoked only when is_recording is true.

Required Methods§

fn on_start(&self, span: &mut Span, cx: &Context)

on_start is called when a Span is started. This method is called synchronously on the thread that started the span, therefore it should not block or throw exceptions.

fn on_end(&self, span: SpanData)

on_end is called after a Span is ended (i.e., the end timestamp is already set). This method is called synchronously within the Span::end API, therefore it should not block or throw an exception.

§Accessing Context

Important: Do not rely on Context::current() in on_end. When on_end is called during span cleanup, Context::current() returns whatever context happens to be active at that moment, which is typically unrelated to the span being ended. Contexts can be activated in any order and are not necessarily hierarchical.

Best Practice: Extract any needed context information in on_start and store it as span attributes. This ensures the information is available in the SpanData passed to on_end.

§Example
impl SpanProcessor for MyProcessor {
    fn on_start(&self, span: &mut Span, cx: &Context) {
        // Extract baggage and store as span attribute
        if let Some(value) = cx.baggage().get("my-key") {
            span.set_attribute(KeyValue::new("my-key", value.to_string()));
        }
    }

    fn on_end(&self, span: SpanData) {
        // Access the attribute stored in on_start
        let my_value = span.attributes.iter()
            .find(|kv| kv.key.as_str() == "my-key");
    }
}

TODO - This method should take reference to SpanData

fn force_flush(&self) -> Result<(), OTelSdkError>

Force the spans lying in the cache to be exported.

fn shutdown_with_timeout(&self, timeout: Duration) -> Result<(), OTelSdkError>

Shuts down the processor. Called when SDK is shut down. This is an opportunity for processors to do any cleanup required.

Implementation should make sure shutdown can be called multiple times.

Provided Methods§

fn shutdown(&self) -> Result<(), OTelSdkError>

shutdown the processor with a default timeout.

fn set_resource(&mut self, _resource: &Resource)

Set the resource for the span processor.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§