prometheus-net

Форк
0
/
HttpClientMetricsExtensions.cs 
100 строк · 3.4 Кб
1
using Microsoft.Extensions.DependencyInjection;
2
using Microsoft.Extensions.Http;
3
using Prometheus.HttpClientMetrics;
4

5
namespace Prometheus;
6

7
public static class HttpClientMetricsExtensions
8
{
9
    /// <summary>
10
    /// Configures the HttpClient pipeline to collect Prometheus metrics.
11
    /// </summary>
12
    public static IHttpClientBuilder UseHttpClientMetrics(this IHttpClientBuilder builder, Action<HttpClientExporterOptions> configure)
13
    {
14
        var options = new HttpClientExporterOptions();
15

16
        configure?.Invoke(options);
17

18
        builder.UseHttpClientMetrics(options);
19

20
        return builder;
21
    }
22

23
    /// <summary>
24
    /// Configures the HttpClient pipeline to collect Prometheus metrics.
25
    /// </summary>
26
    public static IHttpClientBuilder UseHttpClientMetrics(this IHttpClientBuilder builder, HttpClientExporterOptions? options = null)
27
    {
28
        options ??= new HttpClientExporterOptions();
29

30
        var identity = new HttpClientIdentity(builder.Name);
31

32
        if (options.InProgress.Enabled)
33
        {
34
            builder = builder.AddHttpMessageHandler(x => new HttpClientInProgressHandler(options.InProgress, identity));
35
        }
36

37
        if (options.RequestCount.Enabled)
38
        {
39
            builder = builder.AddHttpMessageHandler(x => new HttpClientRequestCountHandler(options.RequestCount, identity));
40
        }
41

42
        if (options.RequestDuration.Enabled)
43
        {
44
            builder = builder.AddHttpMessageHandler(x => new HttpClientRequestDurationHandler(options.RequestDuration, identity));
45
        }
46

47
        if (options.ResponseDuration.Enabled)
48
        {
49
            builder = builder.AddHttpMessageHandler(x => new HttpClientResponseDurationHandler(options.ResponseDuration, identity));
50
        }
51

52
        return builder;
53
    }
54

55
    /// <summary>
56
    /// Configures the HttpMessageHandler pipeline to collect Prometheus metrics.
57
    /// </summary>
58
    public static HttpMessageHandlerBuilder UseHttpClientMetrics(this HttpMessageHandlerBuilder builder, HttpClientExporterOptions? options = null)
59
    {
60
        options ??= new HttpClientExporterOptions();
61

62
        var identity = new HttpClientIdentity(builder.Name);
63

64
        if (options.InProgress.Enabled)
65
        {
66
            builder.AdditionalHandlers.Add(new HttpClientInProgressHandler(options.InProgress, identity));
67
        }
68

69
        if (options.RequestCount.Enabled)
70
        {
71
            builder.AdditionalHandlers.Add(new HttpClientRequestCountHandler(options.RequestCount, identity));
72
        }
73

74
        if (options.RequestDuration.Enabled)
75
        {
76
            builder.AdditionalHandlers.Add(new HttpClientRequestDurationHandler(options.RequestDuration, identity));
77
        }
78

79
        if (options.ResponseDuration.Enabled)
80
        {
81
            builder.AdditionalHandlers.Add(new HttpClientResponseDurationHandler(options.ResponseDuration, identity));
82
        }
83

84
        return builder;
85
    }
86

87
    /// <summary>
88
    /// Configures the service container to collect Prometheus metrics from all registered HttpClients.
89
    /// </summary>
90
    public static IServiceCollection UseHttpClientMetrics(this IServiceCollection services, HttpClientExporterOptions? options = null)
91
    {
92
        return services.ConfigureAll((HttpClientFactoryOptions optionsToConfigure) =>
93
        {
94
            optionsToConfigure.HttpMessageHandlerBuilderActions.Add(builder =>
95
            {
96
                builder.UseHttpClientMetrics(options); 
97
            });
98
        });
99
    }
100
}

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

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

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

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