io.prometheus.metrics.model.snapshots.Exemplars Maven / Gradle / Ivy
Show all versions of prometheus-metrics-model Show documentation
package io.prometheus.metrics.model.snapshots;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* Immutable container for Exemplars.
*
* This is currently backed by a {@code List}. May be refactored later to use a more efficient data structure.
*/
public class Exemplars implements Iterable {
/**
* EMPTY means no Exemplars.
*/
public static final Exemplars EMPTY = new Exemplars(Collections.emptyList());
private final List exemplars;
private Exemplars(Collection exemplars) {
this.exemplars = Collections.unmodifiableList(new ArrayList<>(exemplars));
}
/**
* Create a new Exemplars instance.
* You can either create Exemplars with one of the static {@code Exemplars.of(...)} methods,
* or you can use the {@link Exemplars#newBuilder()}.
*
* @param exemplars a copy of the exemplars collection will be created.
*/
public static Exemplars of(Collection exemplars) {
return new Exemplars(exemplars);
}
/**
* Create a new Exemplars instance.
* You can either create Exemplars with one of the static {@code Exemplars.of(...)} methods,
* or you can use the {@link Exemplars#newBuilder()}.
*
* @param exemplars a copy of the exemplars array will be created.
*/
public static Exemplars of(Exemplar... exemplars) {
return new Exemplars(Arrays.asList(exemplars));
}
@Override
public Iterator iterator() {
return exemplars.iterator();
}
public int size() {
return exemplars.size();
}
public Exemplar get(int index) {
return exemplars.get(index);
}
/**
* This is used by classic histograms to find an exemplar with a value between lowerBound and upperBound.
*/
public Exemplar get(double lowerBound, double upperBound) {
for (int i = 0; i < exemplars.size(); i++) {
Exemplar exemplar = exemplars.get(i);
double value = exemplar.getValue();
if (value > lowerBound && value <= upperBound) {
return exemplar;
}
}
return null;
}
/**
* Find the Exemplar with the newest timestamp. May return {@code null}.
*/
public Exemplar getLatest() {
Exemplar latest = null;
for (int i=0; i exemplars = new ArrayList<>();
private Builder() {
}
public Builder addExemplar(Exemplar exemplar) {
exemplars.add(exemplar);
return this;
}
public Builder addExemplars(List exemplars) {
this.exemplars.addAll(exemplars);
return this;
}
public Exemplars build() {
return Exemplars.of(exemplars);
}
}
public static Builder newBuilder() {
return new Builder();
}
}