Skip to content

Commit 17c98eb

Browse files
authored
Prevent name conflicts and rename jvm_classes_loaded (#681)
Signed-off-by: Fabian Stäber <[email protected]>
1 parent 9701230 commit 17c98eb

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

simpleclient/src/main/java/io/prometheus/client/CollectorRegistry.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ public CollectorRegistry(boolean autoDescribe) {
4848
*/
4949
public void register(Collector m) {
5050
List<String> names = collectorNames(m);
51+
assertNoDuplicateNames(m, names);
5152
synchronized (namesCollectorsLock) {
5253
for (String name : names) {
5354
if (namesToCollectors.containsKey(name)) {
54-
throw new IllegalArgumentException("Collector already registered that provides name: " + name);
55+
throw new IllegalArgumentException("Failed to register Collector of type " + m.getClass().getSimpleName()
56+
+ ": " + name + " is already in use by another Collector of type "
57+
+ namesToCollectors.get(name).getClass().getSimpleName());
5558
}
5659
}
5760
for (String name : names) {
@@ -61,6 +64,16 @@ public void register(Collector m) {
6164
}
6265
}
6366

67+
private void assertNoDuplicateNames(Collector m, List<String> names) {
68+
Set<String> uniqueNames = new HashSet<String>();
69+
for (String name : names) {
70+
if (!uniqueNames.add(name)) {
71+
throw new IllegalArgumentException("Failed to register Collector of type " + m.getClass().getSimpleName()
72+
+ ": The Collector exposes the same name multiple times: " + name);
73+
}
74+
}
75+
}
76+
6477
/**
6578
* Unregister a Collector.
6679
*/

simpleclient_hotspot/src/main/java/io/prometheus/client/hotspot/ClassLoadingExports.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* </pre>
2121
* Example metrics being exported:
2222
* <pre>
23-
* jvm_classes_loaded{} 1000
23+
* jvm_classes_currently_loaded{} 1000
2424
* jvm_classes_loaded_total{} 2000
2525
* jvm_classes_unloaded_total{} 500
2626
* </pre>
@@ -38,7 +38,7 @@ public ClassLoadingExports(ClassLoadingMXBean clBean) {
3838

3939
void addClassLoadingMetrics(List<MetricFamilySamples> sampleFamilies) {
4040
sampleFamilies.add(new GaugeMetricFamily(
41-
"jvm_classes_loaded",
41+
"jvm_classes_currently_loaded",
4242
"The number of classes that are currently loaded in the JVM",
4343
clBean.getLoadedClassCount()));
4444
sampleFamilies.add(new CounterMetricFamily(

simpleclient_hotspot/src/test/java/io/prometheus/client/hotspot/ClassLoadingExportsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class ClassLoadingExportsTest {
1414

1515
private ClassLoadingMXBean mockClassLoadingsBean = Mockito.mock(ClassLoadingMXBean.class);
16-
private CollectorRegistry registry = new CollectorRegistry();
16+
private CollectorRegistry registry = new CollectorRegistry(true);
1717
private ClassLoadingExports collectorUnderTest;
1818

1919
private static final String[] EMPTY_LABEL = new String[0];
@@ -31,7 +31,7 @@ public void testClassLoading() {
3131
assertEquals(
3232
1000,
3333
registry.getSampleValue(
34-
"jvm_classes_loaded", EMPTY_LABEL, EMPTY_LABEL),
34+
"jvm_classes_currently_loaded", EMPTY_LABEL, EMPTY_LABEL),
3535
.0000001);
3636
assertEquals(
3737
2000L,

0 commit comments

Comments
 (0)