prometheus-net

Форк
0
/
CounterExtensions.cs 
116 строк · 4.2 Кб
1
namespace Prometheus;
2

3
public 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>
10
    public static void IncToCurrentTimeUtc(this ICounter counter)
11
    {
12
        counter.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>
20
    public static void IncToTimeUtc(this ICounter counter, DateTimeOffset timestamp)
21
    {
22
        counter.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>
29
    public static void CountExceptions(this ICounter counter, Action wrapped, Func<Exception, bool>? exceptionFilter = null)
30
    {
31
        if (counter == null)
32
            throw new ArgumentNullException(nameof(counter));
33

34
        if (wrapped == null)
35
            throw new ArgumentNullException(nameof(wrapped));
36

37
        try
38
        {
39
            wrapped();
40
        }
41
        catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
42
        {
43
            counter.Inc();
44
            throw;
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>
52
    public static TResult CountExceptions<TResult>(this ICounter counter, Func<TResult> wrapped, Func<Exception, bool>? exceptionFilter = null)
53
    {
54
        if (counter == null)
55
            throw new ArgumentNullException(nameof(counter));
56

57
        if (wrapped == null)
58
            throw new ArgumentNullException(nameof(wrapped));
59

60
        try
61
        {
62
            return wrapped();
63
        }
64
        catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
65
        {
66
            counter.Inc();
67
            throw;
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>
75
    public static async Task CountExceptionsAsync(this ICounter counter, Func<Task> wrapped, Func<Exception, bool>? exceptionFilter = null)
76
    {
77
        if (counter == null)
78
            throw new ArgumentNullException(nameof(counter));
79

80
        if (wrapped == null)
81
            throw new ArgumentNullException(nameof(wrapped));
82

83
        try
84
        {
85
            await wrapped().ConfigureAwait(false);
86
        }
87
        catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
88
        {
89
            counter.Inc();
90
            throw;
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>
98
    public static async Task<TResult> CountExceptionsAsync<TResult>(this ICounter counter, Func<Task<TResult>> wrapped, Func<Exception, bool>? exceptionFilter = null)
99
    {
100
        if (counter == null)
101
            throw new ArgumentNullException(nameof(counter));
102

103
        if (wrapped == null)
104
            throw new ArgumentNullException(nameof(wrapped));
105

106
        try
107
        {
108
            return await wrapped().ConfigureAwait(false);
109
        }
110
        catch (Exception ex) when (exceptionFilter == null || exceptionFilter(ex))
111
        {
112
            counter.Inc();
113
            throw;
114
        }
115
    }
116
}
117

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.