Skip to content

Commit 95051a6

Browse files
Cache sanitized name and labels (#1170)
Signed-off-by: Rafał Sumisławski <rafal.sumislawski@coralogix.com>
1 parent 0a3437b commit 95051a6

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

collector/src/main/java/io/prometheus/jmx/JmxCollector.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,10 +835,9 @@ public void recordBean(
835835
// Add to samples.
836836
LOGGER.log(
837837
FINE,
838-
"add metric sample: %s %s %s %s",
838+
"add metric sample: %s %s %s",
839839
matchedRule.name,
840-
matchedRule.labelNames,
841-
matchedRule.labelValues,
840+
matchedRule.labels,
842841
value.doubleValue());
843842

844843
matchedRules.add(matchedRule.withValue(value.doubleValue()));

collector/src/main/java/io/prometheus/jmx/MatchedRule.java

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.prometheus.jmx;
1818

19+
import io.prometheus.metrics.model.snapshots.Labels;
1920
import io.prometheus.metrics.model.snapshots.PrometheusNaming;
2021
import java.util.List;
2122
import java.util.Objects;
@@ -31,8 +32,7 @@ public class MatchedRule {
3132
final String matchName;
3233
final String type;
3334
final String help;
34-
final List<String> labelNames;
35-
final List<String> labelValues;
35+
final Labels labels;
3636
final Double value;
3737
final double valueFactor;
3838

@@ -43,8 +43,7 @@ private MatchedRule() {
4343
this.matchName = null;
4444
this.type = null;
4545
this.help = null;
46-
this.labelNames = null;
47-
this.labelValues = null;
46+
this.labels = null;
4847
this.value = null;
4948
this.valueFactor = 1.0;
5049
}
@@ -70,12 +69,39 @@ public MatchedRule(
7069
final List<String> labelValues,
7170
final Double value,
7271
double valueFactor) {
72+
this.name = PrometheusNaming.sanitizeMetricName(name);
73+
this.matchName = matchName;
74+
this.type = type;
75+
this.help = help;
76+
this.labels = Labels.of(labelNames, labelValues);
77+
this.value = value;
78+
this.valueFactor = valueFactor;
79+
}
80+
81+
/**
82+
* Constructor
83+
*
84+
* @param name name - has to be already sanitized (we ensure this by keeping the constructor private)
85+
* @param matchName matchName
86+
* @param type type
87+
* @param help help
88+
* @param labels labels
89+
* @param value value
90+
* @param valueFactor valueFactor
91+
*/
92+
private MatchedRule(
93+
final String name,
94+
final String matchName,
95+
final String type,
96+
final String help,
97+
final Labels labels,
98+
final Double value,
99+
double valueFactor) {
73100
this.name = name;
74101
this.matchName = matchName;
75102
this.type = type;
76103
this.help = help;
77-
this.labelNames = labelNames;
78-
this.labelValues = labelValues;
104+
this.labels = labels;
79105
this.value = value;
80106
this.valueFactor = valueFactor;
81107
}
@@ -88,12 +114,11 @@ public MatchedRule(
88114
*/
89115
public MatchedRule withValue(double value) {
90116
return new MatchedRule(
91-
PrometheusNaming.sanitizeMetricName(this.name),
117+
this.name,
92118
this.matchName,
93119
this.type,
94120
this.help,
95-
this.labelNames,
96-
this.labelValues,
121+
this.labels,
97122
value,
98123
this.valueFactor);
99124
}
@@ -142,10 +167,8 @@ public String toString() {
142167
+ ", help='"
143168
+ help
144169
+ '\''
145-
+ ", labelNames="
146-
+ labelNames
147-
+ ", labelValues="
148-
+ labelValues
170+
+ ", labels="
171+
+ labels
149172
+ ", value="
150173
+ value
151174
+ ", valueFactor="
@@ -163,14 +186,13 @@ public boolean equals(Object o) {
163186
&& Objects.equals(matchName, that.matchName)
164187
&& Objects.equals(type, that.type)
165188
&& Objects.equals(help, that.help)
166-
&& Objects.equals(labelNames, that.labelNames)
167-
&& Objects.equals(labelValues, that.labelValues)
189+
&& Objects.equals(labels, that.labels)
168190
&& Objects.equals(value, that.value);
169191
}
170192

171193
@Override
172194
public int hashCode() {
173195
return Objects.hash(
174-
name, matchName, type, help, labelNames, labelValues, value, valueFactor);
196+
name, matchName, type, help, labels, value, valueFactor);
175197
}
176198
}

collector/src/main/java/io/prometheus/jmx/MatchedRuleToMetricSnapshotsConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private static MetricSnapshot convertRulesWithSameName(List<MatchedRule> rulesWi
9090
.name(rulesWithSameName.get(0).name)
9191
.help(rulesWithSameName.get(0).help);
9292
for (MatchedRule rule : rulesWithSameName) {
93-
Labels labels = Labels.of(rule.labelNames, rule.labelValues);
93+
Labels labels = rule.labels;
9494
if (!labelsUnique) {
9595
labels =
9696
labels.merge(
@@ -112,7 +112,7 @@ private static MetricSnapshot convertRulesWithSameName(List<MatchedRule> rulesWi
112112
.name(rulesWithSameName.get(0).name)
113113
.help(rulesWithSameName.get(0).help);
114114
for (MatchedRule rule : rulesWithSameName) {
115-
Labels labels = Labels.of(rule.labelNames, rule.labelValues);
115+
Labels labels = rule.labels;
116116
if (!labelsUnique) {
117117
labels =
118118
labels.merge(
@@ -134,7 +134,7 @@ private static MetricSnapshot convertRulesWithSameName(List<MatchedRule> rulesWi
134134
.name(rulesWithSameName.get(0).name)
135135
.help(rulesWithSameName.get(0).help);
136136
for (MatchedRule rule : rulesWithSameName) {
137-
Labels labels = Labels.of(rule.labelNames, rule.labelValues);
137+
Labels labels = rule.labels;
138138
if (!labelsUnique) {
139139
labels =
140140
labels.merge(
@@ -164,7 +164,7 @@ private static String getType(List<MatchedRule> rulesWithSameName) {
164164
private static boolean isLabelsUnique(List<MatchedRule> rulesWithSameName) {
165165
Set<Labels> labelsSet = new HashSet<>(rulesWithSameName.size());
166166
for (MatchedRule matchedRule : rulesWithSameName) {
167-
Labels labels = Labels.of(matchedRule.labelNames, matchedRule.labelValues);
167+
Labels labels = matchedRule.labels;
168168
if (labelsSet.contains(labels)) {
169169
return false;
170170
}

0 commit comments

Comments
 (0)