This repository was archived by the owner on May 15, 2024. It is now read-only.
forked from prometheus-net/prometheus-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExternalDataCollector.cs
148 lines (125 loc) · 4.87 KB
/
ExternalDataCollector.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Prometheus.Advanced;
using Prometheus.Advanced.DataContracts;
using System;
using System.Collections.Generic;
namespace tester
{
/// <summary>
/// This is an example of how to implement a collector that exposes data retrieved from an external source.
/// For example, Windows performance counters or the monitoring API of another application.
/// </summary>
sealed class ExternalDataCollector : ICollector
{
// Only used as a key in collector registry.
public string Name { get; } = "example_external_data_collector";
// Only used during registration - we can return data with any labels we want.
public string[] LabelNames { get; } = new string[0];
// Example metrics we pretend to collect.
private sealed class ServiceMetrics
{
public string Name { get; set; }
public double HandledRequestCount { get; set; }
public double SuccessRatio { get; set; }
public double Pi { get; set; }
public List<LabelPair> GetLabels()
{
return new List<LabelPair>
{
new LabelPair
{
name = "service",
value = Name
},
new LabelPair
{
name = "domain",
value = "example.test"
}
};
}
}
public IEnumerable<MetricFamily> Collect()
{
// NB! Prometheus is not at all tolerant of slow metrics export - the collection should occur in milliseconds,
// not seconds. If this data takes a long time to gather, collect it as a parallel activity, only reporting
// last known state in the actual metrics collector.
// We pretend that we somehow query external services for their metrics.
var dataForServices = new[]
{
new ServiceMetrics
{
Name = "calculator.worldcom.test",
HandledRequestCount = 2184181,
SuccessRatio = 0.9924952,
Pi = Math.PI
},
new ServiceMetrics
{
Name = "accounting.enron.test",
HandledRequestCount = 8428278281161200,
SuccessRatio = 0.999591951005,
Pi = Math.PI + 0.51
},
new ServiceMetrics
{
Name = "more.test",
HandledRequestCount = 15,
SuccessRatio = 2.0 / 15,
Pi = 3
}
};
// Now we need to transform the collected data into Prometheus data structures.
// First, formulate the metric families that represent the types of data we collect.
var piFamily = new MetricFamily
{
name = "example_value_of_pi",
type = MetricType.GAUGE,
help = "Indicates the current value of pi in the service that was queried",
};
var requestCountFamily = new MetricFamily
{
name = "example_handled_request_count",
type = MetricType.COUNTER,
help = "Number of handled requests, counting both successful and unsuccessful requests"
};
var successRatioFamily = new MetricFamily
{
name = "example_success_ratio",
type = MetricType.GAUGE,
help = "Ratio of succeeded requests to all requests."
};
// Then fill the families with the actual data rows.
foreach (var service in dataForServices)
{
piFamily.metric.Add(new Metric
{
gauge = new Gauge
{
value = service.Pi
},
label = service.GetLabels()
});
requestCountFamily.metric.Add(new Metric
{
counter = new Counter
{
value = service.HandledRequestCount
},
label = service.GetLabels()
});
successRatioFamily.metric.Add(new Metric
{
gauge = new Gauge
{
value = service.SuccessRatio
},
label = service.GetLabels()
});
}
// That's it. Just got to return the metric families for export now.
yield return piFamily;
yield return requestCountFamily;
yield return successRatioFamily;
}
}
}