-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathServiceWithMetrics.cs
More file actions
102 lines (88 loc) · 3.37 KB
/
ServiceWithMetrics.cs
File metadata and controls
102 lines (88 loc) · 3.37 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System.Threading;
using System.Threading.Tasks;
using MetricsExample.DifferentNamespace;
using MetricsExample.Model;
using Motor.Extensions.Diagnostics.Metrics;
using Motor.Extensions.Diagnostics.Metrics.Abstractions;
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.CloudEvents;
using Prometheus.Client;
namespace MetricsExample;
public class ServiceWithMetrics : INoOutputService<InputMessage>
{
private readonly IServiceInDifferentNamespace _serviceInDifferentNamespace;
private readonly ICounter? _counter;
private readonly ISummary? _simpleSummary;
private readonly IMetricFamily<ISummary>? _labeledSummary;
public ServiceWithMetrics(
IMetricsFactory<ServiceWithMetrics> metricFactory,
IServiceInDifferentNamespace serviceInDifferentNamespace
)
{
_serviceInDifferentNamespace = serviceInDifferentNamespace;
// Resulting label in Prometheus: metricsexample_emtpy_string_total
_counter = metricFactory?.CreateCounter(
"empty_string_total",
"Counts the total number of recieved empty strings."
);
// Resulting label in Prometheus: metricsexample_fancy_number
_simpleSummary = metricFactory?.CreateSummary("fancy_number", "Shows the distribution of fancy numbers.");
// Resulting label in Prometheus: metricsexample_processing_time
_labeledSummary = metricFactory?.CreateSummary(
"processing_time",
"Shows the processing time of fancy inputs.",
new[] { "successful" }
);
}
public Task<ProcessedMessageStatus> HandleMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default
)
{
/*
* Handle incoming messages
* Get the input message from the cloud event
*/
var input = inputEvent.TypedData;
// Do your magic here .....
try
{
MagicFunc(input);
}
catch (ThreadInterruptedException)
{
return Task.FromResult(ProcessedMessageStatus.TemporaryFailure);
}
return Task.FromResult(ProcessedMessageStatus.Success);
}
// Use metrics
private void MagicFunc(InputMessage input)
{
if (input.FancyText == "")
{
_counter?.Inc();
}
_simpleSummary?.Observe(input.FancyNumber);
var success = false;
/*
* With AutoObserveStopwatch, the metrics are still collected,
* even if an exception is thrown within the using scope.
* For histograms, AutoObserve can be used as a more generic approach.
*
* When using the Rider IDE, you might see a warning here, regarding possible
* modifications of the variable `success` in the outer scope. Because this is
* exactly what we want to achieve here, you can simply ignore this warning.
* Applying the fix suggested by Rider would even break the functionality.
*/
using (new AutoObserveStopwatch(() => _labeledSummary?.WithLabels(success.ToString())))
{
success = LongRunningDangerousCheck(input);
}
_serviceInDifferentNamespace.CountInDifferentNamespace();
}
private static bool LongRunningDangerousCheck(InputMessage input)
{
Thread.Sleep(100);
return input.FancyNumber < 5;
}
}