Skip to content

Commit f9a42e6

Browse files
committed
Prevent unnecessary collect() calls during register()
1 parent b94922f commit f9a42e6

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/Counter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* requestCount.withLabelValues("/hello-world", "500").inc();
3434
* }</pre>
3535
*/
36-
public class Counter extends StatefulMetric<CounterDataPoint, Counter.DataPoint> implements CounterDataPoint, Collector {
36+
public class Counter extends StatefulMetric<CounterDataPoint, Counter.DataPoint> implements CounterDataPoint {
3737

3838
private final boolean exemplarsEnabled;
3939
private final ExemplarSamplerConfig exemplarSamplerConfig;

prometheus-metrics-core/src/main/java/io/prometheus/metrics/core/metrics/MetricWithFixedMetadata.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ private String makeName(String name, Unit unit) {
4141
return name;
4242
}
4343

44+
@Override
45+
public String getPrometheusName() {
46+
return metadata.getPrometheusName();
47+
}
48+
4449
public static abstract class Builder<B extends Builder<B, M>, M extends MetricWithFixedMetadata> extends Metric.Builder<B, M> {
4550

4651
protected String name;

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/snapshots/Labels.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,24 @@ public static Labels of(String[] names, String[] values) {
139139
* Dots are treated as underscores, so {@code contains("my.label")} and {@code contains("my_label")} are the same.
140140
*/
141141
public boolean contains(String labelName) {
142+
return get(labelName) != null;
143+
}
144+
145+
/**
146+
* Get the label value for a given label name.
147+
* <p>
148+
* Returns {@code null} if the {@code labelName} is not found.
149+
* <p>
150+
* Dots are treated as underscores, so {@code get("my.label")} and {@code get("my_label")} are the same.
151+
*/
152+
public String get(String labelName) {
142153
labelName = prometheusName(labelName);
143-
for (String name : prometheusNames) {
144-
if (name.equals(labelName)) {
145-
return true;
154+
for (int i=0; i<prometheusNames.length; i++) {
155+
if (prometheusNames[i].equals(labelName)) {
156+
return values[i];
146157
}
147158
}
148-
return false;
159+
return null;
149160
}
150161

151162
private static void sortAndValidate(String[] names, String[] prometheusNames, String[] values) {

0 commit comments

Comments
 (0)