forked from microsoft/agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracing_basics.py
More file actions
63 lines (45 loc) · 2.4 KB
/
tracing_basics.py
File metadata and controls
63 lines (45 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from agent_framework import Executor, WorkflowBuilder, WorkflowContext, get_logger, handler
from agent_framework.observability import setup_observability
"""Basic tracing workflow sample.
Sample: Workflow Tracing basics
A minimal two executor workflow demonstrates built in OpenTelemetry spans when diagnostics are enabled.
The sample raises an error if tracing is not configured.
Purpose:
- Require diagnostics by checking ENABLE_OTEL and wiring a console exporter.
- Show the span categories produced by a simple graph:
- workflow.build (events: build.started, build.validation_completed, build.completed, edge_group.process)
- workflow.run (events: workflow.started, workflow.completed or workflow.error)
- executor.process (for each executor invocation)
- message.send (for each outbound message)
- Provide a tiny flow that is easy to run and reason about: uppercase then print.
Prerequisites:
- No external services required for the workflow itself.
"""
logger = get_logger()
class StartExecutor(Executor):
@handler # type: ignore[misc]
async def handle_input(self, message: str, ctx: WorkflowContext[str]) -> None:
# Transform and forward downstream. This produces executor.process and message.send spans.
await ctx.send_message(message.upper())
class EndExecutor(Executor):
@handler # type: ignore[misc]
async def handle_final(self, message: str, ctx: WorkflowContext) -> None:
# Sink executor. The workflow completes when idle with no pending work.
print(f"Final result: {message}")
async def main() -> None:
# This will enable tracing and create the necessary tracing, logging and metrics providers
# based on environment variables.
setup_observability()
# Build a two node graph: StartExecutor -> EndExecutor. The builder emits a workflow.build span.
workflow = (
WorkflowBuilder()
.add_edge(StartExecutor(id="start"), EndExecutor(id="end"))
.set_start_executor("start") # set_start_executor accepts an executor id string or the instance
.build()
) # workflow.build span emitted here
# Run once with a simple payload. You should see workflow.run plus executor and message spans.
await workflow.run("hello tracing") # workflow.run + executor.process and message.send spans
if __name__ == "__main__": # pragma: no cover
asyncio.run(main())