prometheus-net
131 строка · 4.9 Кб
1using Microsoft.AspNetCore.Builder;2using Microsoft.AspNetCore.Http;3using Microsoft.AspNetCore.Routing;4using System.ComponentModel;5
6namespace Prometheus;7
8public static class MetricServerMiddlewareExtensions9{
10private const string DefaultDisplayName = "Prometheus metrics";11
12/// <summary>13/// Starts a Prometheus metrics exporter using endpoint routing.14/// The default URL is /metrics, which is a Prometheus convention.15/// Use static methods on the <see cref="Metrics"/> class to create your metrics.16/// </summary>17public static IEndpointConventionBuilder MapMetrics(18this IEndpointRouteBuilder endpoints,19Action<MetricServerMiddleware.Settings> configure,20string pattern = "/metrics"21)22{23var pipeline = endpoints24.CreateApplicationBuilder()25.InternalUseMiddleware(configure)26.Build();27
28return endpoints29.Map(pattern, pipeline)30.WithDisplayName(DefaultDisplayName);31}32
33/// <summary>34/// Starts a Prometheus metrics exporter, filtering to only handle requests received on a specific port.35/// The default URL is /metrics, which is a Prometheus convention.36/// Use static methods on the <see cref="Metrics"/> class to create your metrics.37/// </summary>38public static IApplicationBuilder UseMetricServer(39this IApplicationBuilder builder,40int port,41Action<MetricServerMiddleware.Settings> configure,42string? url = "/metrics")43{44// If no URL, use root URL.45url ??= "/";46
47return builder48.Map(url, b => b.MapWhen(PortMatches(), b1 => b1.InternalUseMiddleware(configure)));49
50Func<HttpContext, bool> PortMatches()51{52return c => c.Connection.LocalPort == port;53}54}55
56/// <summary>57/// Starts a Prometheus metrics exporter.58/// The default URL is /metrics, which is a Prometheus convention.59/// Use static methods on the <see cref="Metrics"/> class to create your metrics.60/// </summary>61public static IApplicationBuilder UseMetricServer(62this IApplicationBuilder builder,63Action<MetricServerMiddleware.Settings> configure,64string? url = "/metrics")65{66if (url != null)67return builder.Map(url, b => b.InternalUseMiddleware(configure));68else69return builder.InternalUseMiddleware(configure);70}71
72#region Legacy methods without the configure action73/// <summary>74/// Starts a Prometheus metrics exporter using endpoint routing.75/// The default URL is /metrics, which is a Prometheus convention.76/// Use static methods on the <see cref="Metrics"/> class to create your metrics.77/// </summary>78[EditorBrowsable(EditorBrowsableState.Never)] // It is not exactly obsolete but let's de-emphasize it.79public static IEndpointConventionBuilder MapMetrics(80this IEndpointRouteBuilder endpoints,81string pattern = "/metrics",82CollectorRegistry? registry = null83)84{85return MapMetrics(endpoints, LegacyConfigure(registry), pattern);86}87
88/// <summary>89/// Starts a Prometheus metrics exporter, filtering to only handle requests received on a specific port.90/// The default URL is /metrics, which is a Prometheus convention.91/// Use static methods on the <see cref="Metrics"/> class to create your metrics.92/// </summary>93[EditorBrowsable(EditorBrowsableState.Never)] // It is not exactly obsolete but let's de-emphasize it.94public static IApplicationBuilder UseMetricServer(95this IApplicationBuilder builder,96int port,97string? url = "/metrics",98CollectorRegistry? registry = null)99{100return UseMetricServer(builder, port, LegacyConfigure(registry), url);101}102
103/// <summary>104/// Starts a Prometheus metrics exporter.105/// The default URL is /metrics, which is a Prometheus convention.106/// Use static methods on the <see cref="Metrics"/> class to create your metrics.107/// </summary>108[EditorBrowsable(EditorBrowsableState.Never)] // It is not exactly obsolete but let's de-emphasize it.109public static IApplicationBuilder UseMetricServer(110this IApplicationBuilder builder,111string? url = "/metrics",112CollectorRegistry? registry = null)113{114return UseMetricServer(builder, LegacyConfigure(registry), url);115}116
117private static Action<MetricServerMiddleware.Settings> LegacyConfigure(CollectorRegistry? registry) =>118(MetricServerMiddleware.Settings settings) =>119{120settings.Registry = registry;121};122#endregion123
124private static IApplicationBuilder InternalUseMiddleware(this IApplicationBuilder builder, Action<MetricServerMiddleware.Settings> configure)125{126var settings = new MetricServerMiddleware.Settings();127configure(settings);128
129return builder.UseMiddleware<MetricServerMiddleware>(settings);130}131}
132