11"""Custom span processors for UiPath execution tracing."""
22
3- from typing import Optional , cast
3+ from typing import cast
44
55from opentelemetry import context as context_api
66from opentelemetry import trace
7- from opentelemetry .sdk .trace import Span
7+ from opentelemetry .sdk .trace import ReadableSpan , Span , SpanProcessor
88from opentelemetry .sdk .trace .export import (
99 BatchSpanProcessor ,
1010 SimpleSpanProcessor ,
11+ SpanExporter ,
1112)
1213
14+ from uipath .core .tracing .types import UiPathTraceSettings
15+
1316
1417class UiPathExecutionTraceProcessorMixin :
15- def on_start (
16- self , span : Span , parent_context : Optional [context_api .Context ] = None
17- ):
18+ """Mixin that propagates execution.id and optionally filters spans."""
19+
20+ _settings : UiPathTraceSettings | None = None
21+
22+ def on_start (self , span : Span , parent_context : context_api .Context | None = None ):
1823 """Called when a span is started."""
19- parent_span : Optional [ Span ]
24+ parent_span : Span | None
2025 if parent_context :
2126 parent_span = cast (Span , trace .get_current_span (parent_context ))
2227 else :
@@ -27,17 +32,42 @@ def on_start(
2732 if execution_id :
2833 span .set_attribute ("execution.id" , execution_id )
2934
35+ def on_end (self , span : ReadableSpan ):
36+ """Called when a span ends. Filters before delegating to parent."""
37+ span_filter = self ._settings .span_filter if self ._settings else None
38+ if span_filter is None or span_filter (span ):
39+ parent = cast (SpanProcessor , super ())
40+ parent .on_end (span )
41+
3042
3143class UiPathExecutionBatchTraceProcessor (
3244 UiPathExecutionTraceProcessorMixin , BatchSpanProcessor
3345):
34- """Batch span processor that propagates execution.id."""
46+ """Batch span processor that propagates execution.id and optionally filters."""
47+
48+ def __init__ (
49+ self ,
50+ span_exporter : SpanExporter ,
51+ settings : UiPathTraceSettings | None = None ,
52+ ):
53+ """Initialize the batch trace processor."""
54+ super ().__init__ (span_exporter )
55+ self ._settings = settings
3556
3657
3758class UiPathExecutionSimpleTraceProcessor (
3859 UiPathExecutionTraceProcessorMixin , SimpleSpanProcessor
3960):
40- """Simple span processor that propagates execution.id."""
61+ """Simple span processor that propagates execution.id and optionally filters."""
62+
63+ def __init__ (
64+ self ,
65+ span_exporter : SpanExporter ,
66+ settings : UiPathTraceSettings | None = None ,
67+ ):
68+ """Initialize the simple trace processor."""
69+ super ().__init__ (span_exporter )
70+ self ._settings = settings
4171
4272
4373__all__ = [
0 commit comments