@@ -978,6 +978,52 @@ class ConflatingMetricAggregatorTest extends DDSpecification {
978978 aggregator. close()
979979 }
980980
981+ def " should not report dropped aggregate when evicted entry was already flushed" () {
982+ setup :
983+ int maxAggregates = 5
984+ MetricWriter writer = Mock (MetricWriter )
985+ Sink sink = Stub (Sink )
986+ DDAgentFeaturesDiscovery features = Mock (DDAgentFeaturesDiscovery )
987+ features. supportsMetrics() >> true
988+ features. peerTags() >> []
989+ HealthMetrics healthMetrics = Mock (HealthMetrics )
990+ ConflatingMetricsAggregator aggregator = new ConflatingMetricsAggregator (empty,
991+ features, healthMetrics, sink, writer, maxAggregates, queueSize, reportingInterval, SECONDS , false )
992+ aggregator. start()
993+
994+ when : " fill cache and flush — entries are cleared (hitCount=0) but stay in the LRU"
995+ CountDownLatch latch1 = new CountDownLatch (1 )
996+ for (int i = 0 ; i < maxAggregates; ++ i) {
997+ aggregator. publish([
998+ new SimpleSpan (" service" + i, " operation" , " resource" , " type" , false , true , false , 0 , 100 , HTTP_OK )
999+ .setTag(SPAN_KIND , " baz" )
1000+ ])
1001+ }
1002+ aggregator. report()
1003+ latch1. await(2 , SECONDS )
1004+
1005+ then :
1006+ 1 * writer. finishBucket() >> { latch1. countDown() }
1007+
1008+ when : " publish new distinct spans — LRU evicts the cleared entries before the next report"
1009+ CountDownLatch latch2 = new CountDownLatch (1 )
1010+ for (int i = maxAggregates; i < maxAggregates * 2 ; ++ i) {
1011+ aggregator. publish([
1012+ new SimpleSpan (" service" + i, " operation" , " resource" , " type" , false , true , false , 0 , 100 , HTTP_OK )
1013+ .setTag(SPAN_KIND , " baz" )
1014+ ])
1015+ }
1016+ aggregator. report()
1017+ latch2. await(2 , SECONDS )
1018+
1019+ then : " no drop metric because all evicted entries had hitCount=0 (already reported)"
1020+ 1 * writer. finishBucket() >> { latch2. countDown() }
1021+ 0 * healthMetrics. onStatsAggregateDropped()
1022+
1023+ cleanup :
1024+ aggregator. close()
1025+ }
1026+
9811027 def " aggregate not updated in reporting interval not reported" () {
9821028 setup :
9831029 int maxAggregates = 10
0 commit comments