Skip to content

Commit f0b02fc

Browse files
committed
Added MultiCollector example
1 parent 2132df5 commit f0b02fc

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

examples/example-exporter-httpserver/src/main/java/io/prometheus/metrics/examples/httpserver/MainMultiTarget.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public static void main(String[] args) throws IOException, InterruptedException
1414

1515
SampleExtendedCollector xc = new SampleExtendedCollector();
1616
PrometheusRegistry.defaultRegistry.register(xc);
17+
SampleExtendedMultiCollector xmc = new SampleExtendedMultiCollector();
18+
PrometheusRegistry.defaultRegistry.register(xmc);
1719
HTTPServer server = HTTPServer.builder()
1820
.port(9400)
1921
.buildAndStart();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.prometheus.metrics.examples.httpserver;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
7+
import io.prometheus.metrics.core.metrics.Counter;
8+
import io.prometheus.metrics.core.metrics.Gauge;
9+
import io.prometheus.metrics.model.registry.ExtendedMultiCollector;
10+
import io.prometheus.metrics.model.registry.PrometheusScrapeRequest;
11+
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
12+
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
13+
14+
public class SampleExtendedMultiCollector extends ExtendedMultiCollector {
15+
16+
public SampleExtendedMultiCollector() {
17+
super();
18+
}
19+
20+
@Override
21+
protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeRequest) {
22+
Counter sampleCounter;
23+
Gauge sampleGauge;
24+
sampleCounter = Counter.builder().name("x_calls_total").labelNames("target", "proc").build();
25+
sampleGauge = Gauge.builder().name("x_load").labelNames("target", "proc").build();
26+
String[] targetName = scrapeRequest.getParameterValues("target");
27+
String[] procs = scrapeRequest.getParameterValues("proc");
28+
if (targetName == null || targetName.length == 0) {
29+
sampleCounter.labelValues("defaultTarget", "defaultProc").inc();
30+
sampleGauge.labelValues("defaultTarget", "defaultProc").set(Math.random());
31+
} else {
32+
if (procs == null || procs.length == 0) {
33+
sampleCounter.labelValues(targetName[0], "defaultProc").inc(Math.random());
34+
sampleGauge.labelValues(targetName[0], "defaultProc").set(Math.random());
35+
} else {
36+
for (int i = 0; i < procs.length; i++) {
37+
sampleCounter.labelValues(targetName[0], procs[i]).inc(Math.random());
38+
sampleGauge.labelValues(targetName[0], procs[i]).set(Math.random());
39+
}
40+
41+
}
42+
}
43+
Collection<MetricSnapshot> snaps = new ArrayList<MetricSnapshot>();
44+
snaps.add(sampleCounter.collect());
45+
snaps.add(sampleGauge.collect());
46+
MetricSnapshots msnaps = new MetricSnapshots(snaps);
47+
return msnaps;
48+
}
49+
50+
public List<String> getPrometheusNames() {
51+
List<String> names = new ArrayList<String>();
52+
names.add("Multi");
53+
return names ;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)