Span Filtering Tutorial
Filter OpenTelemetry Spans
This tutorial shows how to filter OpenTelemetry spans based on a condition.
If you're using multiple OTEL-compatible libraries, you may want to filter spans based on a condition. This can prevent you from sending spans to multiple backends unnecessarily.
There are multiple approaches to do this, in this tutorial we'll show how to do this using a custom SpanProcessor.
Install Dependencies
Define a custom SpanProcessor
We'll extend the SpanProcessor class to add a condition that determines whether a span should be exported.
Along with this class, we'll define two conditions: one for sending spans to the console, and one for sending spans to Phoenix.
Use our custom SpanProcessor to set up instrumentation
In this example, we'll use Phoenix as one of our destinations, and the console as the other. You could instead add any other exporters you'd like in this approach.
If you need to set up an API key for Phoenix, you can do so here.
Define the console exporter
Define the Phoenix exporter
Add the exporters to the tracer provider
Run our app and view traces
This first span will be exported to the console. Here we're using manual instrumentation to create the span.
This next span will be exported only to Phoenix. In this case, we're using our auto-instrumentor for OpenAI to generate the span.