prometheus-net
116 строк · 4.2 Кб
1namespace Prometheus;
2
3public static class CounterExtensions
4{
5/// <summary>
6/// Increments the value of the counter to the current UTC time as a Unix timestamp in seconds.
7/// Value does not include any elapsed leap seconds because Unix timestamps do not include leap seconds.
8/// Operation is ignored if the current value is already greater.
9/// </summary>
10public static void IncToCurrentTimeUtc(this ICounter counter)
11{
12counter.IncTo(LowGranularityTimeSource.GetSecondsFromUnixEpoch());
13}
14
15/// <summary>
16/// Increments the value of the counter to a specific moment as the UTC Unix timestamp in seconds.
17/// Value does not include any elapsed leap seconds because Unix timestamps do not include leap seconds.
18/// Operation is ignored if the current value is already greater.
19/// </summary>
20public static void IncToTimeUtc(this ICounter counter, DateTimeOffset timestamp)
21{
22counter.IncTo(TimestampHelpers.ToUnixTimeSecondsAsDouble(timestamp));
23}
24
25/// <summary>
26/// Executes the provided operation and increments the counter if an exception occurs. The exception is re-thrown.
27/// If an exception filter is specified, only counts exceptions for which the filter returns true.
28/// </summary>
29public static void CountExceptions(this ICounter counter, Action wrapped, Func<Exception, bool>? exceptionFilter = null)
30{
31if (counter == null)
32throw new ArgumentNullException(nameof(counter));
33
34if (wrapped == null)
35throw new ArgumentNullException(nameof(wrapped));
36
37try
38{
39wrapped();
40}
41catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
42{
43counter.Inc();
44throw;
45}
46}
47
48/// <summary>
49/// Executes the provided operation and increments the counter if an exception occurs. The exception is re-thrown.
50/// If an exception filter is specified, only counts exceptions for which the filter returns true.
51/// </summary>
52public static TResult CountExceptions<TResult>(this ICounter counter, Func<TResult> wrapped, Func<Exception, bool>? exceptionFilter = null)
53{
54if (counter == null)
55throw new ArgumentNullException(nameof(counter));
56
57if (wrapped == null)
58throw new ArgumentNullException(nameof(wrapped));
59
60try
61{
62return wrapped();
63}
64catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
65{
66counter.Inc();
67throw;
68}
69}
70
71/// <summary>
72/// Executes the provided async operation and increments the counter if an exception occurs. The exception is re-thrown.
73/// If an exception filter is specified, only counts exceptions for which the filter returns true.
74/// </summary>
75public static async Task CountExceptionsAsync(this ICounter counter, Func<Task> wrapped, Func<Exception, bool>? exceptionFilter = null)
76{
77if (counter == null)
78throw new ArgumentNullException(nameof(counter));
79
80if (wrapped == null)
81throw new ArgumentNullException(nameof(wrapped));
82
83try
84{
85await wrapped().ConfigureAwait(false);
86}
87catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
88{
89counter.Inc();
90throw;
91}
92}
93
94/// <summary>
95/// Executes the provided async operation and increments the counter if an exception occurs. The exception is re-thrown.
96/// If an exception filter is specified, only counts exceptions for which the filter returns true.
97/// </summary>
98public static async Task<TResult> CountExceptionsAsync<TResult>(this ICounter counter, Func<Task<TResult>> wrapped, Func<Exception, bool>? exceptionFilter = null)
99{
100if (counter == null)
101throw new ArgumentNullException(nameof(counter));
102
103if (wrapped == null)
104throw new ArgumentNullException(nameof(wrapped));
105
106try
107{
108return await wrapped().ConfigureAwait(false);
109}
110catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
111{
112counter.Inc();
113throw;
114}
115}
116}
117