Tracing, Logging and Debugging
This lesson explains traces and spans, the generative AI semantic conventions, how applications are instrumented, how content recording is controlled, and how traces are used to debug.
Learning outcomes
After completing this lesson, you will be able to:
- Describe traces and spans for generative AI.
- Explain the role of the semantic conventions.
- Describe instrumentation with configure_azure_monitor.
- Explain the risks and settings of content recording.
Traces and spans
A trace records one request's journey through an application as a tree of spans, each a timed operation with attributes. In a generative AI application, spans represent the operations that matter for diagnosis: the incoming request, retrieval queries, each model call, each tool call and each agent step. The trace shows what happened, in what order, how long each step took, and what each returned.
Tracing in Foundry is built on OpenTelemetry, the open standard for telemetry, and stores traces in the Application Insights resource connected to the project. Tracing is generally available for prompt agents and hosted agents, and in preview for workflow and external agents.
Generative AI semantic conventions
OpenTelemetry's generative AI semantic conventions standardize span names and attributes, so traces from different libraries and frameworks look alike and tools can interpret them. The conventions are still marked as in development, so names may change.
| Item | Examples |
|---|---|
| Span names | chat, invoke_agent, execute_tool, embeddings |
| Model attributes | gen_ai.request.model, gen_ai.response.model, gen_ai.response.finish_reasons |
| Usage attributes | gen_ai.usage.input_tokens, gen_ai.usage.output_tokens |
| Tool attributes | gen_ai.tool.name, gen_ai.tool.call.arguments, gen_ai.tool.call.result |
Instrumenting an application
Instrumentation has two parts: an exporter that sends telemetry to Application Insights, and instrumentors that create spans for libraries. The project client returns the connection string of the project's Application Insights resource, and the Azure Monitor OpenTelemetry distribution configures export. Instrumentation libraries for the OpenAI SDK, the Microsoft Agent Framework, LangChain and LangGraph and other frameworks create the generative AI spans.
import os
from azure.ai.projects import AIProjectClient
from azure.identity import DefaultAzureCredential
from azure.monitor.opentelemetry import configure_azure_monitor
from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor
from opentelemetry import trace
project = AIProjectClient(endpoint=PROJECT_ENDPOINT, credential=DefaultAzureCredential())
configure_azure_monitor(connection_string=project.telemetry.get_application_insights_connection_string())
OpenAIInstrumentor().instrument()
tracer = trace.get_tracer("support-assistant")
with tracer.start_as_current_span("answer_question") as span:
span.set_attribute("app.prompt_version", os.environ["PROMPT_VERSION"])
passages = search(question) # custom spans can wrap retrieval too
reply = openai.responses.create(model="chat", input=build_messages(question, passages))Custom spans around application steps, such as retrieval, reranking and post-processing, complete the picture, and attributes such as the prompt version, user tier and feature make traces searchable by the dimensions that matter.
Content recording
By default, traces record metadata, such as models, timings, token counts and finish reasons, but not the content of prompts, responses and tool arguments, because content can contain personal or confidential data. Content recording is enabled deliberately, for example in development or under a data handling policy, by setting OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to true or through the instrumentor's options.
Content is data
Traces with content are stored in Application Insights with that resource's retention and access controls. Treat them as sensitive data: limit who can query them, set retention to what policy allows, and avoid enabling content recording in production without approval.
Viewing, querying and debugging
Traces appear in the Foundry portal's tracing view, where a conversation can be opened as a tree of spans with inputs, outputs and timings, and in Application Insights, where the transaction search and end-to-end view show the same data. KQL queries over the dependencies, requests and traces tables find patterns across many requests, such as the slowest tool, the most frequent finish reason of length, or errors by prompt version. Logs are correlated with traces through the operation ID.
| Symptom | What to look for in the trace |
|---|---|
| Wrong or unsupported answer | The retrieved passages in the retrieval span, and the prompt sent to the model |
| Agent loops or takes many steps | Repeated tool calls with similar arguments, and the model's tool choices |
| Truncated answer | A finish reason of length, meaning the token limit was reached |
| Slow request | The longest span: retrieval, a tool, reasoning or generation |
| Failed request | Error status and exception events on the failing span, such as 429 or a tool error |
| Content filter block | A finish reason or error indicating content filtering on the model span |
An agent that gives up
A travel booking agent sometimes replies "I couldn't complete your request" after about 40 seconds. Latency metrics show the slow requests but not the cause. The agent uses the Microsoft Agent Framework, and the project has Application Insights connected, but the application does not send traces.
Instrument the agent with OpenTelemetry tracing to the project's Application Insights resource, using the framework's instrumentation, and open the traces of failed requests. The execute_tool spans will show which tool fails or retries, with its arguments and errors, and the model spans will show how the agent reacted. In development, enabling content recording shows the exact messages; in production, metadata and error details are often enough.
Raising the agent's timeout would make failures slower without explaining them. Switching to a larger model guesses at a cause the evidence has not shown. Platform metrics on the Foundry resource cannot see tool calls made by the agent.
Traces show model calls but not the prompts and completions. Why, and what are the risks of changing it?
Show answer
Content recording is off by default; it is enabled with OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT. Recorded content may contain personal or confidential data, so access and retention must be controlled.
Exam Tips
Remember for the exam
- A trace is a tree of spans for requests, retrieval, model calls, tool calls and agent steps.
- Foundry tracing uses OpenTelemetry and the generative AI semantic conventions, stored in Application Insights.
- Use configure_azure_monitor with the project's connection string and library instrumentors.
- Content recording is off by default (OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT); treat recorded content as sensitive.