eolivelli commented on code in PR #4300:
URL: https://github.com/apache/bookkeeper/pull/4300#discussion_r1576508383


##########
stats/bookkeeper-stats-providers/prometheus-metrics-provider/src/main/java/org/apache/bookkeeper/stats/prometheus/ThreadScopedLongAdderCounter.java:
##########
@@ -92,15 +92,25 @@ private LongAdderCounter getCounter() {
 
             if (tpt == null) {
                 counters.set(defaultCounter);
-                provider.counters.put(new 
ScopeContext(scopeContext.getScope(), originalLabels), defaultCounter);
+                LongAdderCounter previous = provider.counters
+                        .put(new ScopeContext(scopeContext.getScope(), 
originalLabels), defaultCounter);
+                // If we overwrite a counter, metrics will not be collected 
correctly
+                if (previous != null && previous != defaultCounter) {
+                    throw new IllegalStateException("Invalid state. Overwrote 
a counter.");
+                }
                 return defaultCounter;
             } else {
-                Map<String, String> threadScopedlabels = new 
HashMap<>(originalLabels);
-                threadScopedlabels.put("threadPool", tpt.getThreadPool());
-                threadScopedlabels.put("thread", 
String.valueOf(tpt.getOrdinal()));
+                Map<String, String> threadScopedLabels = new 
HashMap<>(originalLabels);
+                threadScopedLabels.put("threadPool", tpt.getThreadPool());
+                threadScopedLabels.put("thread", 
String.valueOf(tpt.getOrdinal()));
 
-                counter.initializeThread(threadScopedlabels);
-                provider.counters.put(new 
ScopeContext(scopeContext.getScope(), threadScopedlabels), counter);
+                counter.initializeThread(threadScopedLabels);
+                LongAdderCounter previous = provider.counters
+                        .put(new ScopeContext(scopeContext.getScope(), 
threadScopedLabels), counter);
+                // If we overwrite a counter, metrics will not be collected 
correctly
+                if (previous != null) {
+                    throw new IllegalStateException("Invalid state. Overwrote 
a counter.");

Review Comment:
   what about reporting the labels of what is being overwritten ? it will help 
trhoubleshooting 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to