rmahindra123 commented on code in PR #13851:
URL: https://github.com/apache/hudi/pull/13851#discussion_r2334472255


##########
hudi-common/src/main/java/org/apache/hudi/metrics/prometheus/PrometheusReporter.java:
##########
@@ -70,23 +103,39 @@ public PrometheusReporter(HoodieMetricsConfig 
metricsConfig, MetricRegistry regi
           });
     }
     metricExports = new DropwizardExports(registry, new 
LabeledSampleBuilder(labelNames, labelValues));
-    this.collectorRegistry = PORT_TO_COLLECTOR_REGISTRY.get(serverPort);
-    metricExports.register(collectorRegistry);
+    
+    synchronized (PrometheusReporter.class) {
+      PrometheusServerState serverState = PORT_TO_SERVER_STATE.get(serverPort);
+      this.collectorRegistry = serverState.getCollectorRegistry();
+      metricExports.register(collectorRegistry);
+      serverState.getExports().add(metricExports);
+      serverState.getReferenceCount().incrementAndGet();
+      
+      LOG.debug("Registered PrometheusReporter for port {}, reference count: 
{}", 
+               serverPort, serverState.getReferenceCount().get());
+    }
   }
 
-  private static synchronized void startHttpServer(int serverPort) {
-    if (!PORT_TO_COLLECTOR_REGISTRY.containsKey(serverPort)) {
-      PORT_TO_COLLECTOR_REGISTRY.put(serverPort, new CollectorRegistry());
-    }
-    if (!PORT_TO_SERVER.containsKey(serverPort)) {
-      try {
-        HTTPServer server = new HTTPServer(new InetSocketAddress(serverPort), 
PORT_TO_COLLECTOR_REGISTRY.get(serverPort), true);
-        PORT_TO_SERVER.put(serverPort, server);
-        Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
-      } catch (Exception e) {
-        String msg = "Could not start PrometheusReporter HTTP server on port " 
+ serverPort;
-        LOG.error(msg, e);
-        throw new HoodieException(msg, e);
+  private static void startHttpServer(int serverPort) {

Review Comment:
   can we just use this method to get the serverState and if not available, 
initialize and return the serverSate? 
   private synchronized static ServerState getServerState(int serverPort).
   Then we can move the codes 108->116 here as well, and make it a single place 
to take class level locks.



-- 
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