Class PerformanceStatisticsMiddleware
- Namespace
- WorkflowForge.Extensions.Observability.Performance
- Assembly
- WorkflowForge.Extensions.Observability.Performance.dll
Operation middleware that records timing, success/failure, and approximate memory allocation for each operation into a FoundryPerformanceStatistics instance.
Added automatically by EnablePerformanceMonitoring(IWorkflowFoundry). In that mode the middleware resolves the active statistics from the foundry services on each call, so enabling/disabling monitoring is a matter of setting/removing that service — the middleware simply no-ops while no statistics object is present.
For standalone use, construct it with an explicit FoundryPerformanceStatistics
and add it via foundry.AddMiddleware(...); it will always record into that instance.
public sealed class PerformanceStatisticsMiddleware : IWorkflowOperationMiddleware
- Inheritance
-
PerformanceStatisticsMiddleware
- Implements
- Inherited Members
Constructors
PerformanceStatisticsMiddleware()
Initializes a middleware that resolves the active statistics from the foundry services
on each call (the mode used by EnablePerformanceMonitoring).
public PerformanceStatisticsMiddleware()
PerformanceStatisticsMiddleware(FoundryPerformanceStatistics)
Initializes a middleware that always records into the supplied statistics instance.
public PerformanceStatisticsMiddleware(FoundryPerformanceStatistics statistics)
Parameters
statisticsFoundryPerformanceStatisticsThe statistics accumulator to record into.
Exceptions
- ArgumentNullException
Thrown when
statisticsis null.
Methods
ExecuteAsync(IWorkflowOperation, IWorkflowFoundry, object?, Func<CancellationToken, Task<object?>>, CancellationToken)
Executes the operation middleware logic.
public Task<object?> ExecuteAsync(IWorkflowOperation operation, IWorkflowFoundry foundry, object? inputData, Func<CancellationToken, Task<object?>> next, CancellationToken cancellationToken = default)
Parameters
operationIWorkflowOperationThe operation being executed.
foundryIWorkflowFoundryThe foundry context providing properties, logger, and services.
inputDataobjectInput data passed to the operation.
nextFunc<CancellationToken, Task<object>>Delegate to invoke the next middleware in the pipeline or the operation itself. Call this to continue the operation execution. Not calling this will short-circuit the operation.
cancellationTokenCancellationTokenCancellation token for the operation execution.
Returns
- Task<object>
A task representing the operation execution result. The result can be null or any object depending on the operation.
Remarks
Middleware should:
- Call
next(cancellationToken) to continue operation execution - Handle exceptions appropriately (log, transform, or rethrow)
- Clean up resources in finally blocks
- Respect cancellation tokens
- Preserve operation results unless intentionally transforming them
Example implementation:
public async Task<object?> ExecuteAsync(
IWorkflowOperation operation,
IWorkflowFoundry foundry,
object? inputData,
Func<CancellationToken, Task<object?>> next,
CancellationToken cancellationToken)
{
// Pre-execution logic
Logger.LogInformation("Starting operation: {Name}", operation.Name);
try
{
// Execute operation
var result = await next(cancellationToken).ConfigureAwait(false);
// Post-execution logic (success)
Logger.LogInformation("Operation completed: {Name}", operation.Name);
return result;
}
catch (Exception ex)
{
// Post-execution logic (failure)
Logger.LogError(ex, "Operation failed: {Name}", operation.Name);
throw;
}
}
Exceptions
- OperationCanceledException
Thrown when the operation execution is cancelled.