Created
September 10, 2021 03:01
-
-
Save bruno-garcia/d402ef6f24a951058cd5dfed43cabb7b to your computer and use it in GitHub Desktop.
sentry measure IEnumerator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// measure enumeration: | |
//Where each enumeration is a result of a potentially coslty operation such as a DB cursor read | |
foreach (var group in groups.Measure()) | |
{ | |
// work | |
} | |
//Code: | |
static class MeasureEnumerableExtensions | |
{ | |
public static IEnumerable<IList<string>> Measure(this IEnumerable<IList<string>> enumerable) | |
=> new MeasureEnumerable(enumerable); | |
} | |
class MeasureEnumerable : IEnumerable<IList<string>> | |
{ | |
private readonly IEnumerable<IList<string>> _inner; | |
public MeasureEnumerable(IEnumerable<IList<string>> inner) => _inner = inner; | |
public IEnumerator<IList<string>> GetEnumerator() => new MeasureEnumerator(_inner.GetEnumerator()); | |
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | |
class MeasureEnumerator : IEnumerator<IList<string>> | |
{ | |
private readonly IEnumerator<IList<string>> _inner; | |
public MeasureEnumerator(IEnumerator<IList<string>> inner) | |
{ | |
_inner = inner; | |
} | |
public bool MoveNext() | |
{ | |
var next = SentrySdk.GetSpan()?.StartChild("MoveNext"); | |
var result = _inner.MoveNext(); | |
next?.SetExtra("result", result.ToString()); | |
next?.Finish(); | |
return result; | |
} | |
public void Reset() => _inner.Reset(); | |
public IList<string> Current => _inner.Current; | |
object IEnumerator.Current => Current; | |
public void Dispose() => _inner.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment