Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various cleanups in prometheus-metrics-model #993

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ Prometheus uses GitHub to manage reviews of pull requests.
on our [mailing list](https://groups.google.com/forum/?fromgroups#!forum/prometheus-developers).
This will avoid unnecessary work and surely give you and us a good deal
of inspiration.

## Formatting

This repository uses [Google Java Format](https://github.com/google/google-java-format) to format the code.

Run `./mvnw spotless:apply` to format the code (only changed files) before committing.
Original file line number Diff line number Diff line change
@@ -1,76 +1,77 @@
package io.prometheus.metrics.model.registry;

import io.prometheus.metrics.model.snapshots.MetricSnapshot;

import java.util.function.Predicate;

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;

/**
* To be registered with the Prometheus collector registry.
* See <i>Overall Structure</i> on
* <a href="https://prometheus.io/docs/instrumenting/writing_clientlibs/">https://prometheus.io/docs/instrumenting/writing_clientlibs/</a>.
* To be registered with the Prometheus collector registry. See <i>Overall Structure</i> on <a
* href="https://prometheus.io/docs/instrumenting/writing_clientlibs/">https://prometheus.io/docs/instrumenting/writing_clientlibs/</a>.
*/
@FunctionalInterface
public interface Collector {

/**
* Called when the Prometheus server scrapes metrics.
*/
MetricSnapshot collect();
/** Called when the Prometheus server scrapes metrics. */
MetricSnapshot collect();

/**
* Provides Collector with the details of the request issued by Prometheus to allow multi-target pattern implementation
* Override to implement request dependent logic to provide MetricSnapshot
*/
default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) {
return collect();
}

/**
* Like {@link #collect()}, but returns {@code null} if {@code includedNames.test(name)} is {@code false}.
* <p>
* Override this if there is a more efficient way than first collecting the snapshot and then discarding it.
*/
default MetricSnapshot collect(Predicate<String> includedNames) {
MetricSnapshot result = collect();
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
}

/**
* Like {@link #collect(Predicate)}, but with support for multi-target pattern.
* <p>
* Override this if there is a more efficient way than first collecting the snapshot and then discarding it.
*/
default MetricSnapshot collect(Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
MetricSnapshot result = collect(scrapeRequest);
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
/**
* Provides Collector with the details of the request issued by Prometheus to allow multi-target
* pattern implementation Override to implement request dependent logic to provide MetricSnapshot
*/
default MetricSnapshot collect(PrometheusScrapeRequest scrapeRequest) {
return collect();
}

/**
* Like {@link #collect()}, but returns {@code null} if {@code includedNames.test(name)} is {@code
* false}.
*
* <p>Override this if there is a more efficient way than first collecting the snapshot and then
* discarding it.
*/
default MetricSnapshot collect(Predicate<String> includedNames) {
MetricSnapshot result = collect();
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
}

/**
* 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 null;
/**
* Like {@link #collect(Predicate)}, but with support for multi-target pattern.
*
* <p>Override this if there is a more efficient way than first collecting the snapshot and then
* discarding it.
*/
default MetricSnapshot collect(
Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
MetricSnapshot result = collect(scrapeRequest);
if (includedNames.test(result.getMetadata().getPrometheusName())) {
return result;
} else {
return null;
}
}

/**
* This is called in two places:
*
* <ol>
* <li>During registration to check if a metric with that name already exists.
* <li>During scrape to check if this collector can be skipped because a name filter is present
* and the metric name is excluded.
* </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 null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,128 +2,143 @@

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;

import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Predicate;

import io.prometheus.metrics.model.snapshots.MetricSnapshot;
import io.prometheus.metrics.model.snapshots.MetricSnapshots;

public class PrometheusRegistry {

public static final PrometheusRegistry defaultRegistry = new PrometheusRegistry();

private final Set<String> prometheusNames = ConcurrentHashMap.newKeySet();
private final List<Collector> collectors = new CopyOnWriteArrayList<>();
private final List<MultiCollector> multiCollectors = new CopyOnWriteArrayList<>();
public static final PrometheusRegistry defaultRegistry = new PrometheusRegistry();

public void register(Collector collector) {
String prometheusName = collector.getPrometheusName();
if (prometheusName != null) {
if (!prometheusNames.add(prometheusName)) {
throw new IllegalStateException("Can't register " + prometheusName + " because a metric with that name is already registered.");
}
}
collectors.add(collector);
}
private final Set<String> prometheusNames = ConcurrentHashMap.newKeySet();
private final List<Collector> collectors = new CopyOnWriteArrayList<>();
private final List<MultiCollector> multiCollectors = new CopyOnWriteArrayList<>();

public void register(MultiCollector collector) {
for (String prometheusName : collector.getPrometheusNames()) {
if (!prometheusNames.add(prometheusName)) {
throw new IllegalStateException("Can't register " + prometheusName + " because that name is already registered.");
}
}
multiCollectors.add(collector);
}
public void register(Collector collector) {
String prometheusName = collector.getPrometheusName();
if (prometheusName != null) {
if (!prometheusNames.add(prometheusName)) {
throw new IllegalStateException(
"Can't register "
+ prometheusName
+ " because a metric with that name is already registered.");
}
}
collectors.add(collector);
}

public void unregister(Collector collector) {
collectors.remove(collector);
String prometheusName = collector.getPrometheusName();
if (prometheusName != null) {
prometheusNames.remove(collector.getPrometheusName());
}
}
public void register(MultiCollector collector) {
for (String prometheusName : collector.getPrometheusNames()) {
if (!prometheusNames.add(prometheusName)) {
throw new IllegalStateException(
"Can't register " + prometheusName + " because that name is already registered.");
}
}
multiCollectors.add(collector);
}

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

public MetricSnapshots scrape() {
return scrape((PrometheusScrapeRequest) null);
}
public void unregister(MultiCollector collector) {
multiCollectors.remove(collector);
for (String prometheusName : collector.getPrometheusNames()) {
prometheusNames.remove(prometheusName(prometheusName));
}
}

public MetricSnapshots scrape(PrometheusScrapeRequest scrapeRequest) {
MetricSnapshots.Builder result = MetricSnapshots.builder();
for (Collector collector : collectors) {
MetricSnapshot snapshot = scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
if (snapshot != null) {
if (result.containsMetricName(snapshot.getMetadata().getName())) {
throw new IllegalStateException(snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
}
result.metricSnapshot(snapshot);
}
}
for (MultiCollector collector : multiCollectors) {
MetricSnapshots snaphots = scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
for (MetricSnapshot snapshot : snaphots) {
if (result.containsMetricName(snapshot.getMetadata().getName())) {
throw new IllegalStateException(snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
}
result.metricSnapshot(snapshot);
}
}
return result.build();
}
public MetricSnapshots scrape() {
return scrape((PrometheusScrapeRequest) null);
}

public MetricSnapshots scrape(Predicate<String> includedNames) {
if (includedNames == null) {
return scrape();
}
return scrape(includedNames, null);
}
public MetricSnapshots scrape(PrometheusScrapeRequest scrapeRequest) {
MetricSnapshots.Builder result = MetricSnapshots.builder();
for (Collector collector : collectors) {
MetricSnapshot snapshot =
scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
if (snapshot != null) {
if (result.containsMetricName(snapshot.getMetadata().getName())) {
throw new IllegalStateException(
snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
}
result.metricSnapshot(snapshot);
}
}
for (MultiCollector collector : multiCollectors) {
MetricSnapshots snapshots =
scrapeRequest == null ? collector.collect() : collector.collect(scrapeRequest);
for (MetricSnapshot snapshot : snapshots) {
if (result.containsMetricName(snapshot.getMetadata().getName())) {
throw new IllegalStateException(
snapshot.getMetadata().getPrometheusName() + ": duplicate metric name.");
}
result.metricSnapshot(snapshot);
}
}
return result.build();
}

public MetricSnapshots scrape(Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
if (includedNames == null) {
return scrape(scrapeRequest);
}
MetricSnapshots.Builder result = MetricSnapshots.builder();
for (Collector collector : collectors) {
String prometheusName = collector.getPrometheusName();
// prometheusName == null means the name is unknown, and we have to scrape to learn the name.
// prometheusName != null means we can skip the scrape if the name is excluded.
if (prometheusName == null || includedNames.test(prometheusName)) {
MetricSnapshot snapshot = scrapeRequest == null ? collector.collect(includedNames) : collector.collect(includedNames, scrapeRequest);
if (snapshot != null) {
result.metricSnapshot(snapshot);
}
}
}
for (MultiCollector collector : multiCollectors) {
List<String> prometheusNames = collector.getPrometheusNames();
// empty prometheusNames means the names are unknown, and we have to scrape to learn the names.
// non-empty prometheusNames means we can exclude the collector if all names are excluded by the filter.
boolean excluded = !prometheusNames.isEmpty();
for (String prometheusName : prometheusNames) {
if (includedNames.test(prometheusName)) {
excluded = false;
break;
}
}
if (!excluded) {
MetricSnapshots snapshots = scrapeRequest == null ? collector.collect(includedNames) : collector.collect(includedNames, scrapeRequest);
for (MetricSnapshot snapshot : snapshots) {
if (snapshot != null) {
result.metricSnapshot(snapshot);
}
}
}
}
return result.build();
}
public MetricSnapshots scrape(Predicate<String> includedNames) {
if (includedNames == null) {
return scrape();
}
return scrape(includedNames, null);
}

public MetricSnapshots scrape(
Predicate<String> includedNames, PrometheusScrapeRequest scrapeRequest) {
if (includedNames == null) {
return scrape(scrapeRequest);
}
MetricSnapshots.Builder result = MetricSnapshots.builder();
for (Collector collector : collectors) {
String prometheusName = collector.getPrometheusName();
// prometheusName == null means the name is unknown, and we have to scrape to learn the name.
// prometheusName != null means we can skip the scrape if the name is excluded.
if (prometheusName == null || includedNames.test(prometheusName)) {
MetricSnapshot snapshot =
scrapeRequest == null
? collector.collect(includedNames)
: collector.collect(includedNames, scrapeRequest);
if (snapshot != null) {
result.metricSnapshot(snapshot);
}
}
}
for (MultiCollector collector : multiCollectors) {
List<String> prometheusNames = collector.getPrometheusNames();
// empty prometheusNames means the names are unknown, and we have to scrape to learn the
// names.
// non-empty prometheusNames means we can exclude the collector if all names are excluded by
// the filter.
boolean excluded = !prometheusNames.isEmpty();
for (String prometheusName : prometheusNames) {
if (includedNames.test(prometheusName)) {
excluded = false;
break;
}
}
if (!excluded) {
MetricSnapshots snapshots =
scrapeRequest == null
? collector.collect(includedNames)
: collector.collect(includedNames, scrapeRequest);
for (MetricSnapshot snapshot : snapshots) {
if (snapshot != null) {
result.metricSnapshot(snapshot);
}
}
}
}
return result.build();
}
}
Loading