From bdb5ad89065cb583ca9792a487894976c2c533b4 Mon Sep 17 00:00:00 2001 From: Jesse Liberty Date: Fri, 31 Jul 2026 11:07:33 -0400 Subject: [PATCH] Added UseOpenTelemetry --- AuthorAgent.cs | 7 +++++-- BloggerAgent.cs | 7 +++++-- Program.cs | 17 ++++++++++++----- ResearcherAgent.cs | 7 +++++-- ReviewerAgent.cs | 7 +++++-- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/AuthorAgent.cs b/AuthorAgent.cs index d8b66d6..5c35afd 100644 --- a/AuthorAgent.cs +++ b/AuthorAgent.cs @@ -13,7 +13,7 @@ namespace BlogWriter; /// public class AuthorAgent : IAuthorAgent { - private readonly ChatClientAgent _agent; + private readonly AIAgent _agent; // Emits a span per draft creation/revision. Activated by the ActivityListener // registered in Program.cs (or an OpenTelemetry TracerProvider). @@ -38,7 +38,10 @@ public AuthorAgent(IChatClient llm, ChatOptions chatOptions, ILogger new TokenCapChatClient(inner, maxTotalTokens: 10000)) .Build(); @@ -90,9 +96,10 @@ var app = new BlogWorkflow(bloggerAgent, researcherAgent, authorAgent, reviewerAgent); // Distributed tracing: an ActivityListener activates every "BlogWriter.*" -// ActivitySource in the app (agents + workflow) and writes span start/stop to -// the console. Swap this listener for OpenTelemetry's TracerProvider (and chain -// IChatClient.UseOpenTelemetry() above) to export the same spans instead. +// ActivitySource in the app (agents, workflow, and the IChatClient's +// "BlogWriter.ChatClient" GenAI spans) and writes span start/stop to the +// console. Swap this listener for OpenTelemetry's TracerProvider to export the +// same spans to a backend instead. var appActivitySource = new ActivitySource("BlogWriter.Program"); ActivitySource.AddActivityListener(new ActivityListener diff --git a/ResearcherAgent.cs b/ResearcherAgent.cs index 0d88fa4..eda3a41 100644 --- a/ResearcherAgent.cs +++ b/ResearcherAgent.cs @@ -17,7 +17,7 @@ public class ResearcherAgent : IResearcherAgent // The agent is built once and reused for every research turn. It is stateless // across turns (no AgentSession is retained), which matches the original // per-call behaviour while gaining tool-calling for free. - private readonly ChatClientAgent _agent; + private readonly AIAgent _agent; // Emits a span per research turn. Activated by the ActivityListener // registered in Program.cs (or an OpenTelemetry TracerProvider). @@ -48,7 +48,10 @@ public ResearcherAgent(IChatClient llm, ChatOptions chatOptions, AIFunction tavi // Attaching the tool lets the model call it autonomously. Tools = [tavilyTool], }, - }); + }) + .AsBuilder() + .UseOpenTelemetry(sourceName: "BlogWriter.Agents") + .Build(); _logger.LogInformation("ResearcherAgent initialized with Tavily tool: {ToolName}", tavilyTool.Name); } diff --git a/ReviewerAgent.cs b/ReviewerAgent.cs index c55b78c..b0a8d47 100644 --- a/ReviewerAgent.cs +++ b/ReviewerAgent.cs @@ -14,7 +14,7 @@ namespace BlogWriter; /// public class ReviewerAgent : IReviewerAgent { - private readonly ChatClientAgent _agent; + private readonly AIAgent _agent; // Emits a span per review. Activated by the ActivityListener registered in // Program.cs (or an OpenTelemetry TracerProvider). @@ -39,7 +39,10 @@ public ReviewerAgent(IChatClient llm, ChatOptions chatOptions, ILogger