jt2594838 commented on code in PR #18562:
URL: https://github.com/apache/iotdb/pull/18562#discussion_r3910447628


##########
iotdb-core/metrics/interface/src/main/java/org/apache/iotdb/metrics/reporter/prometheus/PrometheusReporter.java:
##########
@@ -135,6 +173,66 @@ public boolean start() {
     return true;
   }
 
+  @SuppressWarnings("unsafeThreadSchedule")
+  private void startSnapshotUpdater() {
+    // Keep metric collection off Reactor HTTP threads and avoid overlapping 
scrapes.
+    if (snapshotUpdateExecutor == null) {
+      snapshotUpdateExecutor =
+          Executors.newSingleThreadScheduledExecutor(
+              runnable -> {
+                Thread thread = new Thread(runnable, 
"prometheus-reporter-snapshot-updater");
+                thread.setDaemon(true);
+                return thread;
+              });
+    }
+    // Delay the first background scrape until metric sets have been bound by 
the metric service.
+    snapshotUpdateFuture =
+        snapshotUpdateExecutor.scheduleAtFixedRate(
+            this::updateSnapshot,
+            PROMETHEUS_DEFAULT_SCRAPE_INTERVAL_SECONDS,
+            PROMETHEUS_DEFAULT_SCRAPE_INTERVAL_SECONDS,
+            TimeUnit.SECONDS);
+  }
+
+  private void updateSnapshot() {
+    try {
+      String snapshot = scrape();
+      // Do not publish an empty scrape taken before metric sets are bound. 
The request path will
+      // synchronously scrape until the first complete snapshot is available. 
Empty snapshots are
+      // published after initialization so removed metrics are not kept in the 
cache indefinitely.
+      if (!snapshot.isEmpty() || metricsSnapshot != null) {

Review Comment:
   The updater ignores only the pre-initialization empty scrape, identified by 
a still-null cache. Once any snapshot has been published, an empty result is 
retained so metrics removed at runtime are reflected instead of leaving stale 
series in the cached response. The asynchronous path remains off Reactor 
request threads.



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