-
Notifications
You must be signed in to change notification settings - Fork 146
OpenAI OTel logging #1270
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
Draft
jssmith
wants to merge
9
commits into
temporalio:main
Choose a base branch
from
jssmith:openai-otel-logging
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
OpenAI OTel logging #1270
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c9ff7ac
Add create_spans parameter and TemporalAwareContext for OTEL support
jssmith 48b7af5
Add OtelTracingPlugin to openai_agents contrib module
jssmith f436013
Add workflow_span() helper for replay-safe OTEL spans
jssmith e966c8d
Refactor OTEL tracing: use passthrough instead of TemporalAwareContext
jssmith 4f56f9d
Move OTEL tests from samples to SDK, add shared fixtures
jssmith a5a5158
OtelTracingPlugin: auto-configure sandbox passthrough
jssmith 4070836
Add concurrency and cross-process trace context tests
jssmith 58730ce
Document OTEL cross-process trace propagation design
jssmith aa9a383
Add TemporalIdGenerator for deterministic span IDs in workflows
jssmith 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| """OpenTelemetry tracing support for OpenAI Agents in Temporal workflows.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from contextlib import contextmanager | ||
| from typing import TYPE_CHECKING, Iterator | ||
|
|
||
| from opentelemetry import trace as otel_trace | ||
| from opentelemetry.trace import Span | ||
|
|
||
| from temporalio import workflow | ||
|
|
||
| if TYPE_CHECKING: | ||
| from opentelemetry.sdk.trace import TracerProvider | ||
|
|
||
|
|
||
| def setup_tracing(tracer_provider: TracerProvider) -> None: | ||
| """Set up OpenAI Agents OTEL tracing with OpenInference instrumentation. | ||
|
|
||
| This instruments the OpenAI Agents SDK with OpenInference, which converts | ||
| agent spans to OTEL spans. Combined with opentelemetry passthrough in the | ||
| sandbox, this enables proper span parenting inside Temporal's workflows. | ||
|
|
||
| Args: | ||
| tracer_provider: The TracerProvider to use for creating spans. | ||
| """ | ||
| from openinference.instrumentation.openai_agents import OpenAIAgentsInstrumentor | ||
|
|
||
| otel_trace.set_tracer_provider(tracer_provider) | ||
| OpenAIAgentsInstrumentor().instrument(tracer_provider=tracer_provider) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def workflow_span(name: str, **attributes: str) -> Iterator[Span | None]: | ||
| """Create an OTEL span in workflow code that is replay-safe. | ||
|
|
||
| This context manager creates a span only on the first execution of the | ||
| workflow task, not during replay. This prevents span duplication when | ||
| workflow code is re-executed during replay (e.g., when max_cached_workflows=0). | ||
|
|
||
| .. warning:: | ||
| This API is experimental and may change in future versions. | ||
| Consider using ReplayFilteringSpanProcessor instead for automatic | ||
| filtering of all spans during replay. | ||
|
|
||
| Args: | ||
| name: The name of the span. | ||
| **attributes: Optional attributes to set on the span. | ||
|
|
||
| Yields: | ||
| The span on first execution, None during replay. | ||
|
|
||
| Example: | ||
| >>> @workflow.defn | ||
| ... class MyWorkflow: | ||
| ... @workflow.run | ||
| ... async def run(self) -> str: | ||
| ... with workflow_span("my_operation", query="test") as span: | ||
| ... result = await workflow.execute_activity(...) | ||
| ... return result | ||
|
|
||
| Note: | ||
| Spans created in activities do not need this wrapper since activities | ||
| are not replayed - they only execute once and their results are cached. | ||
| """ | ||
| if workflow.unsafe.is_replaying(): | ||
| yield None | ||
| else: | ||
| tracer = otel_trace.get_tracer(__name__) | ||
| with tracer.start_as_current_span(name) as span: | ||
| for key, value in attributes.items(): | ||
| span.set_attribute(key, value) | ||
| yield span |
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
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.
I don't think this comment makes any sense.