Sometimes while instrumenting your application, you may want to filter out or modify certain spans from being sent to Phoenix. For example, you may want to filter out spans that are that contain sensitive information or contain redundant information.
To do this, you can use a custom SpanProcessor and attach it to the OpenTelemetry TracerProvider.
from phoenix.otel import registerfrom phoenix.otel import BatchSpanProcessorfrom opentelemetry.context import Contextfrom opentelemetry.sdk.trace import ReadableSpan, SpanclassFilteringSpanProcessor(BatchSpanProcessor):def_filter_condition(self,span: Span) ->bool:# returns True if the span should be filtered outreturn span.name =="secret_span"defon_start(self,span: Span,parent_context: Context) ->None:if self._filter_condition(span):returnsuper().on_start(span, parent_context)defon_end(self,span: ReadableSpan) ->None:if self._filter_condition(span): logger.info("Filtering span: %s", span.name)returnsuper().on_end(span)tracer_provider =register()tracer_provider.add_span_processor(FilteringSpanProcessor( endpoint="http://localhost:6006/v1/traces", protocol="http/protobuf", ))
In this example, we're filtering out any spans that have the name "secret_span" by bypassing the on_start and on_end hooks of the inherited BatchSpanProcessor.
Notice that this logic can be extended to modify a span and redact sensitive information if preserving the span is preferred.