aho135 commented on code in PR #18689:
URL: https://github.com/apache/druid/pull/18689#discussion_r2466987527


##########
extensions-contrib/prometheus-emitter/src/test/java/org/apache/druid/emitter/prometheus/PrometheusEmitterTest.java:
##########
@@ -507,14 +510,81 @@ public void testMetricTtlUpdate()
 
     Assert.assertFalse(
         "Metric should not be expired",
-        testMetric.isExpired()
+        testMetric.removeIfExpired(Arrays.asList("historical", 
"druid.test.cn", "historical1"))
     );
-    emitter.emit(event);
+    emitter.close();
+  }
+
+  @Test
+  public void testMetricTtlUpdateWithDifferentLabels()
+  {
+    int flushPeriod = 3;
+    PrometheusEmitterConfig config = new 
PrometheusEmitterConfig(PrometheusEmitterConfig.Strategy.exporter, "test", 
null, 0, null, true, true, flushPeriod, null, false, null);
+    PrometheusEmitter emitter = new PrometheusEmitter(config);
+    emitter.start();
+
+    ServiceMetricEvent event1 = ServiceMetricEvent.builder()
+                                                 
.setMetric("segment/loadQueue/count", 10)
+                                                 .setDimension("server", 
"historical1")
+                                                 
.build(ImmutableMap.of("service", "historical", "host", "druid.test.cn"));
+    ServiceMetricEvent event2 = ServiceMetricEvent.builder()
+                                                  
.setMetric("segment/loadQueue/count", 10)
+                                                  .setDimension("server", 
"historical2")
+                                                  
.build(ImmutableMap.of("service", "historical", "host", "druid.test.cn"));
+    emitter.emit(event1);
+    emitter.emit(event2);
+
+    // Get the metrics and check that it's not expired initially
+    Map<String, DimensionsAndCollector> registeredMetrics = 
emitter.getMetrics().getRegisteredMetrics();
+    DimensionsAndCollector testMetric = 
registeredMetrics.get("segment/loadQueue/count");
 
-    long timeSinceLastUpdate = testMetric.getMillisSinceLastUpdate();
+    Assert.assertNotNull(
+        "Test metric should be registered",
+        testMetric
+    );
+    Assert.assertFalse(
+        "Metric should not be expired initially",
+        testMetric.removeIfExpired(Arrays.asList("historical", 
"druid.test.cn", "historical1"))
+    );
+    Assert.assertFalse(
+        "Metric should not be expired initially",
+        testMetric.removeIfExpired(Arrays.asList("historical", 
"druid.test.cn", "historical2"))
+    );
+
+    // Wait for a little, but not long enough for the metric to expire
+    long waitTime = TimeUnit.SECONDS.toMillis(flushPeriod) / 5;
+    try {
+      Thread.sleep(waitTime);
+    }
+    catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+    }
+
+    Assert.assertFalse(
+        "Metric should not be expired",
+        testMetric.removeIfExpired(Arrays.asList("historical", 
"druid.test.cn", "historical1"))
+    );
+    Assert.assertFalse(
+        "Metric should not be expired",
+        testMetric.removeIfExpired(Arrays.asList("historical", 
"druid.test.cn", "historical2"))
+    );
+    // Reset update time only for event2
+    emitter.emit(event2);
+
+    try {
+      // Wait for the remainder of the TTL to allow event1 to expire
+      Thread.sleep(waitTime * 4);
+    }
+    catch (InterruptedException e) {
+      Thread.currentThread().interrupt();
+    }
     Assert.assertTrue(
-        "Update time should have been refreshed",
-        timeSinceLastUpdate < waitTime
+        "Metric should be expired",
+        testMetric.removeIfExpired(Arrays.asList("historical", 
"druid.test.cn", "historical1"))
+    );
+    Assert.assertFalse(

Review Comment:
   Yeah agreed. Ideally I'd wanted to check SimpleCollector.children and ensure 
that the labels were removed by  `cleanUpStaleMetrics()`. Unfortunately the 
SimpleCollector doesn't expose any function to access the children Map. I did 
verify via the debugger that the entry is removed from the child map after the 
TTL though
   
   The tests do actually run `cleanUpStaleMetrics()` because the executor is 
created in `emitter.start()` and manual sleep times allow the scheduled 
executor to run



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to