microsoft / microsoft/agent-framework
.NET: [Feature]: UseOpenTelemetry support for IHostedAgentBuilder
- Dominant language
- Python
- Stars
- 13.6k
- Forks
- 2.3k
- Avg merge
- 2d 45m
- Merged PRs (30d)
- 358
Description
### Requested feature
Observability (OpenTelemetry) configuration support for agents and workflows using the `IHostedAgentBuilder`
### The issue
When building an ASP.Net Core Agent Service following the [Hosting documentation](https://learn.microsoft.com/en-us/agent-framework/get-started/hosting?pivots=programming-language-csharp), I'm using IHostedAgentBuilder - something like this:
```csharp
var pirateAgent = builder.AddAIAgent(
"pirate",
instructions: "You are a pirate. Speak like a pirate",
description: "An agent that speaks like a pirate.",
chatClientServiceKey: "chat-model");
```
However, that is incompatible with the code from the [Observability documentation](https://learn.microsoft.com/en-us/agent-framework/agents/observability?pivots=programming-language-csharp) which is something like
```csharp
var agent = new ChatClientAgent(
instrumentedChatClient,
name: "OpenTelemetryDemoAgent",
instructions: "You are a helpful assistant that provides concise and informative responses.",
tools: [AIFunctionFactory.Create(GetWeatherAsync)]
)
.AsBuilder()
.UseOpenTelemetry(sourceName: SourceName, configure: (cfg) => cfg.EnableSensitiveData = true) // Enable OpenTelemetry instrumentation with sensitive data
.Build();
```
### Workaround
The ugly workaround is:
```csharp
// Built by hand rather than via AddAIAgent(name, instructions) so the agent can be wrapped for OpenTelemetry;
// IHostedAgentBuilder has no equivalent of AIAgentBuilder.UseOpenTelemetry.
var pirateAgent = builder.AddAIAgent(
name: "pirate",
createAgentDelegate: (sp, name) =>
{
AITool[] tools =
[
];
return new ChatClientAgent(
sp.GetRequiredService(),
name: "pirate",
instructions: "You are a pirate. Speak like a pirate",
tools: tools)
.AsBuilder()
.UseOpenTelemetry()
.Build();
})
```
### Language/SDK
.NET
Contributor guide
Assessment
This issue has not been assessed yet.