Table of Contents

Class OpenTelemetryWorkflowMiddleware

Namespace
WorkflowForge.Extensions.Observability.OpenTelemetry
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

service WorkflowForgeOpenTelemetryService

The 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

workflow IWorkflow

The workflow being executed.

foundry IWorkflowFoundry

The foundry context for the workflow execution.

next Func<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.

cancellationToken CancellationToken

Cancellation 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.