-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathDaprMetricsServerMiddleware.cs
58 lines (51 loc) · 2.32 KB
/
DaprMetricsServerMiddleware.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
// This implementation is based on the approach used by prometheus-net.
// See https://github.com/prometheus-net/prometheus-net/blob/master/Prometheus.AspNetCore/MetricServerMiddleware.cs
// See PROMETHEUS_LICENSE in this directory for license information.
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace Man.Dapr.Sidekick.AspNetCore.Metrics
{
public class DaprMetricsServerMiddleware
{
private readonly IDaprMetricsCollectorRegistry _collectorRegistry;
#pragma warning disable RCS1163 // Unused parameter.
public DaprMetricsServerMiddleware(RequestDelegate next, IDaprMetricsCollectorRegistry collectorRegistry)
#pragma warning restore RCS1163 // Unused parameter.
{
_collectorRegistry = collectorRegistry;
}
public async Task Invoke(HttpContext context)
{
var response = context.Response;
try
{
// We first touch the response.Body only in the callback because touching
// it means we can no longer send headers (the status code).
var serializer = new DaprMetricsTextSerializer(() =>
{
response.ContentType = DaprMetricsConstants.ExporterContentType;
response.StatusCode = StatusCodes.Status200OK;
return response.Body;
});
// Write all collectors
await _collectorRegistry.CollectAndExportAsTextAsync(serializer, context.RequestAborted);
}
catch (OperationCanceledException) when (context.RequestAborted.IsCancellationRequested)
{
// The scrape was cancalled by the client. This is fine. Just swallow the exception to not generate pointless spam.
}
catch (Exception ex)
{
// Update the status code and write an error message.
response.StatusCode = StatusCodes.Status503ServiceUnavailable;
if (!string.IsNullOrWhiteSpace(ex.Message))
{
using var writer = new StreamWriter(response.Body, DaprMetricsConstants.ExportEncoding, bufferSize: -1, leaveOpen: true);
await writer.WriteAsync(ex.Message);
}
}
}
}
}