prometheus-net
100 строк · 3.4 Кб
1using Microsoft.Extensions.DependencyInjection;2using Microsoft.Extensions.Http;3using Prometheus.HttpClientMetrics;4
5namespace Prometheus;6
7public static class HttpClientMetricsExtensions8{
9/// <summary>10/// Configures the HttpClient pipeline to collect Prometheus metrics.11/// </summary>12public static IHttpClientBuilder UseHttpClientMetrics(this IHttpClientBuilder builder, Action<HttpClientExporterOptions> configure)13{14var options = new HttpClientExporterOptions();15
16configure?.Invoke(options);17
18builder.UseHttpClientMetrics(options);19
20return builder;21}22
23/// <summary>24/// Configures the HttpClient pipeline to collect Prometheus metrics.25/// </summary>26public static IHttpClientBuilder UseHttpClientMetrics(this IHttpClientBuilder builder, HttpClientExporterOptions? options = null)27{28options ??= new HttpClientExporterOptions();29
30var identity = new HttpClientIdentity(builder.Name);31
32if (options.InProgress.Enabled)33{34builder = builder.AddHttpMessageHandler(x => new HttpClientInProgressHandler(options.InProgress, identity));35}36
37if (options.RequestCount.Enabled)38{39builder = builder.AddHttpMessageHandler(x => new HttpClientRequestCountHandler(options.RequestCount, identity));40}41
42if (options.RequestDuration.Enabled)43{44builder = builder.AddHttpMessageHandler(x => new HttpClientRequestDurationHandler(options.RequestDuration, identity));45}46
47if (options.ResponseDuration.Enabled)48{49builder = builder.AddHttpMessageHandler(x => new HttpClientResponseDurationHandler(options.ResponseDuration, identity));50}51
52return builder;53}54
55/// <summary>56/// Configures the HttpMessageHandler pipeline to collect Prometheus metrics.57/// </summary>58public static HttpMessageHandlerBuilder UseHttpClientMetrics(this HttpMessageHandlerBuilder builder, HttpClientExporterOptions? options = null)59{60options ??= new HttpClientExporterOptions();61
62var identity = new HttpClientIdentity(builder.Name);63
64if (options.InProgress.Enabled)65{66builder.AdditionalHandlers.Add(new HttpClientInProgressHandler(options.InProgress, identity));67}68
69if (options.RequestCount.Enabled)70{71builder.AdditionalHandlers.Add(new HttpClientRequestCountHandler(options.RequestCount, identity));72}73
74if (options.RequestDuration.Enabled)75{76builder.AdditionalHandlers.Add(new HttpClientRequestDurationHandler(options.RequestDuration, identity));77}78
79if (options.ResponseDuration.Enabled)80{81builder.AdditionalHandlers.Add(new HttpClientResponseDurationHandler(options.ResponseDuration, identity));82}83
84return builder;85}86
87/// <summary>88/// Configures the service container to collect Prometheus metrics from all registered HttpClients.89/// </summary>90public static IServiceCollection UseHttpClientMetrics(this IServiceCollection services, HttpClientExporterOptions? options = null)91{92return services.ConfigureAll((HttpClientFactoryOptions optionsToConfigure) =>93{94optionsToConfigure.HttpMessageHandlerBuilderActions.Add(builder =>95{96builder.UseHttpClientMetrics(options);97});98});99}100}