Skip to main content

How to Trace a Microsoft Agent Framework Workflow with OpenTelemetry in C#

· 11 min read
Jagdish Kumawat
Founder @ Dewiride

WithOpenTelemetry turns on tracing for a Microsoft Agent Framework workflow: each run becomes a trace, with a span for the run, one for each executor and one for each message they send. Here a two-step workflow converts 21.5 °C to Fahrenheit, and its trace in the Aspire Dashboard shows where the run's 22.85 ms went.

In OpenTelemetry, a trace follows one operation, and each span in it is one timed piece of work with a name and attributes. A workflow creates no spans until you ask it to. The documentation of WithOpenTelemetry in Agent Framework 1.28.0 says so plainly: By default, workflow telemetry is disabled.

Syntax​

Syntax
using TracerProvider <tracerProvider> = Sdk.CreateTracerProviderBuilder()
.AddSource("Microsoft.Agents.AI.Workflows")
.AddOtlpExporter(otlp => otlp.Endpoint = new Uri("<otlpEndpoint>"))
.Build();

Workflow <workflow> = new WorkflowBuilder(<startExecutor>)
.WithName("<workflowName>")
// <the workflow's edges and outputs>
.WithOpenTelemetry()
.Build();
PartWhat it is
<tracerProvider>OpenTelemetry's tracer provider. It collects the spans of the sources you add and hands them to the exporter
AddSource("Microsoft.Agents.AI.Workflows")The source a workflow creates its spans on, unless you give it another one. The provider collects nothing from a source it hasn't added
<otlpEndpoint>Where the OTLP exporter sends the spans. Optional: without it, the exporter uses gRPC and localhost:4317
<workflow>The workflow that Build() returns
<startExecutor>The executor that receives the run's input
<workflowName>Optional. The workflow's name, which becomes part of the run's span name
WithOpenTelemetry()Turns the workflow's telemetry on. Its two optional arguments are configure, to switch off single kinds of span, and activitySource, to use a source of your own

The documentation of WithOpenTelemetry names the default source: without an activitySource, a shared default activity source named "Microsoft.Agents.AI.Workflows" will be used. That's the name AddSource needs. Microsoft's observability guide explains why: The AddSource method is used to specify the source name which the provider will listen to.

The Example​

A console app called TemperatureConverter has a workflow of two steps, each a plain C# function turned into an executor: ParseCelsius turns the text 21.5 into a number, and ToFahrenheit converts it. The workflow is named temperature-converter, and its spans go to the Aspire Dashboard, a local viewer for OpenTelemetry data. Nothing in it calls a model, so running it costs nothing.

Prerequisites​

Step 1: Create a Console App and Add the Packages​

Terminal
dotnet new console -o TemperatureConverter
cd TemperatureConverter
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/TemperatureConverter/TemperatureConverter.csproj:
Restore succeeded.

dotnet new console creates a console app in a new folder, TemperatureConverter, and cd moves into it. Then add three packages:

Terminal
dotnet add package Microsoft.Agents.AI.Workflows --version 1.28.0
dotnet add package OpenTelemetry --version 1.21.0
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.21.0

Each command adds a package reference to the project and restores it. The first one ends like this:

Output (the last lines of the first command)
info : PackageReference for package 'Microsoft.Agents.AI.Workflows' version '1.28.0' added to file '/Users/you/source/TemperatureConverter/TemperatureConverter.csproj'.
info : Generating MSBuild file /Users/you/source/TemperatureConverter/obj/TemperatureConverter.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: /Users/you/source/TemperatureConverter/obj/project.assets.json
log : Restored /Users/you/source/TemperatureConverter/TemperatureConverter.csproj (in 1.22 sec).
  • Microsoft.Agents.AI.Workflows is Agent Framework's workflow library: executors, edges, WorkflowBuilder and WithOpenTelemetry.
  • OpenTelemetry is the OpenTelemetry SDK for .NET, with the tracer provider.
  • OpenTelemetry.Exporter.OpenTelemetryProtocol is the OTLP exporter, which sends the spans to anything that receives OTLP, such as the Aspire Dashboard.

The project file now lists all three:

TemperatureConverter.csproj
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Agents.AI.Workflows" Version="1.28.0" />
<PackageReference Include="OpenTelemetry" Version="1.21.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.21.0" />
</ItemGroup>

</Project>

Step 2: Collect the Workflow's Spans​

Replace everything in Program.cs with the tracer provider:

Program.cs (the tracer provider)
using Microsoft.Agents.AI.Workflows;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

// Collect the workflow's traces and send them over OTLP, to the Aspire Dashboard on this machine.
using TracerProvider tracing = Sdk.CreateTracerProviderBuilder()
.ConfigureResource(resource => resource.AddService("TemperatureConverter"))
.AddSource("Microsoft.Agents.AI.Workflows")
.AddOtlpExporter(otlp => otlp.Endpoint = new Uri("http://localhost:4317"))
.Build();
  • AddService("TemperatureConverter") names the app, so the dashboard lists its traces under TemperatureConverter.
  • AddSource("Microsoft.Agents.AI.Workflows") listens to the source the workflow will create its spans on.
  • AddOtlpExporter(...) sends the spans over OTLP to http://localhost:4317, where the dashboard receives them.
  • using disposes the provider when the program ends. That's the life OpenTelemetry's documentation describes for it: In a typical application, a single TracerProvider is created at application startup and disposed at application shutdown.

Step 3: Build the Workflow with Telemetry On​

Add the two steps and the workflow below the tracer provider:

Program.cs (the workflow)
Func<string, double> parse = text => double.Parse(text);
Func<double, double> toFahrenheit = celsius => celsius * 9 / 5 + 32;

ExecutorBinding parseCelsius = parse.BindAsExecutor("ParseCelsius");
ExecutorBinding convert = toFahrenheit.BindAsExecutor("ToFahrenheit");

Workflow workflow = new WorkflowBuilder(parseCelsius)
.WithName("temperature-converter")
.AddEdge(parseCelsius, convert)
.WithOutputFrom(convert)
.WithOpenTelemetry()
.Build();
  • parse and toFahrenheit are ordinary functions: text to a number, and Celsius to Fahrenheit.
  • BindAsExecutor("ParseCelsius") turns a function into an executor with that ID. The IDs are what the spans show.
  • WithName("temperature-converter") names the workflow.
  • AddEdge(parseCelsius, convert) sends the result of ParseCelsius on to ToFahrenheit, and WithOutputFrom(convert) makes the result of ToFahrenheit the workflow's output.
  • WithOpenTelemetry() turns the workflow's telemetry on, on the default source the tracer provider listens to.

Step 4: Run the Workflow​

Add the run at the end of the file:

Program.cs (the run)
await using Run run = await InProcessExecution.RunAsync(workflow, "21.5");

foreach (WorkflowEvent workflowEvent in run.OutgoingEvents)
{
if (workflowEvent is WorkflowOutputEvent output)
{
Console.WriteLine($"21.5 °C is {output.Data} °F");
}
}
  • InProcessExecution.RunAsync(workflow, "21.5") runs the workflow in this process with the input 21.5, and returns when the run is done.
  • run.OutgoingEvents holds the events the run raised. The WorkflowOutputEvent among them carries the result of ToFahrenheit.

Run the app:

Terminal
dotnet run
Output
21.5 °C is 70.7 °F

macOS-style terminal window titled Terminal — TemperatureConverter showing dotnet run printing 21.5 °C is 70.7 °F

The app printed the result and exited. Its spans went to the dashboard.

Step 5: Find the Run in the Dashboard​

In the Aspire Dashboard, open Traces and select TemperatureConverter among the telemetry sources:

The Traces page of the Aspire Dashboard for TemperatureConverter, listing two traces: TemperatureConverter: workflow.build with 1 span and a duration of 33.64ms, and TemperatureConverter: workflow.session with 8 spans and a duration of 22.85ms

The run left two traces, each named after the app and its top span:

  • workflow.build, 1 span, 33.64 ms: Build() building the workflow.
  • workflow.session, 8 spans, 22.85 ms: the run itself.

Building and running are separate operations, so each got a trace of its own.

Step 6: Read the Run's Spans​

Select the workflow.session trace to see its spans on a timeline:

The workflow.session trace of TemperatureConverter in the Aspire Dashboard, showing 8 spans on a timeline of 22.85ms: invoke_workflow temperature-converter taking 16.94ms, an edge_group.process of 2ms, executor.process ParseCelsius taking 5.68ms with a message.send of 2.19ms and an edge_group.process of 0.13ms inside it, and executor.process ToFahrenheit taking 1.98ms with a message.send of 0.02ms

  • workflow.session, 22.85 ms: the run from start to finish. The 1.28.0 documentation calls it the outer/parent span that represents the entire lifetime of a workflow execution.
  • invoke_workflow temperature-converter, 16.94 ms: the run's one input-to-halt cycle, named after the workflow.
  • edge_group.process, 2 ms, before ParseCelsius starts.
  • executor.process ParseCelsius, 5.68 ms: ParseCelsius handling the input. Inside it, a message.send of 2.19 ms sends the number on, and inside that, an edge_group.process of 0.13 ms.
  • executor.process ToFahrenheit, 1.98 ms: ToFahrenheit handling the number, with a message.send of 0.02 ms for its result.

The two steps themselves took 7.66 ms of the run's 22.85 ms.

ToFahrenheit doesn't sit inside the message.send that brought it the number. Microsoft's Workflows observability page explains that the target executor's span isn't a child of that message.send, because the execution isn't nested: Instead, the executor.process span of the target executor is linked to the message.send span of the source executor.

Step 7: Read the Workflow Span's Attributes​

Select invoke_workflow temperature-converter to open its details:

The Span section of the details pane for invoke_workflow temperature-converter in the Aspire Dashboard: Kind Internal, gen_ai.operation.name invoke_workflow, gen_ai.workflow.name temperature-converter, a session.id, and workflow.id ParseCelsius

  • gen_ai.operation.name is invoke_workflow.
  • gen_ai.workflow.name is temperature-converter, the name from WithName.
  • session.id identifies the run's session.
  • workflow.id is ParseCelsius, the ID of the executor the workflow starts with.

The same details list the span's two events, workflow.started and workflow.completed, and its source, Microsoft.Agents.AI.Workflows.

One thing to know when you read Microsoft's documentation: the C# tab of the Workflows observability page calls this span workflow_invoke. In 1.28.0 it's invoke_workflow followed by the workflow's name, as the package's own documentation describes it, invoke_workflow {gen_ai.workflow.name}, and as the dashboard showed.

The Complete File​

Program.cs
using Microsoft.Agents.AI.Workflows;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

// Collect the workflow's traces and send them over OTLP, to the Aspire Dashboard on this machine.
using TracerProvider tracing = Sdk.CreateTracerProviderBuilder()
.ConfigureResource(resource => resource.AddService("TemperatureConverter"))
.AddSource("Microsoft.Agents.AI.Workflows")
.AddOtlpExporter(otlp => otlp.Endpoint = new Uri("http://localhost:4317"))
.Build();

Func<string, double> parse = text => double.Parse(text);
Func<double, double> toFahrenheit = celsius => celsius * 9 / 5 + 32;

ExecutorBinding parseCelsius = parse.BindAsExecutor("ParseCelsius");
ExecutorBinding convert = toFahrenheit.BindAsExecutor("ToFahrenheit");

Workflow workflow = new WorkflowBuilder(parseCelsius)
.WithName("temperature-converter")
.AddEdge(parseCelsius, convert)
.WithOutputFrom(convert)
.WithOpenTelemetry()
.Build();

await using Run run = await InProcessExecution.RunAsync(workflow, "21.5");

foreach (WorkflowEvent workflowEvent in run.OutgoingEvents)
{
if (workflowEvent is WorkflowOutputEvent output)
{
Console.WriteLine($"21.5 °C is {output.Data} °F");
}
}

macOS-style code window titled Program.cs — TemperatureConverter showing a tracer provider that adds the Microsoft.Agents.AI.Workflows source and exports over OTLP to localhost port 4317, two functions bound as the executors ParseCelsius and ToFahrenheit, a workflow named temperature-converter built with WithOpenTelemetry, and a run with the input 21.5 that prints the output

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out AddSource("Microsoft.Agents.AI.Workflows")The app still prints 21.5 °C is 70.7 °F, but no trace reaches the dashboard. I ran it without that line to checkAdd the source to the tracer provider
Leaving out WithOpenTelemetry()The workflow creates no spans: its telemetry is off by defaultCall it on the builder, before Build()
Passing your own activitySource but adding the default nameThe spans go to your source, which the provider doesn't listen toAdd your source's name with AddSource
Leaving out WithNameThe run's span is just invoke_workflow, whatever the workflowName each workflow
Looking for a workflow_invoke span, as the C# docs call itThere's no such span in 1.28.0Look for invoke_workflow and the workflow's name

FAQ​

Why does one run make two traces?​

Because building the workflow and running it are two operations, each with a root span of its own: workflow.build when Build() runs, and workflow.session for the run. You can switch the build spans off with the configure callback of WithOpenTelemetry: the 1.28.0 documentation of DisableWorkflowBuild says true disables workflow.build activities.

Do the spans show what each executor received and returned?​

No. In 1.28.0, WorkflowTelemetryOptions.EnableSensitiveData is a legacy option that changes nothing, and its documentation says Workflow payload attributes are no longer emitted. Microsoft's Workflows observability page still describes the option as adding inputs and outputs, but the 1.28.0 package's own documentation says its value is ignored.

Conclusion​

To trace a Microsoft Agent Framework workflow in C#, call WithOpenTelemetry() on its builder and add Microsoft.Agents.AI.Workflows to your tracer provider with AddSource. TemperatureConverter's run became a workflow.session trace of 8 spans in 22.85 ms, with an invoke_workflow temperature-converter span, one executor.process span for each step and a message.send for each message, and building the workflow became a separate workflow.build trace.

Additional Resources​