-
Notifications
You must be signed in to change notification settings - Fork 14
Add a RestateTracer and RestateTracerProvider for AI observability #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| from ._tracing import RestateTracer, RestateTracerProvider | ||
|
|
||
| __all__ = ["RestateTracer", "RestateTracerProvider"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| """Restate OTEL tracer wrapper that flattens all spans under the Restate trace. | ||
|
|
||
| Wraps any tracer so that every span — regardless of framework nesting — becomes a | ||
| direct child of the Restate invocation trace. Works transparently with any | ||
| OTEL-integrated agent framework (Google ADK, Pydantic AI, OpenAI Agents, etc.). | ||
|
|
||
| Usage: | ||
| tracer = RestateTracer(trace_api.get_tracer("my-tracer")) | ||
| # All spans created by this tracer are flat children of the Restate trace. | ||
| """ | ||
|
|
||
| from opentelemetry.trace import INVALID_SPAN, use_span, Tracer, TracerProvider | ||
| from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator | ||
| from restate.server_context import ( | ||
| current_context, | ||
| get_extension_data, | ||
| set_extension_data, | ||
| restate_context_is_replaying, | ||
| ) | ||
|
|
||
| _propagator = TraceContextTextMapPropagator() | ||
| _EXTENSION_KEY = "otel_span_cleanup" | ||
|
|
||
|
|
||
| class _SpanCleanup: | ||
| """Stored as Restate extension data. ``__close__`` is called automatically | ||
| when the Restate invocation context is torn down, ending any spans that | ||
| were never properly closed (e.g. because the handler raised).""" | ||
|
|
||
| def __init__(self): | ||
| self._spans = [] | ||
|
|
||
| def track(self, span): | ||
| self._spans.append(span) | ||
|
|
||
| def __close__(self): | ||
| for span in self._spans: | ||
| if span.is_recording(): | ||
| span.end() | ||
| self._spans.clear() | ||
|
|
||
|
|
||
| class RestateTracer(Tracer): | ||
| """Wraps a ``Tracer`` to always parent spans under the Restate root context. | ||
|
|
||
| During Restate replay, returns no-op spans to avoid duplicates.""" | ||
|
|
||
| def __init__(self, tracer): | ||
| self._tracer = tracer | ||
|
|
||
| @staticmethod | ||
| def _get_root_context(): | ||
| """Extract the Restate trace parent from the current handler, or None.""" | ||
| ctx = current_context() | ||
| if ctx is None: | ||
| raise Exception("You are not in a Restate handler") | ||
| return _propagator.extract(ctx.request().attempt_headers) | ||
|
|
||
| def start_span( | ||
| self, | ||
| name, | ||
| context=None, | ||
| kind=None, | ||
| attributes=None, | ||
| links=None, | ||
| start_time=None, | ||
| record_exception=True, | ||
| set_status_on_exception=True, | ||
| ): | ||
| if restate_context_is_replaying.get(False): | ||
| return INVALID_SPAN | ||
| root = self._get_root_context() | ||
| if root is not None: | ||
| context = root | ||
| span = self._tracer.start_span( | ||
| name, | ||
| context=context, | ||
| kind=kind, | ||
| attributes=attributes, | ||
| links=links, | ||
| start_time=start_time, | ||
| record_exception=record_exception, | ||
| set_status_on_exception=set_status_on_exception, | ||
| ) | ||
| self._track_span(span) | ||
| return span | ||
|
|
||
| def start_as_current_span( | ||
| self, | ||
| name, | ||
| context=None, | ||
| kind=None, | ||
| attributes=None, | ||
| links=None, | ||
| start_time=None, | ||
| record_exception=True, | ||
| set_status_on_exception=True, | ||
| end_on_exit=True, | ||
| ): | ||
| if restate_context_is_replaying.get(False): | ||
| return use_span(INVALID_SPAN, end_on_exit=False) | ||
| root = self._get_root_context() | ||
| if root is not None: | ||
| context = root | ||
| return self._tracer.start_as_current_span( | ||
| name, | ||
| context=context, | ||
| kind=kind, | ||
| attributes=attributes, | ||
| links=links, | ||
| start_time=start_time, | ||
| record_exception=record_exception, | ||
| set_status_on_exception=set_status_on_exception, | ||
| end_on_exit=end_on_exit, | ||
| ) | ||
|
|
||
| @staticmethod | ||
| def _track_span(span): | ||
| """Register a span for cleanup when the Restate invocation ends.""" | ||
| ctx = current_context() | ||
| if ctx is None: | ||
| return | ||
| cleanup = get_extension_data(ctx, _EXTENSION_KEY) | ||
| if cleanup is None: | ||
| cleanup = _SpanCleanup() | ||
| set_extension_data(ctx, _EXTENSION_KEY, cleanup) | ||
| cleanup.track(span) | ||
|
|
||
| def __getattr__(self, name): | ||
| return getattr(self._tracer, name) | ||
|
|
||
|
|
||
| class RestateTracerProvider(TracerProvider): | ||
| """Wraps a ``TracerProvider`` to return ``RestateTracer`` instances. | ||
|
|
||
| Pass this to instrumentors (e.g. ``GoogleADKInstrumentor``) so that every | ||
| span they create is automatically parented under the Restate invocation.""" | ||
|
|
||
| def __init__(self, provider): | ||
| self._provider = provider | ||
|
|
||
| def get_tracer(self, *args, **kwargs): | ||
| return RestateTracer(self._provider.get_tracer(*args, **kwargs)) | ||
|
|
||
| def __getattr__(self, name): | ||
| return getattr(self._provider, name) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you really need to collect spans like this? if you create the parent span for hte invocation attempt, and close that one, all the child spans should be closed as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gvdongen worth to double check if this creates an issues?