forked from man-group/dapr-sidekick-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaprProcessHostPrometheusCollector.cs
50 lines (44 loc) · 1.65 KB
/
DaprProcessHostPrometheusCollector.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
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Man.Dapr.Sidekick.Process;
namespace Man.Dapr.Sidekick.AspNetCore.Metrics
{
public abstract class DaprProcessHostPrometheusCollector : IPrometheusCollector
{
private readonly IDaprProcessHost _daprProcessHost;
protected DaprProcessHostPrometheusCollector(IDaprProcessHost daprProcessHost)
{
_daprProcessHost = daprProcessHost;
}
public async Task CollectTextAsync(PrometheusModel model, CancellationToken cancellationToken = default)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
// Don't do anything if the process host isn't running
if (_daprProcessHost.GetProcessInfo()?.IsRunning != true)
{
return;
}
var options = _daprProcessHost.GetProcessOptions()?.Metrics;
if (options?.EnableCollector == false)
{
// Collector disabled
return;
}
using var ms = new MemoryStream();
var bytesWritten = await _daprProcessHost.WriteMetricsAsync(ms, cancellationToken);
if (bytesWritten > 0)
{
// Read the source stream into the model, enriching with the options labels.
ms.Position = 0;
var enricher = new PrometheusLabelEnricher(options?.Labels);
var reader = new PrometheusTextReader(model, enricher);
await reader.ReadAsync(ms, cancellationToken);
}
}
}
}