|
| 1 | +"""Restate OTEL tracer wrapper that flattens all spans under the Restate trace. |
| 2 | +
|
| 3 | +Wraps any tracer so that every span — regardless of framework nesting — becomes a |
| 4 | +direct child of the Restate invocation trace. Works transparently with any |
| 5 | +OTEL-integrated agent framework (Google ADK, Pydantic AI, OpenAI Agents, etc.). |
| 6 | +
|
| 7 | +Usage: |
| 8 | + tracer = RestateTracer(trace_api.get_tracer("my-tracer")) |
| 9 | + # All spans created by this tracer are flat children of the Restate trace. |
| 10 | +""" |
| 11 | + |
| 12 | +from opentelemetry.trace import INVALID_SPAN, use_span, Tracer, TracerProvider |
| 13 | +from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator |
| 14 | +from restate.server_context import ( |
| 15 | + current_context, |
| 16 | + get_extension_data, |
| 17 | + set_extension_data, |
| 18 | + restate_context_is_replaying, |
| 19 | +) |
| 20 | + |
| 21 | +_propagator = TraceContextTextMapPropagator() |
| 22 | +_EXTENSION_KEY = "otel_span_cleanup" |
| 23 | + |
| 24 | + |
| 25 | +class _SpanCleanup: |
| 26 | + """Stored as Restate extension data. ``__close__`` is called automatically |
| 27 | + when the Restate invocation context is torn down, ending any spans that |
| 28 | + were never properly closed (e.g. because the handler raised).""" |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + self._spans = [] |
| 32 | + |
| 33 | + def track(self, span): |
| 34 | + self._spans.append(span) |
| 35 | + |
| 36 | + def __close__(self): |
| 37 | + for span in self._spans: |
| 38 | + if span.is_recording(): |
| 39 | + span.end() |
| 40 | + self._spans.clear() |
| 41 | + |
| 42 | + |
| 43 | +class RestateTracer(Tracer): |
| 44 | + """Wraps a ``Tracer`` to always parent spans under the Restate root context. |
| 45 | +
|
| 46 | + During Restate replay, returns no-op spans to avoid duplicates.""" |
| 47 | + |
| 48 | + def __init__(self, tracer): |
| 49 | + self._tracer = tracer |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def _get_root_context(): |
| 53 | + """Extract the Restate trace parent from the current handler, or None.""" |
| 54 | + ctx = current_context() |
| 55 | + if ctx is None: |
| 56 | + raise Exception("You are not in a Restate handler") |
| 57 | + return _propagator.extract(ctx.request().attempt_headers) |
| 58 | + |
| 59 | + def start_span( |
| 60 | + self, |
| 61 | + name, |
| 62 | + context=None, |
| 63 | + kind=None, |
| 64 | + attributes=None, |
| 65 | + links=None, |
| 66 | + start_time=None, |
| 67 | + record_exception=True, |
| 68 | + set_status_on_exception=True, |
| 69 | + ): |
| 70 | + if restate_context_is_replaying.get(False): |
| 71 | + return INVALID_SPAN |
| 72 | + root = self._get_root_context() |
| 73 | + if root is not None: |
| 74 | + context = root |
| 75 | + span = self._tracer.start_span( |
| 76 | + name, |
| 77 | + context=context, |
| 78 | + kind=kind, |
| 79 | + attributes=attributes, |
| 80 | + links=links, |
| 81 | + start_time=start_time, |
| 82 | + record_exception=record_exception, |
| 83 | + set_status_on_exception=set_status_on_exception, |
| 84 | + ) |
| 85 | + self._track_span(span) |
| 86 | + return span |
| 87 | + |
| 88 | + def start_as_current_span( |
| 89 | + self, |
| 90 | + name, |
| 91 | + context=None, |
| 92 | + kind=None, |
| 93 | + attributes=None, |
| 94 | + links=None, |
| 95 | + start_time=None, |
| 96 | + record_exception=True, |
| 97 | + set_status_on_exception=True, |
| 98 | + end_on_exit=True, |
| 99 | + ): |
| 100 | + if restate_context_is_replaying.get(False): |
| 101 | + return use_span(INVALID_SPAN, end_on_exit=False) |
| 102 | + root = self._get_root_context() |
| 103 | + if root is not None: |
| 104 | + context = root |
| 105 | + return self._tracer.start_as_current_span( |
| 106 | + name, |
| 107 | + context=context, |
| 108 | + kind=kind, |
| 109 | + attributes=attributes, |
| 110 | + links=links, |
| 111 | + start_time=start_time, |
| 112 | + record_exception=record_exception, |
| 113 | + set_status_on_exception=set_status_on_exception, |
| 114 | + end_on_exit=end_on_exit, |
| 115 | + ) |
| 116 | + |
| 117 | + @staticmethod |
| 118 | + def _track_span(span): |
| 119 | + """Register a span for cleanup when the Restate invocation ends.""" |
| 120 | + ctx = current_context() |
| 121 | + if ctx is None: |
| 122 | + return |
| 123 | + cleanup = get_extension_data(ctx, _EXTENSION_KEY) |
| 124 | + if cleanup is None: |
| 125 | + cleanup = _SpanCleanup() |
| 126 | + set_extension_data(ctx, _EXTENSION_KEY, cleanup) |
| 127 | + cleanup.track(span) |
| 128 | + |
| 129 | + def __getattr__(self, name): |
| 130 | + return getattr(self._tracer, name) |
| 131 | + |
| 132 | + |
| 133 | +class RestateTracerProvider(TracerProvider): |
| 134 | + """Wraps a ``TracerProvider`` to return ``RestateTracer`` instances. |
| 135 | +
|
| 136 | + Pass this to instrumentors (e.g. ``GoogleADKInstrumentor``) so that every |
| 137 | + span they create is automatically parented under the Restate invocation.""" |
| 138 | + |
| 139 | + def __init__(self, provider): |
| 140 | + self._provider = provider |
| 141 | + |
| 142 | + def get_tracer(self, *args, **kwargs): |
| 143 | + return RestateTracer(self._provider.get_tracer(*args, **kwargs)) |
| 144 | + |
| 145 | + def __getattr__(self, name): |
| 146 | + return getattr(self._provider, name) |
0 commit comments