fix(common): classify request cancellation in profiler
Preserve legacy profiler contracts and record expected cancellation below error level.
This commit is contained in:
@@ -6,12 +6,26 @@ using Serilog;
|
||||
public interface IProfiler
|
||||
{
|
||||
Task<T> MeasureExecutionAsync<T>(Func<Task<T>> function, string blockName = "");
|
||||
Task<T> MeasureExecutionAsync<T>(Func<Task<T>> function, string blockName, CancellationToken cancellationToken)
|
||||
{
|
||||
return MeasureExecutionAsync(function, blockName);
|
||||
}
|
||||
|
||||
Task MeasureExecutionAsync(Func<Task> action, string blockName = "");
|
||||
Task MeasureExecutionAsync(Func<Task> action, string blockName, CancellationToken cancellationToken)
|
||||
{
|
||||
return MeasureExecutionAsync(action, blockName);
|
||||
}
|
||||
}
|
||||
|
||||
public class Profiler(ILogger logger) : IProfiler
|
||||
{
|
||||
public async Task<T> MeasureExecutionAsync<T>(Func<Task<T>> function, string blockName = "")
|
||||
public Task<T> MeasureExecutionAsync<T>(Func<Task<T>> function, string blockName = "")
|
||||
{
|
||||
return MeasureExecutionAsync(function, blockName, CancellationToken.None);
|
||||
}
|
||||
|
||||
public async Task<T> MeasureExecutionAsync<T>(Func<Task<T>> function, string blockName, CancellationToken cancellationToken)
|
||||
{
|
||||
logger.ForContext("PerformanceLog", true).Information("{BlockName} - Start", blockName);
|
||||
|
||||
@@ -42,6 +56,13 @@ public class Profiler(ILogger logger) : IProfiler
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
logger
|
||||
.ForContext("PerformanceLog", true)
|
||||
.Debug("{BlockName} - Canceled", blockName);
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex) // NOSONAR
|
||||
{
|
||||
logger
|
||||
@@ -51,12 +72,17 @@ public class Profiler(ILogger logger) : IProfiler
|
||||
}
|
||||
}
|
||||
|
||||
public async Task MeasureExecutionAsync(Func<Task> action, string blockName = "")
|
||||
public Task MeasureExecutionAsync(Func<Task> action, string blockName = "")
|
||||
{
|
||||
return MeasureExecutionAsync(action, blockName, CancellationToken.None);
|
||||
}
|
||||
|
||||
public async Task MeasureExecutionAsync(Func<Task> action, string blockName, CancellationToken cancellationToken)
|
||||
{
|
||||
await MeasureExecutionAsync<object?>(async () =>
|
||||
{
|
||||
await action();
|
||||
return null;
|
||||
}, blockName);
|
||||
}, blockName, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user