Skip to content

Commit

Permalink
Redesign of Collector architecture (#901).
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Kielar <[email protected]>
  • Loading branch information
Marcin Kielar committed Dec 8, 2023
1 parent 60f72b6 commit ede5cc3
Show file tree
Hide file tree
Showing 36 changed files with 820 additions and 544 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class Main {

public static void main(String[] args) throws IOException, InterruptedException {

SampleMultiCollector xmc = new SampleMultiCollector();
SampleCollector xmc = new SampleCollector();
PrometheusRegistry.defaultRegistry.register(xmc);
HTTPServer server = HTTPServer.builder()
.port(9401)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
package io.prometheus.metrics.examples.multitarget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

import io.prometheus.metrics.model.registry.MultiCollector;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.PrometheusScrapeRequest;
import io.prometheus.metrics.model.snapshots.CounterSnapshot;
import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot.Builder;
import io.prometheus.metrics.model.snapshots.GaugeSnapshot;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import io.prometheus.metrics.model.snapshots.PrometheusNaming;

public class SampleMultiCollector implements MultiCollector {

public SampleMultiCollector() {
super();
}

@Override
public MetricSnapshots collect() {
return new MetricSnapshots();
}

public class SampleCollector implements Collector {
@Override
public MetricSnapshots collect(PrometheusScrapeRequest scrapeRequest) {
return collectMetricSnapshots(scrapeRequest);
public MetricSnapshots collect(Predicate<String> nameFilter, PrometheusScrapeRequest scrapeRequest) {
return collectMetricSnapshots(scrapeRequest).filter(nameFilter);
}

protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeRequest) {

GaugeSnapshot.Builder gaugeBuilder = GaugeSnapshot.builder();
gaugeBuilder.name("x_load").help("process load");

Expand Down Expand Up @@ -71,18 +57,7 @@ protected MetricSnapshots collectMetricSnapshots(PrometheusScrapeRequest scrapeR
gaugeBuilder.dataPoint(gaugeDataPointBuilder.build());
}
}
Collection<MetricSnapshot> snaps = new ArrayList<MetricSnapshot>();
snaps.add(counterBuilder.build());
snaps.add(gaugeBuilder.build());
MetricSnapshots msnaps = new MetricSnapshots(snaps);
MetricSnapshots msnaps = new MetricSnapshots(counterBuilder.build(), gaugeBuilder.build());
return msnaps;
}

public List<String> getPrometheusNames() {
List<String> names = new ArrayList<String>();
names.add("x_calls_total");
names.add("x_load");
return names;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.prometheus.metrics.core.metrics.Gauge;
import io.prometheus.metrics.core.metrics.Info;
import io.prometheus.metrics.exporter.httpserver.HTTPServer;
import io.prometheus.metrics.model.registry.CollectorBuilder;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.Unit;

import java.io.IOException;
Expand Down Expand Up @@ -53,9 +53,9 @@ public static void main(String[] args) throws IOException, InterruptedException
gauge.labelValues("outside").set(27.0);

if (mode == Mode.error) {
Collector failingCollector = () -> {
Collector failingCollector = CollectorBuilder.fromMetric(() -> {
throw new RuntimeException("Simulating an error.");
};
});

PrometheusRegistry.defaultRegistry.register(failingCollector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.prometheus.metrics.core.metrics.Gauge;
import io.prometheus.metrics.core.metrics.Info;
import io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet;
import io.prometheus.metrics.model.registry.CollectorBuilder;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.Unit;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
Expand Down Expand Up @@ -57,9 +57,9 @@ public static void main(String[] args) throws Exception {
gauge.labelValues("outside").set(27.0);

if (mode == Mode.error) {
Collector failingCollector = () -> {
Collector failingCollector = CollectorBuilder.fromMetrics(() -> {
throw new RuntimeException("Simulating an error.");
};
});

PrometheusRegistry.defaultRegistry.register(failingCollector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.prometheus.metrics.core.metrics.Gauge;
import io.prometheus.metrics.core.metrics.Info;
import io.prometheus.metrics.exporter.servlet.jakarta.PrometheusMetricsServlet;
import io.prometheus.metrics.model.registry.CollectorBuilder;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.Unit;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
Expand Down Expand Up @@ -61,9 +61,9 @@ public static void main(String[] args) throws LifecycleException, IOException {
gauge.labelValues("outside").set(27.0);

if (mode == Mode.error) {
Collector failingCollector = () -> {
Collector failingCollector = CollectorBuilder.fromMetrics(() -> {
throw new RuntimeException("Simulating an error.");
};
});

PrometheusRegistry.defaultRegistry.register(failingCollector);
}
Expand Down
12 changes: 12 additions & 0 deletions prometheus-metrics-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
<properties>
<automatic.module.name>io.prometheus.metrics.core</automatic.module.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>

<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.CollectorBuilder;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.Label;
import io.prometheus.metrics.model.snapshots.Labels;
Expand All @@ -21,9 +22,6 @@ protected Metric(Builder<?, ?> builder) {
this.constLabels = builder.constLabels;
}

@Override
public abstract MetricSnapshot collect();

protected static abstract class Builder<B extends Builder<B, M>, M extends Metric> {

protected final List<String> illegalLabelNames;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package io.prometheus.metrics.core.metrics;

import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.model.snapshots.Labels;
import io.prometheus.metrics.model.snapshots.MetricMetadata;
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
import io.prometheus.metrics.model.snapshots.Unit;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.CollectorBuilder;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.registry.PrometheusScrapeRequest;
import io.prometheus.metrics.model.snapshots.*;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

/**
* Almost all metrics have fixed metadata, i.e. the metric name is known when the metric is created.
Expand All @@ -30,6 +32,15 @@ protected MetricMetadata getMetadata() {
return metadata;
}

protected abstract MetricSnapshot collect();

public MetricSnapshots collect(Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
if(includedNames.test(this.metadata.getPrometheusName()))
return MetricSnapshots.of(collect());
else
return MetricSnapshots.empty();
}

private String makeName(String name, Unit unit) {
if (unit != null) {
if (!name.endsWith(unit.toString())) {
Expand All @@ -39,11 +50,6 @@ private String makeName(String name, Unit unit) {
return name;
}

@Override
public String getPrometheusName() {
return metadata.getPrometheusName();
}

public static abstract class Builder<B extends Builder<B, M>, M extends MetricWithFixedMetadata> extends Metric.Builder<B, M> {

protected String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.prometheus.metrics.core.metrics;

import io.prometheus.metrics.model.registry.MetricNameFilter;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import org.junit.Assert;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

public class NameFilterTest {
@Test
public void testCounterWithCallback() {
AtomicInteger accessCount = new AtomicInteger();
CounterWithCallback metrics = CounterWithCallback.builder()
.name("my_counter")
.callback(cb -> {
accessCount.incrementAndGet();
cb.call(1.0);
})
.build();


PrometheusRegistry registry = new PrometheusRegistry();
registry.register(metrics);

var result1 = registry.scrape(MetricNameFilter.builder().nameMustBeEqualTo("XXX").build());
Assert.assertEquals(accessCount.get(), 0);
Assert.assertTrue(result1.stream().toList().isEmpty());

var result2 = registry.scrape(MetricNameFilter.builder().nameMustBeEqualTo("my_counter").build());
Assert.assertEquals(accessCount.get(), 1);
Assert.assertEquals(result2.stream().toList().size(), 1);
Assert.assertEquals(result2.get(0).getMetadata().getPrometheusName(), "my_counter");
}

@Test
public void testGaugeWithCallback() {
AtomicInteger accessCount = new AtomicInteger();
GaugeWithCallback metrics = GaugeWithCallback.builder()
.name("my_gauge")
.callback(cb -> {
accessCount.incrementAndGet();
cb.call(1.0);
})
.build();


PrometheusRegistry registry = new PrometheusRegistry();
registry.register(metrics);

var result1 = registry.scrape(MetricNameFilter.builder().nameMustBeEqualTo("XXX").build());
Assert.assertEquals(accessCount.get(), 0);
Assert.assertTrue(result1.stream().toList().isEmpty());

var result2 = registry.scrape(MetricNameFilter.builder().nameMustBeEqualTo("my_gauge").build());
Assert.assertEquals(accessCount.get(), 1);
Assert.assertEquals(result2.stream().toList().size(), 1);
Assert.assertEquals(result2.get(0).getMetadata().getPrometheusName(), "my_gauge");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.expositionformats.ExpositionFormatWriter;
import io.prometheus.metrics.expositionformats.ExpositionFormats;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.MetricNameFilter;
import io.prometheus.metrics.model.registry.PrometheusRegistry;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
Expand All @@ -23,7 +24,7 @@
*/
public class PrometheusScrapeHandler {

private final PrometheusRegistry registry;
private final Collector registry;
private final ExpositionFormats expositionFormats;
private final Predicate<String> nameFilter;
private AtomicInteger lastResponseSize = new AtomicInteger(2 << 9); // 0.5 MB
Expand All @@ -32,15 +33,15 @@ public PrometheusScrapeHandler() {
this(PrometheusProperties.get(), PrometheusRegistry.defaultRegistry);
}

public PrometheusScrapeHandler(PrometheusRegistry registry) {
public PrometheusScrapeHandler(Collector registry) {
this(PrometheusProperties.get(), registry);
}

public PrometheusScrapeHandler(PrometheusProperties config) {
this(config, PrometheusRegistry.defaultRegistry);
}

public PrometheusScrapeHandler(PrometheusProperties config, PrometheusRegistry registry) {
public PrometheusScrapeHandler(PrometheusProperties config, Collector registry) {
this.expositionFormats = ExpositionFormats.init(config.getExporterProperties());
this.registry = registry;
this.nameFilter = makeNameFilter(config.getExporterFilterProperties());
Expand Down Expand Up @@ -106,15 +107,11 @@ private Predicate<String> makeNameFilter(ExporterFilterProperties props) {
private MetricSnapshots scrape(PrometheusHttpRequest request) {

Predicate<String> filter = makeNameFilter(request.getParameterValues("name[]"));
if (filter != null) {
return registry.scrape(filter, request);
} else {
return registry.scrape(request);
}
return registry.collect(filter, request);
}

private Predicate<String> makeNameFilter(String[] includedNames) {
Predicate<String> result = null;
Predicate<String> result = MetricNameFilter.ALLOW_ALL;
if (includedNames != null && includedNames.length > 0) {
result = MetricNameFilter.builder().nameMustBeEqualTo(includedNames).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.sun.net.httpserver.HttpHandler;
import io.prometheus.metrics.config.PrometheusProperties;
import io.prometheus.metrics.exporter.common.PrometheusScrapeHandler;
import io.prometheus.metrics.model.registry.Collector;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

import java.io.ByteArrayOutputStream;
Expand All @@ -26,7 +27,7 @@ public MetricsHandler() {
prometheusScrapeHandler = new PrometheusScrapeHandler();
}

public MetricsHandler(PrometheusRegistry registry) {
public MetricsHandler(Collector registry) {
prometheusScrapeHandler = new PrometheusScrapeHandler(registry);
}

Expand Down
12 changes: 12 additions & 0 deletions prometheus-metrics-instrumentation-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
<properties>
<automatic.module.name>io.prometheus.metrics.instrumentation.jvm</automatic.module.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>

<licenses>
<license>
Expand Down
Loading

0 comments on commit ede5cc3

Please sign in to comment.