forked from prometheus/client_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SampleExtendedMultiCollector.java
56 lines (48 loc) · 2.03 KB
/
SampleExtendedMultiCollector.java
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
package io.prometheus.metrics.examples.httpserver;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import io.prometheus.metrics.core.metrics.Counter;
import io.prometheus.metrics.core.metrics.Gauge;
import io.prometheus.metrics.model.registry.ExtendedMultiCollector;
import io.prometheus.metrics.model.registry.PrometheusScrapeRequest;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
public class SampleExtendedMultiCollector extends ExtendedMultiCollector {
public SampleExtendedMultiCollector() {
super();
}
@Override
protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeRequest) {
Counter sampleCounter;
Gauge sampleGauge;
sampleCounter = Counter.builder().name("x_calls_total").labelNames("target", "proc").build();
sampleGauge = Gauge.builder().name("x_load").labelNames("target", "proc").build();
String[] targetName = scrapeRequest.getParameterValues("target");
String[] procs = scrapeRequest.getParameterValues("proc");
if (targetName == null || targetName.length == 0) {
sampleCounter.labelValues("defaultTarget", "defaultProc").inc();
sampleGauge.labelValues("defaultTarget", "defaultProc").set(Math.random());
} else {
if (procs == null || procs.length == 0) {
sampleCounter.labelValues(targetName[0], "defaultProc").inc(Math.random());
sampleGauge.labelValues(targetName[0], "defaultProc").set(Math.random());
} else {
for (int i = 0; i < procs.length; i++) {
sampleCounter.labelValues(targetName[0], procs[i]).inc(Math.random());
sampleGauge.labelValues(targetName[0], procs[i]).set(Math.random());
}
}
}
Collection<MetricSnapshot> snaps = new ArrayList<MetricSnapshot>();
snaps.add(sampleCounter.collect());
snaps.add(sampleGauge.collect());
MetricSnapshots msnaps = new MetricSnapshots(snaps);
return msnaps;
}
public List<String> getPrometheusNames() {
List<String> names = new ArrayList<String>();
names.add("Multi");
return names ;
}
}