Skip to content

Commit

Permalink
additional improve MetricSnapshots.Builder performance (#985)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Burov <burov4j@yandex.ru>
  • Loading branch information
burov4j authored Sep 19, 2024
1 parent 72b79d4 commit 4ce0d15
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import static io.prometheus.metrics.model.snapshots.PrometheusNaming.prometheusName;
Expand Down Expand Up @@ -74,6 +76,7 @@ public static Builder builder() {
public static class Builder {

private final List<MetricSnapshot> snapshots = new ArrayList<>();
private final Set<String> prometheusNames = new HashSet<>();

private Builder() {
}
Expand All @@ -83,19 +86,15 @@ public boolean containsMetricName(String name) {
return false;
}
String prometheusName = prometheusName(name);
for (MetricSnapshot snapshot : snapshots) {
if (snapshot.getMetadata().getPrometheusName().equals(prometheusName)) {
return true;
}
}
return false;
return prometheusNames.contains(prometheusName);
}

/**
* Add a metric snapshot. Call multiple times to add multiple metric snapshots.
*/
public Builder metricSnapshot(MetricSnapshot snapshot) {
snapshots.add(snapshot);
prometheusNames.add(snapshot.getMetadata().getPrometheusName());
return this;
}

Expand Down

0 comments on commit 4ce0d15

Please sign in to comment.