Implementing Tracing for Foundry Agents and Custom Code
This lesson explains how to implement tracing for Foundry agents and custom code, which records each step of every agent run, and how to use it for execution tracking and alerting.
Learning outcomes
After completing this lesson, you will be able to:
- Set up tracing with Application Insights.
- Record tokens, prompts and correlation IDs with the GenAI semantic conventions.
- Use traces for execution tracking and alerts.
Setting up tracing
Tracing in Foundry uses OpenTelemetry and stores data in Application Insights. The first step is to connect an Application Insights resource to the Foundry project. Agents running in Foundry Agent Service are then traced automatically. Application code, including Agent Framework and LangGraph orchestrators, configures the Azure Monitor OpenTelemetry exporter with the connection string from the project and enables instrumentation for the libraries it uses.
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry import trace
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
configure_azure_monitor(connection_string=project.telemetry.get_application_insights_connection_string())
OpenAIInstrumentor().instrument() # spans for every OpenAI client call
tracer = trace.get_tracer("claims-orchestrator")
with tracer.start_as_current_span("process_claim") as span:
span.set_attribute("app.claim_id", claim_id) # business correlation ID for searching
span.set_attribute("app.tenant", tenant_id)
result = await workflow.run(claim_text)Traces with gaps
Tokens, prompts and correlation IDs
Spans follow the GenAI semantic conventions: model call spans record the operation, model, input and output token counts and finish reasons; agent spans record invocations; tool spans record tool names and outcomes. Prompts and completions are not captured by default. Setting OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true records them, which is invaluable for debugging but stores user content, so it is enabled according to privacy policy, often only in non-production environments or for sampled traffic.
Correlation IDs link telemetry. The trace ID ties all spans of a request across services through W3C Trace Context propagation, and custom attributes such as a case number, tenant or conversation ID make traces searchable by business key and support per-tenant analysis.
What spans record
Execution tracking and alerting
The Foundry portal shows traces per agent and conversation as a tree of steps, which provides execution tracking: which agent ran, which tools it called with which arguments, what each returned, how long each step took and how many tokens it used. Application Insights provides the same data for queries with Kusto Query Language (KQL).
Alerting uses Azure Monitor alert rules on this telemetry: log search alerts on KQL queries (for example, the rate of failed tool spans, runs exceeding a token threshold, or runs with more than N iterations) and metric alerts on deployment metrics. Alerts route through action groups to people and automation. The following table lists common trace-based alerts.
| Alert | Signal from traces |
|---|---|
| Tool failures | Share of tool spans with error status above threshold |
| Token spikes | Tokens per run above the 99th percentile baseline |
| Runaway loops | Runs with iteration count above the cap |
| Slow runs | Workflow span duration above the SLO |
Alerts built on traces
Tracing across services
An orchestrator in Container Apps calls a Foundry agent and an MCP server on Azure Functions. Traces in Application Insights show the orchestrator's spans and the Foundry agent's spans as separate traces, and the MCP server's calls do not appear at all.
Trace context is not propagated, and the MCP server is not instrumented. The orchestrator must pass W3C Trace Context headers on outgoing calls (OpenTelemetry HTTP instrumentation does this), and the Functions app must export OpenTelemetry to the same Application Insights resource. Adding more log statements would not join the traces.
Why add business IDs, such as a claim ID, as span attributes?
Show answer
They make traces searchable by the IDs people use when they report a problem.
Exam Tips
Remember for the exam
- Connect Application Insights to the project; export app spans with the Azure Monitor distribution.
- Message content capture is opt-in; decide by privacy policy.
- Business IDs as span attributes make traces searchable; alerts use KQL on trace data.