Skip to content

Commit b17cd0f

Browse files
committed
code review
Signed-off-by: Jay DeLuca <jaydeluca4@gmail.com>
1 parent e7aeef5 commit b17cd0f

4 files changed

Lines changed: 34 additions & 33 deletions

File tree

prometheus-metrics-exposition-formats/src/test/java/io/prometheus/metrics/expositionformats/DuplicateNamesProtobufTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public MetricSnapshot collect() {
4141

4242
@Override
4343
public String getPrometheusName() {
44-
return "api_responses_total";
44+
return "api_responses";
4545
}
4646
});
4747

@@ -63,7 +63,7 @@ public MetricSnapshot collect() {
6363

6464
@Override
6565
public String getPrometheusName() {
66-
return "api_responses_total";
66+
return "api_responses";
6767
}
6868
});
6969
return registry;
@@ -140,7 +140,7 @@ public MetricSnapshot collect() {
140140

141141
@Override
142142
public String getPrometheusName() {
143-
return "api_responses_total";
143+
return "api_responses";
144144
}
145145
});
146146

@@ -168,7 +168,7 @@ public MetricSnapshot collect() {
168168

169169
@Override
170170
public String getPrometheusName() {
171-
return "api_responses_total";
171+
return "api_responses";
172172
}
173173
});
174174

@@ -258,7 +258,7 @@ public MetricSnapshot collect() {
258258

259259
@Override
260260
public String getPrometheusName() {
261-
return "http_requests_total";
261+
return "http_requests";
262262
}
263263
});
264264

prometheus-metrics-exposition-textformats/src/main/java/io/prometheus/metrics/expositionformats/TextFormatUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.LinkedHashMap;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.Objects;
2425
import javax.annotation.Nullable;
2526

2627
/**
@@ -235,6 +236,21 @@ private static MetricSnapshot mergeSnapshots(List<MetricSnapshot> snapshots) {
235236
"Cannot merge histograms: gauge histogram and classic histogram");
236237
}
237238
}
239+
// Validate metadata consistency so we don't silently pick one help/unit when they differ.
240+
if (!Objects.equals(
241+
first.getMetadata().getPrometheusName(), snapshot.getMetadata().getPrometheusName())) {
242+
throw new IllegalArgumentException("Cannot merge snapshots: inconsistent metric name");
243+
}
244+
if (!Objects.equals(first.getMetadata().getHelp(), snapshot.getMetadata().getHelp())) {
245+
throw new IllegalArgumentException(
246+
"Cannot merge snapshots: conflicting help for metric "
247+
+ first.getMetadata().getPrometheusName());
248+
}
249+
if (!Objects.equals(first.getMetadata().getUnit(), snapshot.getMetadata().getUnit())) {
250+
throw new IllegalArgumentException(
251+
"Cannot merge snapshots: conflicting unit for metric "
252+
+ first.getMetadata().getPrometheusName());
253+
}
238254
totalDataPoints += snapshot.getDataPoints().size();
239255
}
240256

prometheus-metrics-exposition-textformats/src/test/java/io/prometheus/metrics/expositionformats/DuplicateNamesExpositionTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public MetricSnapshot collect() {
3535

3636
@Override
3737
public String getPrometheusName() {
38-
return "api_responses_total";
38+
return "api_responses";
3939
}
4040
});
4141

@@ -57,7 +57,7 @@ public MetricSnapshot collect() {
5757

5858
@Override
5959
public String getPrometheusName() {
60-
return "api_responses_total";
60+
return "api_responses";
6161
}
6262
});
6363
return registry;
@@ -110,7 +110,7 @@ public MetricSnapshot collect() {
110110

111111
@Override
112112
public String getPrometheusName() {
113-
return "api_responses_total";
113+
return "api_responses";
114114
}
115115
});
116116

@@ -138,7 +138,7 @@ public MetricSnapshot collect() {
138138

139139
@Override
140140
public String getPrometheusName() {
141-
return "api_responses_total";
141+
return "api_responses";
142142
}
143143
});
144144

@@ -205,7 +205,7 @@ public MetricSnapshot collect() {
205205

206206
@Override
207207
public String getPrometheusName() {
208-
return "api_responses_total";
208+
return "api_responses";
209209
}
210210
});
211211

@@ -228,7 +228,7 @@ public MetricSnapshot collect() {
228228

229229
@Override
230230
public String getPrometheusName() {
231-
return "api_responses_total";
231+
return "api_responses";
232232
}
233233
});
234234

prometheus-metrics-model/src/main/java/io/prometheus/metrics/model/registry/PrometheusRegistry.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class PrometheusRegistry {
1818

1919
public static final PrometheusRegistry defaultRegistry = new PrometheusRegistry();
2020

21-
private final Set<String> prometheusNames = ConcurrentHashMap.newKeySet();
2221
private final Set<Collector> collectors = ConcurrentHashMap.newKeySet();
2322
private final Set<MultiCollector> multiCollectors = ConcurrentHashMap.newKeySet();
2423
private final ConcurrentHashMap<String, RegistrationInfo> registered = new ConcurrentHashMap<>();
@@ -187,22 +186,19 @@ public void register(Collector collector) {
187186
+ ", new: "
188187
+ type);
189188
}
190-
existingInfo.validateMetadata(helpForValidation, unitForValidation);
189+
// Check label set first; only mutate help/unit after validation passes.
191190
if (!existingInfo.addLabelSet(names)) {
192191
throw new IllegalArgumentException(
193192
name + ": duplicate metric name with identical label schema " + names);
194193
}
194+
existingInfo.validateMetadata(helpForValidation, unitForValidation);
195195
return existingInfo;
196196
}
197197
});
198198

199199
collectorMetadata.put(
200200
collector, new CollectorRegistration(prometheusName, normalizedLabels));
201201
}
202-
203-
if (prometheusName != null) {
204-
prometheusNames.add(prometheusName);
205-
}
206202
} catch (Exception e) {
207203
collectors.remove(collector);
208204
CollectorRegistration reg = collectorMetadata.remove(collector);
@@ -219,7 +215,6 @@ public void register(MultiCollector collector) {
219215
}
220216
List<String> prometheusNamesList = collector.getPrometheusNames();
221217
List<MultiCollectorRegistration> registrations = new ArrayList<>();
222-
Set<String> namesOnlyInPrometheusNames = new HashSet<>();
223218

224219
try {
225220
for (String prometheusName : prometheusNamesList) {
@@ -249,24 +244,20 @@ public void register(MultiCollector collector) {
249244
+ ", new: "
250245
+ type);
251246
}
252-
existingInfo.validateMetadata(helpForValidation, unitForValidation);
247+
// Check label set first; only mutate help/unit after validation passes.
253248
if (!existingInfo.addLabelSet(labelNamesForValidation)) {
254249
throw new IllegalArgumentException(
255250
prometheusName
256251
+ ": duplicate metric name with identical label schema "
257252
+ labelNamesForValidation);
258253
}
254+
existingInfo.validateMetadata(helpForValidation, unitForValidation);
259255
return existingInfo;
260256
}
261257
});
262258

263259
registrations.add(new MultiCollectorRegistration(prometheusName, normalizedLabels));
264260
}
265-
266-
boolean addedToPrometheusNames = prometheusNames.add(prometheusName);
267-
if (addedToPrometheusNames && metricType == null) {
268-
namesOnlyInPrometheusNames.add(prometheusName);
269-
}
270261
}
271262

272263
multiCollectorMetadata.put(collector, registrations);
@@ -275,9 +266,6 @@ public void register(MultiCollector collector) {
275266
for (MultiCollectorRegistration registration : registrations) {
276267
unregisterLabelSchema(registration.prometheusName, registration.labelNames);
277268
}
278-
for (String name : namesOnlyInPrometheusNames) {
279-
prometheusNames.remove(name);
280-
}
281269
throw e;
282270
}
283271
}
@@ -303,27 +291,24 @@ public void unregister(MultiCollector collector) {
303291
}
304292

305293
/**
306-
* Decrements the reference count for a label schema and removes the metric name entirely if no
307-
* schemas remain.
294+
* Removes the label schema for the given metric name. If no label schemas remain for that name,
295+
* removes the metric name entirely from the registry.
308296
*/
309297
private void unregisterLabelSchema(String prometheusName, Set<String> labelNames) {
310298
registered.computeIfPresent(
311299
prometheusName,
312300
(name, info) -> {
313301
info.removeLabelSet(labelNames);
314302
if (info.isEmpty()) {
315-
// No more label schemas for this name, remove it entirely
316-
prometheusNames.remove(prometheusName);
317303
return null; // remove from registered map
318304
}
319-
return info; // keep the RegistrationInfo, just with decremented count
305+
return info;
320306
});
321307
}
322308

323309
public void clear() {
324310
collectors.clear();
325311
multiCollectors.clear();
326-
prometheusNames.clear();
327312
registered.clear();
328313
collectorMetadata.clear();
329314
multiCollectorMetadata.clear();

0 commit comments

Comments
 (0)