Skip to content

Commit

Permalink
Remove scrape() from getPrometheusName() default implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Fabian Stäber <fabian@fstab.de>
  • Loading branch information
fstab committed Oct 15, 2023
1 parent 586c2f9 commit 5817253
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,8 @@ public static void main(String[] args) throws IOException, InterruptedException
gauge.labelValues("outside").set(27.0);

if (mode == Mode.error) {
Collector failingCollector = new Collector() {

@Override
public String getPrometheusName() {
return null;
}

@Override
public MetricSnapshot collect() {
throw new RuntimeException("Simulating an error.");
}
Collector failingCollector = () -> {
throw new RuntimeException("Simulating an error.");
};

PrometheusRegistry.defaultRegistry.register(failingCollector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,8 @@ public static void main(String[] args) throws Exception {
gauge.labelValues("outside").set(27.0);

if (mode == Mode.error) {
Collector failingCollector = new Collector() {

@Override
public String getPrometheusName() {
return null;
}

@Override
public MetricSnapshot collect() {
throw new RuntimeException("Simulating an error.");
}
Collector failingCollector = () -> {
throw new RuntimeException("Simulating an error.");
};

PrometheusRegistry.defaultRegistry.register(failingCollector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,8 @@ public static void main(String[] args) throws LifecycleException, IOException {
gauge.labelValues("outside").set(27.0);

if (mode == Mode.error) {
Collector failingCollector = new Collector() {

@Override
public String getPrometheusName() {
return null;
}

@Override
public MetricSnapshot collect() {
throw new RuntimeException("Simulating an error.");
}
Collector failingCollector = () -> {
throw new RuntimeException("Simulating an error.");
};

PrometheusRegistry.defaultRegistry.register(failingCollector);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,20 @@ default MetricSnapshot collect(Predicate<String> includedNames) {
}

/**
* Override this and return {@code null} if a collector does not have a constant name,
* or if you don't want this library to call {@link #collect()} during registration of this collector.
* This is called in two places:
* <ol>
* <li>During registration to check if a metric with that name already exists.</li>
* <li>During scrape to check if this collector can be skipped because a name filter is present and the metric name is excluded.</li>
* </ol>
* Returning {@code null} means checks are omitted (registration the metric always succeeds),
* and the collector is always scraped (the result is dropped after scraping if a name filter is present and
* the metric name is excluded).
* <p>
* If your metric has a name that does not change at runtime it is a good idea to overwrite this and return the name.
* <p>
* All metrics in {@code prometheus-metrics-core} override this.
*/
default String getPrometheusName() {
return collect().getMetadata().getPrometheusName();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
* Like {@link Collector}, but collecting multiple Snapshots at once.
Expand Down Expand Up @@ -35,10 +35,18 @@ default MetricSnapshots collect(Predicate<String> includedNames) {
}

/**
* Override this and return an empty list if the MultiCollector does not return a constant list of names
* (names may be added / removed between scrapes).
* This is called in two places:
* <ol>
* <li>During registration to check if a metric with that name already exists.</li>
* <li>During scrape to check if the collector can be skipped because a name filter is present and all names are excluded.</li>
* </ol>
* Returning an empty list means checks are omitted (registration metric always succeeds),
* and the collector is always scraped (if a name filter is present and all names are excluded the result is dropped).
* <p>
* If your collector returns a constant list of metrics that have names that do not change at runtime
* it is a good idea to overwrite this and return the names.
*/
default List<String> getPrometheusNames() {
return collect().stream().map(snapshot -> snapshot.getMetadata().getPrometheusName()).collect(Collectors.toList());
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public void register(MultiCollector collector) {

public void unregister(Collector collector) {
collectors.remove(collector);
prometheusNames.remove(collector.getPrometheusName());
String prometheusName = collector.getPrometheusName();
if (prometheusName != null) {
prometheusNames.remove(collector.getPrometheusName());
}
}

public void unregister(MultiCollector collector) {
Expand Down Expand Up @@ -88,7 +91,7 @@ public MetricSnapshots scrape(Predicate<String> includedNames) {
}
for (MultiCollector collector : multiCollectors) {
List<String> prometheusNames = collector.getPrometheusNames();
boolean excluded = true; // the multi-collector is excluded unless at least one name matches
boolean excluded = prometheusNames.size() > 0; // the multi-collector is excluded unless at least one name matches
for (String prometheusName : prometheusNames) {
if (includedNames.test(prometheusName)) {
excluded = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,57 @@

public class PrometheusRegistryTest {

Collector noName = new Collector() {
Collector noName = () -> GaugeSnapshot.builder()
.name("no_name_gauge")
.build();

Collector counterA1 = new Collector() {
@Override
public MetricSnapshot collect() {
return GaugeSnapshot.builder()
.name("no_name_gauge")
.build();
return CounterSnapshot.builder().name("counter_a").build();
}

@Override
public String getPrometheusName() {
return null;
return "counter_a";
}
};

Collector counterA1 = () -> CounterSnapshot.builder()
.name("counter_a")
.build();
Collector counterA2 = new Collector() {
@Override
public MetricSnapshot collect() {
return CounterSnapshot.builder().name("counter.a").build();
}

Collector counterA2 = () -> CounterSnapshot.builder()
.name("counter.a")
.build();
@Override
public String getPrometheusName() {
return "counter_a";
}
};

Collector counterB = () -> CounterSnapshot.builder()
.name("counter_b")
.build();
Collector counterB = new Collector() {
@Override
public MetricSnapshot collect() {
return CounterSnapshot.builder().name("counter_b").build();
}

Collector gaugeA = () -> GaugeSnapshot.builder()
.name("gauge_a")
.build();
@Override
public String getPrometheusName() {
return "counter_b";
}
};

Collector gaugeA = new Collector() {
@Override
public MetricSnapshot collect() {
return GaugeSnapshot.builder().name("gauge_a").build();
}

@Override
public String getPrometheusName() {
return "gauge_a";
}
};

@Test
public void registerNoName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,6 @@ public MetricSnapshots collect() {
return convert(simpleclientRegistry.metricFamilySamples());
}

@Override
public MetricSnapshots collect(Predicate<String> includedNames) {
return MultiCollector.super.collect(includedNames);
}

@Override
public List<String> getPrometheusNames() {
return MultiCollector.super.getPrometheusNames();
}

private MetricSnapshots convert(Enumeration<Collector.MetricFamilySamples> samples) {
MetricSnapshots.Builder result = MetricSnapshots.builder();
while (samples.hasMoreElements()) {
Expand Down

0 comments on commit 5817253

Please sign in to comment.