Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
Signed-off-by: Gregor Zeitlinger <[email protected]>
  • Loading branch information
zeitlinger committed Oct 1, 2024
1 parent 9ff3c38 commit 12ebaf2
Show file tree
Hide file tree
Showing 17 changed files with 1,830 additions and 1,780 deletions.
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,74 +1,77 @@
package io.prometheus.metrics.model.registry;

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

import java.util.function.Predicate;

/**
* 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 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() {
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

0 comments on commit 12ebaf2

Please sign in to comment.