> ## Documentation Index
> Fetch the complete documentation index at: https://cndoc-langchain.site/llms.txt
> Use this file to discover all available pages before exploring further.

# 追踪 Microsoft Agent Framework 应用程序

LangSmith 可以通过其内置的 OpenTelemetry 检测功能，捕获由 [Microsoft Agent Framework](https://learn.microsoft.com/en-us/agent-framework/overview/agent-framework-overview) 生成的追踪数据。本指南将向您展示如何自动捕获来自 Microsoft Agent Framework 代理的追踪数据，并将其发送到 LangSmith 进行监控和分析。

## 安装

安装所需的包：

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install agent-framework opentelemetry-exporter-otlp-proto-http
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add agent-framework opentelemetry-exporter-otlp-proto-http
  ```
</CodeGroup>

## 设置

### 1. 配置环境变量

启用代理的 OpenTelemetry 检测，并设置 OpenTelemetry 环境变量以指向 LangSmith OTEL 端点：

```bash theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
export ENABLE_INSTRUMENTATION=true
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_ENDPOINT=https://api.smith.langchain.com/otel/v1/traces
export OTEL_EXPORTER_OTLP_HEADERS="x-api-key=<your_langsmith_api_key>,Langsmith-Project=<your_project_name>"
```

### 2. 在应用程序中启用 OpenTelemetry

在您的 Microsoft Agent Framework 应用程序中，使用内置的 `configure_otel_providers` 函数启用 OpenTelemetry 追踪：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from agent_framework.observability import configure_otel_providers

# 启用 OpenTelemetry 追踪
configure_otel_providers(enable_sensitive_data=True)
```

<Note>
  设置 `enable_sensitive_data=True` 允许在追踪中捕获输入和输出内容。如果您希望从追踪中排除敏感数据，请将其设置为 `False`。
</Note>

### 3. 创建并运行您的代理

配置完成后，您的 Microsoft Agent Framework 代理将自动向 LangSmith 发送追踪数据：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from agent_framework import ChatAgent
from agent_framework.observability import configure_otel_providers
from agent_framework.openai import OpenAIChatClient

# 启用 OpenTelemetry 追踪
configure_otel_providers(enable_sensitive_data=True)


agent = ChatAgent(
    chat_client=OpenAIChatClient(model_id="gpt-4o"),
)

result = await agent.run("What's the the capital of Bavaria?")
print(result.text)
```

***

<div className="source-links">
  <Callout icon="terminal-2">
    [将这些文档](/use-these-docs) 通过 MCP 连接到 Claude、VSCode 等，以获取实时答案。
  </Callout>

  <Callout icon="edit">
    [在 GitHub 上编辑此页面](https://github.com/langchain-ai/docs/edit/main/src/langsmith/trace-with-microsoft-agent-framework.mdx) 或 [提交问题](https://github.com/langchain-ai/docs/issues/new/choose)。
  </Callout>
</div>
