@@ -61,7 +61,7 @@ Let's begin instrumenting our agent with OpenTelemetry tracing, by starting off
61
61
``` python
62
62
from azure.ai.projects import AIProjectClient
63
63
from azure.identity import DefaultAzureCredential
64
- project_client = AIProjectClient.from_connection_string (
64
+ project_client = AIProjectClient(
65
65
credential = DefaultAzureCredential(),
66
66
endpoint = os.environ[" PROJECT_ENDPOINT" ],
67
67
)
@@ -71,12 +71,8 @@ Next, retrieve the connection string from the Application Insights resource conn
71
71
72
72
``` python
73
73
from azure.monitor.opentelemetry import configure_azure_monitor
74
- connection_string = project_client.telemetry.get_connection_string()
75
-
76
- if not connection_string:
77
- print (" Application Insights is not enabled. Enable by going to Tracing in your Azure AI Foundry project." )
78
- exit ()
79
74
75
+ connection_string = project_client.telemetry.get_application_insights_connection_string()
80
76
configure_azure_monitor(connection_string = connection_string) # enable telemetry collection
81
77
```
82
78
@@ -92,11 +88,11 @@ with tracer.start_as_current_span("example-tracing"):
92
88
name = " my-assistant" ,
93
89
instructions = " You are a helpful assistant"
94
90
)
95
- thread = project_client.agents.create_thread ()
96
- message = project_client.agents.create_message (
91
+ thread = project_client.agents.threads.create ()
92
+ message = project_client.agents.messages.create (
97
93
thread_id = thread.id, role = " user" , content = " Tell me a joke"
98
94
)
99
- run = project_client.agents.create_run (thread_id = thread.id, agent_id = agent.id)
95
+ run = project_client.agents.runs.create_and_process (thread_id = thread.id, agent_id = agent.id)
100
96
```
101
97
102
98
After running your agent, you can go begin to [ view traces in Azure AI Foundry Portal] ( #view-traces-in-azure-ai-foundry-portal ) .
@@ -108,7 +104,8 @@ To connect to [Aspire Dashboard](https://aspiredashboard.com/#start) or another
108
104
``` bash
109
105
pip install azure-core-tracing-opentelemetry opentelemetry-exporter-otlp opentelemetry-sdk
110
106
```
111
- Next, you want to configure tracing for your application.
107
+
108
+ Next, configure tracing for console output:
112
109
113
110
``` python
114
111
from azure.core.settings import settings
@@ -124,18 +121,16 @@ tracer_provider = TracerProvider()
124
121
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter))
125
122
trace.set_tracer_provider(tracer_provider)
126
123
```
127
- Use ` enable_telemetry ` to begin collecting telemetry.
128
124
129
- ``` python
130
- from azure.ai.projects import enable_telemetry
131
- enable_telemetry( destination = sys.stdout)
125
+ Or modify the above code, based on [ Aspire Dashboard ] ( https://aspiredashboard.com/#start ) , to trace to a local OTLP viewer.
126
+
127
+ Now enable Agent instrumentation and run your Agent:
132
128
133
- # Logging to an OTLP endpoint, change the destination to
134
- # enable_telemetry(destination="http://localhost:4317")
135
- ```
136
129
``` python
130
+ from azure.ai.agents.telemetry import AIAgentsInstrumentor
131
+ AIAgentsInstrumentor().instrument()
132
+
137
133
# Start tracing
138
- from opentelemetry import trace
139
134
tracer = trace.get_tracer(__name__ )
140
135
141
136
with tracer.start_as_current_span(" example-tracing" ):
@@ -144,11 +139,11 @@ with tracer.start_as_current_span("example-tracing"):
144
139
name = " my-assistant" ,
145
140
instructions = " You are a helpful assistant"
146
141
)
147
- thread = project_client.agents.create_thread ()
148
- message = project_client.agents.create_message (
142
+ thread = project_client.agents.threads.create ()
143
+ message = project_client.agents.messages.create (
149
144
thread_id = thread.id, role = " user" , content = " Tell me a joke"
150
145
)
151
- run = project_client.agents.create_run (thread_id = thread.id, agent_id = agent.id)
146
+ run = project_client.agents.runs.create_and_process (thread_id = thread.id, agent_id = agent.id)
152
147
```
153
148
154
149
## Trace custom functions
0 commit comments