Skip to content

Commit 584f8de

Browse files
committed
Add tracing to procurement agent
1 parent debc2b1 commit 584f8de

File tree

2 files changed

+67
-15
lines changed

2 files changed

+67
-15
lines changed

examples/demos/procurement_agent/manifest.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,19 @@ agent:
100100
- env_var_name: REDIS_URL
101101
secret_name: redis-url-secret
102102
secret_key: url
103+
- env_var_name: SGP_API_KEY
104+
secret_name: sgp-api-key
105+
secret_key: api-key
103106
# - env_var_name: OPENAI_API_KEY
104107
# secret_name: openai-api-key
105108
# secret_key: api-key
106-
107-
# Optional: Set Environment variables for running your agent locally as well
109+
110+
# Optional: Set Environment variables for running your agent locally as well
108111
# as for deployment later on
109112
env:
110113
OPENAI_API_KEY: ""
111114
# OPENAI_BASE_URL: "<YOUR_OPENAI_BASE_URL_HERE>"
112115
OPENAI_ORG_ID: ""
113-
114-
115116
# Deployment Configuration
116117
# -----------------------
117118
# Configuration for deploying your agent to Kubernetes clusters

examples/demos/procurement_agent/project/workflow.py

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import json
23
import asyncio
34
from typing import Any, Dict, List, override
@@ -19,6 +20,7 @@
1920
ShipmentArrivedSiteEvent,
2021
ShipmentDepartedFactoryEvent,
2122
)
23+
from agentex.lib.types.tracing import SGPTracingProcessorConfig
2224
from agentex.lib.utils.logging import make_logger
2325
from agentex.types.data_content import DataContent
2426
from agentex.types.text_content import TextContent
@@ -36,6 +38,9 @@
3638
from project.agents.extract_learnings_agent import new_extract_learnings_agent
3739
from agentex.lib.core.temporal.types.workflow import SignalName
3840
from agentex.lib.core.temporal.workflows.workflow import BaseWorkflow
41+
from agentex.lib.core.tracing.tracing_processor_manager import (
42+
add_tracing_processor_config,
43+
)
3944
from agentex.lib.core.temporal.plugins.openai_agents.hooks.hooks import TemporalStreamingHooks
4045

4146
environment_variables = EnvironmentVariables.refresh()
@@ -48,14 +53,40 @@
4853

4954
logger = make_logger(__name__)
5055

56+
# Setup tracing for SGP (Scale GenAI Platform)
57+
# This enables visibility into your agent's execution in the SGP dashboard
58+
add_tracing_processor_config(
59+
SGPTracingProcessorConfig(
60+
sgp_api_key=os.environ.get("SGP_API_KEY", ""),
61+
sgp_account_id=os.environ.get("SGP_ACCOUNT_ID", ""),
62+
sgp_base_url=os.environ.get("SGP_BASE_URL"),
63+
)
64+
)
65+
66+
67+
class TurnInput(BaseModel):
68+
"""Input model for tracing spans."""
69+
input_list: List[Dict[str, Any]]
70+
71+
72+
class TurnOutput(BaseModel):
73+
"""Output model for tracing spans."""
74+
final_output: Any
75+
76+
5177
class StateModel(BaseModel):
5278
"""
5379
State model for preserving conversation history.
5480
5581
This allows the agent to maintain context throughout the conversation,
5682
making it possible to reference previous messages and build on the discussion.
83+
84+
Attributes:
85+
input_list: The conversation history in OpenAI message format.
86+
turn_number: Counter for tracking conversation turns (useful for tracing).
5787
"""
5888
input_list: List[Dict[str, Any]]
89+
turn_number: int
5990

6091

6192
@workflow.defn(name=environment_variables.WORKFLOW_NAME)
@@ -119,7 +150,7 @@ async def on_task_event_send(self, params: SendEventParams) -> None:
119150
async def on_task_create(self, params: CreateTaskParams) -> str:
120151
logger.info(f"Received task create params: {params}")
121152

122-
self._state = StateModel(input_list=[])
153+
self._state = StateModel(input_list=[], turn_number=0)
123154

124155
self._task_id = params.task.id
125156
self._trace_id = params.task.id
@@ -225,21 +256,41 @@ async def on_task_create(self, params: CreateTaskParams) -> str:
225256
)
226257
continue # Skip this event, wait for next one
227258

259+
# Increment turn number for tracing
260+
self._state.turn_number += 1
261+
262+
# Create a span to track this turn of the conversation
263+
turn_input = TurnInput(
264+
input_list=self._state.input_list,
265+
)
266+
228267
# Create agent and execute with error handling
229268
try:
230-
procurement_agent = new_procurement_agent(
231-
master_construction_schedule=master_construction_schedule,
232-
human_input_learnings=self.human_input_learnings
233-
)
269+
async with adk.tracing.span(
270+
trace_id=params.task.id,
271+
name=f"Turn {self._state.turn_number}",
272+
input=turn_input.model_dump(),
273+
) as span:
274+
self._parent_span_id = span.id if span else None
275+
276+
procurement_agent = new_procurement_agent(
277+
master_construction_schedule=master_construction_schedule,
278+
human_input_learnings=self.human_input_learnings
279+
)
280+
281+
hooks = TemporalStreamingHooks(task_id=params.task.id)
234282

235-
hooks = TemporalStreamingHooks(task_id=params.task.id)
283+
# Execute agent with graceful degradation pattern (from temporal-community demos)
284+
result = await Runner.run(procurement_agent, self._state.input_list, hooks=hooks) # type: ignore[arg-type]
236285

237-
# Execute agent with graceful degradation pattern (from temporal-community demos)
238-
result = await Runner.run(procurement_agent, self._state.input_list, hooks=hooks) # type: ignore[arg-type]
286+
# Update state with result
287+
self._state.input_list = result.to_input_list() # type: ignore[assignment]
288+
logger.info("Successfully processed event")
239289

240-
# Update state with result
241-
self._state.input_list = result.to_input_list() # type: ignore[assignment]
242-
logger.info("Successfully processed event")
290+
# Set span output for tracing
291+
if span:
292+
turn_output = TurnOutput(final_output=result.final_output)
293+
span.output = turn_output.model_dump()
243294
# Extract learnings from NEW wait_for_human calls only (using going backwards approach)
244295
try:
245296
result_context = get_new_wait_for_human_context(

0 commit comments

Comments
 (0)