From 648f69e5c21a882550143155fd47307ede6e8820 Mon Sep 17 00:00:00 2001 From: Gagan Juneja Date: Sun, 17 Nov 2024 13:54:39 +0530 Subject: [PATCH] Skips the counter incremet if value is zero (#764) Signed-off-by: Gagan Juneja Co-authored-by: Gagan Juneja --- .../PerformanceAnalyzerActionFilter.java | 6 ++- ...RTFNodeStatsAllShardsMetricsCollector.java | 38 +++++++++++++------ .../performanceanalyzer/util/Utils.java | 4 +- ...deStatsAllShardsMetricsCollectorTests.java | 19 +++++----- 4 files changed, 44 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/opensearch/performanceanalyzer/action/PerformanceAnalyzerActionFilter.java b/src/main/java/org/opensearch/performanceanalyzer/action/PerformanceAnalyzerActionFilter.java index 5b5436a7..5eb05f89 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/action/PerformanceAnalyzerActionFilter.java +++ b/src/main/java/org/opensearch/performanceanalyzer/action/PerformanceAnalyzerActionFilter.java @@ -17,6 +17,7 @@ import org.opensearch.core.action.ActionListener; import org.opensearch.core.action.ActionResponse; import org.opensearch.performanceanalyzer.commons.metrics.PerformanceAnalyzerMetrics; +import org.opensearch.performanceanalyzer.commons.util.Util; import org.opensearch.performanceanalyzer.config.PerformanceAnalyzerController; import org.opensearch.tasks.Task; @@ -39,7 +40,10 @@ public void app ActionListener listener, ActionFilterChain chain) { - if (controller.isPerformanceAnalyzerEnabled()) { + if (controller.isPerformanceAnalyzerEnabled() + && (controller.getCollectorsRunModeValue() == Util.CollectorMode.DUAL.getValue() + || controller.getCollectorsRunModeValue() + == Util.CollectorMode.RCA.getValue())) { if (request instanceof BulkRequest) { PerformanceAnalyzerActionListener newListener = new PerformanceAnalyzerActionListener<>(); diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java index 6b32b3c5..9bd21757 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollector.java @@ -255,18 +255,34 @@ private void recordMetrics( nodeStatsMetricsTag.addTag( RTFMetrics.CommonDimension.INDEX_UUID.toString(), shardId.getIndex().getUUID()); } + populateOnlyIfNonZero( + cacheQueryMissMetrics, metrics.getQueryCacheMissCount(), nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheQuerySizeMetrics, metrics.getQueryCacheInBytes(), nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheQueryHitMetrics, metrics.getQueryCacheHitCount(), nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheFieldDataEvictionMetrics, + metrics.getFieldDataEvictions(), + nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheFieldDataSizeMetrics, metrics.getFieldDataInBytes(), nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheRequestEvictionMetrics, + metrics.getRequestCacheEvictions(), + nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheRequestHitMetrics, metrics.getRequestCacheHitCount(), nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheRequestMissMetrics, metrics.getRequestCacheMissCount(), nodeStatsMetricsTag); + populateOnlyIfNonZero( + cacheRequestSizeMetrics, metrics.getRequestCacheInBytes(), nodeStatsMetricsTag); + } - cacheQueryMissMetrics.add(metrics.getQueryCacheMissCount(), nodeStatsMetricsTag); - cacheQuerySizeMetrics.add(metrics.getQueryCacheInBytes(), nodeStatsMetricsTag); - cacheQueryHitMetrics.add(metrics.getQueryCacheHitCount(), nodeStatsMetricsTag); - - cacheFieldDataEvictionMetrics.add(metrics.getFieldDataEvictions(), nodeStatsMetricsTag); - cacheFieldDataSizeMetrics.add(metrics.getFieldDataInBytes(), nodeStatsMetricsTag); - - cacheRequestEvictionMetrics.add(metrics.getRequestCacheEvictions(), nodeStatsMetricsTag); - cacheRequestHitMetrics.add(metrics.getRequestCacheHitCount(), nodeStatsMetricsTag); - cacheRequestMissMetrics.add(metrics.getRequestCacheMissCount(), nodeStatsMetricsTag); - cacheRequestSizeMetrics.add(metrics.getRequestCacheInBytes(), nodeStatsMetricsTag); + private void populateOnlyIfNonZero(Counter counter, double value, Tags tags) { + if (value > 0.0) { + counter.add(value, tags); + } } public void populateDiffMetricValue( diff --git a/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java b/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java index 8245c38b..fd788b80 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java +++ b/src/main/java/org/opensearch/performanceanalyzer/util/Utils.java @@ -49,7 +49,9 @@ public static void configureMetrics() { MetricsConfiguration.CONFIG_MAP.put(ShardIndexingPressureMetricsCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put(RTFDisksCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put(RTFHeapMetricsCollector.class, cdefault); - MetricsConfiguration.CONFIG_MAP.put(RTFNodeStatsAllShardsMetricsCollector.class, cdefault); + MetricsConfiguration.CONFIG_MAP.put( + RTFNodeStatsAllShardsMetricsCollector.class, + new MetricsConfiguration.MetricConfig(60000, 0)); MetricsConfiguration.CONFIG_MAP.put(RTFThreadPoolMetricsCollector.class, cdefault); MetricsConfiguration.CONFIG_MAP.put( RTFCacheConfigMetricsCollector.class, diff --git a/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java b/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java index 918c766a..8f88bf19 100644 --- a/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java +++ b/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFNodeStatsAllShardsMetricsCollectorTests.java @@ -9,7 +9,6 @@ import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; -import static org.mockito.Mockito.atLeastOnce; import java.io.IOException; import org.junit.After; @@ -125,14 +124,14 @@ public void testCollectMetrics() throws IOException { rtfNodeStatsAllShardsMetricsCollector.collectMetrics(startTimeInMills); verify(rtfNodeStatsAllShardsMetricsCollector, times(1)) .populateDiffMetricValue(any(), any(), any()); - verify(cacheFieldDataEvictionCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheFieldDataSizeCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheQueryMissCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheQueryHitCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheQuerySizeCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheRequestEvictionCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheRequestHitCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheRequestMissCounter, atLeastOnce()).add(anyDouble(), any()); - verify(cacheRequestSizeCounter, atLeastOnce()).add(anyDouble(), any()); + verify(cacheFieldDataEvictionCounter, never()).add(anyDouble(), any()); + verify(cacheFieldDataSizeCounter, never()).add(anyDouble(), any()); + verify(cacheQueryMissCounter, never()).add(anyDouble(), any()); + verify(cacheQueryHitCounter, never()).add(anyDouble(), any()); + verify(cacheQuerySizeCounter, never()).add(anyDouble(), any()); + verify(cacheRequestEvictionCounter, never()).add(anyDouble(), any()); + verify(cacheRequestHitCounter, never()).add(anyDouble(), any()); + verify(cacheRequestMissCounter, never()).add(anyDouble(), any()); + verify(cacheRequestSizeCounter, never()).add(anyDouble(), any()); } }