diff --git a/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs b/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs index d51414769f..5eac701958 100644 --- a/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs +++ b/dotnet/src/Microsoft.Agents.AI/OpenTelemetryAgent.cs @@ -191,20 +191,22 @@ public sealed class OpenTelemetryAgent : DelegatingAIAgent, IDisposable return options; } - // The auto-wiring only applies when the inner agent is a ChatClientAgent. Otherwise, no-op. - if (this.InnerAgent.GetService() is null) + // The auto-wiring only applies when a ChatClientAgent is reachable from the inner agent. Otherwise, no-op. + // Use GetService rather than a type check so wrapping agents that expose a nested ChatClientAgent are supported. + var chatClientAgent = this.InnerAgent.GetService(); + if (chatClientAgent is null) { return options; } // Respect ChatClientAgentOptions.UseProvidedChatClientAsIs: don't decorate the chat client when the user opted out. - if (this.InnerAgent.GetService()?.UseProvidedChatClientAsIs is true) + if (chatClientAgent.GetService()?.UseProvidedChatClientAsIs is true) { return options; } // Capture the underlying IChatClient and check whether it is already instrumented. - var chatClient = this.InnerAgent.GetService(); + var chatClient = chatClientAgent.GetService(); if (chatClient is null || chatClient.GetService(typeof(OpenTelemetryChatClient)) is not null) { return options;