How to Trace a Microsoft Agent Framework Workflow with OpenTelemetry in C#
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
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();
| Part | What 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
- The .NET 10 SDK.
- The Aspire Dashboard, running on your computer and receiving OTLP at
http://localhost:4317. The standalone Aspire Dashboard documentation shows how to start it.
Step 1: Create a Console App and Add the Packages
dotnet new console -o TemperatureConverter
cd TemperatureConverter
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:
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:
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.Workflowsis Agent Framework's workflow library: executors, edges,WorkflowBuilderandWithOpenTelemetry.OpenTelemetryis the OpenTelemetry SDK for .NET, with the tracer provider.OpenTelemetry.Exporter.OpenTelemetryProtocolis the OTLP exporter, which sends the spans to anything that receives OTLP, such as the Aspire Dashboard.
The project file now lists all three:
<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:
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 tohttp://localhost:4317, where the dashboard receives them.usingdisposes 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:
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();
parseandtoFahrenheitare 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 ofParseCelsiuson toToFahrenheit, andWithOutputFrom(convert)makes the result ofToFahrenheitthe 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:
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 input21.5, and returns when the run is done.run.OutgoingEventsholds the events the run raised. TheWorkflowOutputEventamong them carries the result ofToFahrenheit.
Run the app:
dotnet run
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 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:

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, beforeParseCelsiusstarts.executor.process ParseCelsius, 5.68 ms:ParseCelsiushandling the input. Inside it, amessage.sendof 2.19 ms sends the number on, and inside that, anedge_group.processof 0.13 ms.executor.process ToFahrenheit, 1.98 ms:ToFahrenheithandling the number, with amessage.sendof 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:

gen_ai.operation.nameisinvoke_workflow.gen_ai.workflow.nameistemperature-converter, the name fromWithName.session.ididentifies the run's session.workflow.idisParseCelsius, 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
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");
}
}

Common Mistakes
| Mistake | What happens | Do 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 check | Add the source to the tracer provider |
Leaving out WithOpenTelemetry() | The workflow creates no spans: its telemetry is off by default | Call it on the builder, before Build() |
Passing your own activitySource but adding the default name | The spans go to your source, which the provider doesn't listen to | Add your source's name with AddSource |
Leaving out WithName | The run's span is just invoke_workflow, whatever the workflow | Name each workflow |
Looking for a workflow_invoke span, as the C# docs call it | There's no such span in 1.28.0 | Look 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
- Microsoft Agent Framework Workflows - Observability — span names, attributes, events and
WorkflowTelemetryOptions. - Agent Framework observability — tracer providers,
AddSourceand the Aspire Dashboard. - Standalone Aspire Dashboard — running the dashboard on its own.
- OTLP exporter for OpenTelemetry .NET — the exporter's options and their defaults.
