OneAgent-SDK-for-dotnet
OneAgent-SDK-for-dotnet copied to clipboard
How to trace IAsyncEnumerable calls
The current ITrace
API has Trace(Func<T> func)
and TraceAsync(Func<Task<T>> func)
, but none of these two can trace an IAsyncEnumerable
call. It would be possible to do it with the now-obsolete StartAsync
method, eg.:
_tracer.StartAsync();
try
{
await foreach (var item in items)
{
yield return item;
}
}
catch (Exception ex)
{
_tracer.Error(ex);
}
finally
{
_tracer.End();
}
Though I'm not even sure if this would be the best implementation, since it would also take into consideration the time the caller took to do whatever computation with the streamed items.
The current alternative is to just _tracer.Trace(MethodThatReturnsIAsyncEnumerable)
, but this also has its drawbacks, namely that it will only trace the call that starts the streaming, but will not track the streaming itself. For example, if an error occurs while streaming from the database, the tracer will not catch that.