|
| 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