MCPcopy Create free account
hub / github.com/AgentOps-AI/agentops / get_session_as_trace

Function get_session_as_trace

app/api/agentops/exporter/processor.py:193–230  ·  view source on GitHub ↗

Convert a session to a trace with all related spans

(session: Session)

Source from the content-addressed store, hash-verified

191
192
193async def get_session_as_trace(session: Session) -> Trace:
194 """Convert a session to a trace with all related spans"""
195 write_last_session_id(session.id)
196 try:
197 trace: Trace = await session.to_trace()
198 parent_span_id = trace.spans[0].span_id
199 except Exception as e:
200 if not session:
201 write_dropped_record('session', 'unknown', e)
202 return
203 write_dropped_record('session', session.id, e)
204 return
205
206 for table_name in EXPORT_AS_SPANS:
207 model_class = EXPORT_TABLES_MODELS[table_name]
208 async for record in fetch_all_for_session(model_class, session.id):
209 try:
210 if isinstance(record, (Agent, ErrorEvent)):
211 # agent and error belong to parent span
212 span = await record.to_span(
213 trace_id=trace.id, parent_span_id=parent_span_id, project_id=str(session.project_id)
214 )
215 elif isinstance(record, (ActionEvent, LLMEvent, ToolEvent)):
216 # actions, llms, and tools belong to an agent
217 span = await record.to_span(
218 trace_id=trace.id,
219 parent_span_id=str(record.agent_id),
220 project_id=str(session.project_id),
221 )
222 else:
223 warnings.warn(f"Unknown record type: {type(record)}")
224 continue
225 except Exception as e:
226 table_name = [k for k, v in EXPORT_TABLES_MODELS.items() if v == model_class][0]
227 write_dropped_record(table_name, record.id, e)
228 continue
229 trace.spans.append(span)
230 return trace
231
232
233async def get_sessions(offset: int, limit: int) -> list[Session]:

Callers 1

get_sessions_as_tracesFunction · 0.85

Calls 5

write_last_session_idFunction · 0.85
write_dropped_recordFunction · 0.85
fetch_all_for_sessionFunction · 0.85
to_traceMethod · 0.80
to_spanMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…