forked from man-group/dapr-sidekick-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaprMetricsMiddlewareExtensions.cs
60 lines (52 loc) · 2.06 KB
/
DaprMetricsMiddlewareExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This implementation is based on the approach used by prometheus-net.
// See https://github.com/prometheus-net/prometheus-net/blob/master/Prometheus.AspNetCore/MetricServerMiddlewareExtensions.cs
// See PROMETHEUS_LICENSE in this directory for license information.
using System;
using Man.Dapr.Sidekick.AspNetCore.Metrics;
using Microsoft.AspNetCore.Http;
namespace Microsoft.AspNetCore.Builder
{
public static class DaprMetricsMiddlewareExtensions
{
#if NETCOREAPP
private const string DefaultDisplayName = "Dapr Prometheus metrics";
public static IEndpointConventionBuilder MapDaprMetrics(
this Routing.IEndpointRouteBuilder endpoints,
string pattern = "/metrics")
{
var pipeline = endpoints
.CreateApplicationBuilder()
.UseMiddleware<DaprMetricsServerMiddleware>()
.Build();
return endpoints
.Map(pattern, pipeline)
.WithDisplayName(DefaultDisplayName);
}
#endif
public static IApplicationBuilder UseDaprMetricsServer(this IApplicationBuilder builder, int port, string url = "/metrics")
{
return builder
.Map(url, b => b.MapWhen(PortMatches(), b1 => b1.InternalUseMiddleware()));
Func<HttpContext, bool> PortMatches()
{
return c => c.Connection.LocalPort == port;
}
}
public static IApplicationBuilder UseDaprMetricsServer(this IApplicationBuilder builder, string url = "/metrics")
{
// If there is a URL to map, map it and re-enter without the URL.
if (url != null)
{
return builder.Map(url, b => b.InternalUseMiddleware());
}
else
{
return builder.InternalUseMiddleware();
}
}
private static IApplicationBuilder InternalUseMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<DaprMetricsServerMiddleware>();
}
}
}