Skip to content

Commit e94a4e8

Browse files
committed
Move tracing uuids to use a separate Random from the workflow seed
1 parent d863f5c commit e94a4e8

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

temporalio/contrib/openai_agents/_temporal_trace_provider.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Provides support for integration with OpenAI Agents SDK tracing across workflows"""
22

33
import uuid
4+
from random import Random
45
from types import TracebackType
56
from typing import Any, Optional, cast
7+
from uuid import UUID
68

79
from agents import SpanData, Trace, TracingProcessor
810
from agents.tracing import (
@@ -133,6 +135,15 @@ def force_flush(self) -> None:
133135
self._impl.force_flush()
134136

135137

138+
def _workflow_uuid() -> str:
139+
random = cast(
140+
Random, getattr(workflow.instance(), "__temporal_openai_tracing_random")
141+
)
142+
return UUID(bytes=random.getrandbits(16 * 8).to_bytes(16, "big"), version=4).hex[
143+
:24
144+
]
145+
146+
136147
class TemporalTraceProvider(DefaultTraceProvider):
137148
"""A trace provider that integrates with Temporal workflows."""
138149

@@ -156,7 +167,7 @@ def gen_trace_id(self) -> str:
156167
if workflow.in_workflow():
157168
try:
158169
"""Generate a new trace ID."""
159-
return f"trace_{workflow.uuid4().hex}"
170+
return f"trace_{_workflow_uuid()}"
160171
except ReadOnlyContextError:
161172
return f"trace_{uuid.uuid4().hex}"
162173
return super().gen_trace_id()
@@ -166,7 +177,7 @@ def gen_span_id(self) -> str:
166177
if workflow.in_workflow():
167178
try:
168179
"""Generate a deterministic span ID."""
169-
return f"span_{workflow.uuid4().hex[:24]}"
180+
return f"span_{_workflow_uuid()}"
170181
except ReadOnlyContextError:
171182
return f"span_{uuid.uuid4().hex[:24]}"
172183
return super().gen_span_id()
@@ -176,7 +187,7 @@ def gen_group_id(self) -> str:
176187
if workflow.in_workflow():
177188
try:
178189
"""Generate a deterministic group ID."""
179-
return f"group_{workflow.uuid4().hex[:24]}"
190+
return f"group_{_workflow_uuid()}"
180191
except ReadOnlyContextError:
181192
return f"group_{uuid.uuid4().hex[:24]}"
182193
return super().gen_group_id()

temporalio/contrib/openai_agents/_trace_interceptor.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import random
56
from contextlib import contextmanager
67
from typing import Any, Mapping, Protocol, Type, cast
78

@@ -283,6 +284,18 @@ async def execute_activity(
283284
return await self.next.execute_activity(input)
284285

285286

287+
def _ensure_tracing_random() -> None:
288+
instance = workflow.instance()
289+
if not hasattr(instance, "__temporal_openai_tracing_random"):
290+
new_random = random.Random()
291+
new_random.setstate(workflow.random().getstate())
292+
setattr(
293+
workflow.instance(),
294+
"__temporal_openai_tracing_random",
295+
new_random,
296+
)
297+
298+
286299
class _ContextPropagationWorkflowInboundInterceptor(
287300
temporalio.worker.WorkflowInboundInterceptor
288301
):
@@ -292,18 +305,21 @@ def init(self, outbound: temporalio.worker.WorkflowOutboundInterceptor) -> None:
292305
async def execute_workflow(
293306
self, input: temporalio.worker.ExecuteWorkflowInput
294307
) -> Any:
308+
_ensure_tracing_random()
295309
with context_from_header(
296310
"temporal:executeWorkflow", input, temporalio.workflow.payload_converter()
297311
):
298312
return await self.next.execute_workflow(input)
299313

300314
async def handle_signal(self, input: temporalio.worker.HandleSignalInput) -> None:
315+
_ensure_tracing_random()
301316
with context_from_header(
302317
"temporal:handleSignal", input, temporalio.workflow.payload_converter()
303318
):
304319
return await self.next.handle_signal(input)
305320

306321
async def handle_query(self, input: temporalio.worker.HandleQueryInput) -> Any:
322+
_ensure_tracing_random()
307323
with context_from_header(
308324
"temporal:handleQuery", input, temporalio.workflow.payload_converter()
309325
):
@@ -322,6 +338,7 @@ def handle_update_validator(
322338
async def handle_update_handler(
323339
self, input: temporalio.worker.HandleUpdateInput
324340
) -> Any:
341+
_ensure_tracing_random()
325342
with context_from_header(
326343
"temporal:handleUpdateHandler",
327344
input,

0 commit comments

Comments
 (0)