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


##########
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:
   good suggestion.. will fix this to have single point of sync. 



##########
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:
   good suggestion.. will fix this to have single point of synchronization. 



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