Convert a session to a trace with all related spans
(session: Session)
| 191 | |
| 192 | |
| 193 | async 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 | |
| 233 | async def get_sessions(offset: int, limit: int) -> list[Session]: |
no test coverage detected
searching dependent graphs…