Class OpenTelemetryWorkflowMiddleware
- Assembly
- WorkflowForge.Extensions.Observability.OpenTelemetry.dll
Creates the workflow-level span that operation spans nest under.
Register it on the smith with AddWorkflowMiddleware.
public sealed class OpenTelemetryWorkflowMiddleware : IWorkflowMiddleware
- Inheritance
-
OpenTelemetryWorkflowMiddleware
- Implements
- Inherited Members
Constructors
OpenTelemetryWorkflowMiddleware(WorkflowForgeOpenTelemetryService)
Initializes a new instance of the OpenTelemetryWorkflowMiddleware class.
public OpenTelemetryWorkflowMiddleware(WorkflowForgeOpenTelemetryService service)
Parameters
serviceWorkflowForgeOpenTelemetryServiceThe telemetry service that owns the activity source.
Exceptions
- ArgumentNullException
Thrown when service is null.
Methods
ExecuteAsync(IWorkflow, IWorkflowFoundry, Func<Task>, CancellationToken)
Executes the workflow middleware logic.
public Task ExecuteAsync(IWorkflow workflow, IWorkflowFoundry foundry, Func<Task> next, CancellationToken cancellationToken = default)
Parameters
workflowIWorkflowThe workflow being executed.
foundryIWorkflowFoundryThe foundry context for the workflow execution.
nextFunc<Task>Delegate to invoke the next middleware in the pipeline or the workflow execution itself. Call this to continue the workflow execution. Not calling this will short-circuit the workflow.
cancellationTokenCancellationTokenCancellation token for the workflow execution.
Returns
- Task
A task representing the workflow execution.
Remarks
Middleware should:
- Call
next() to continue workflow execution - Handle exceptions appropriately (log, transform, or rethrow)
- Clean up resources in finally blocks
- Respect cancellation tokens
Example implementation:
public async Task ExecuteAsync(
IWorkflow workflow,
IWorkflowFoundry foundry,
Func<Task> next,
CancellationToken cancellationToken)
{
// Pre-execution logic
Logger.LogInformation("Starting workflow: {Name}", workflow.Name);
try
{
// Execute workflow
await next().ConfigureAwait(false);
// Post-execution logic (success)
Logger.LogInformation("Workflow completed: {Name}", workflow.Name);
}
catch (Exception ex)
{
// Post-execution logic (failure)
Logger.LogError(ex, "Workflow failed: {Name}", workflow.Name);
throw;
}
}
Exceptions
- OperationCanceledException
Thrown when the workflow execution is cancelled.