diff --git a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java index b5bc67a8..440c8664 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java +++ b/src/main/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollector.java @@ -124,6 +124,9 @@ private void closeOpenGaugeObservablesIfAny() { requestCacheGauge = null; } } + if (metricsInitialised) { + metricsInitialised = false; + } } private double getRequestCacheMaxSizeStatus(IndicesService indicesService) { diff --git a/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollectorTests.java b/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollectorTests.java index c8d5b52a..7a197c3b 100644 --- a/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollectorTests.java +++ b/src/test/java/org/opensearch/performanceanalyzer/collectors/telemetry/RTFCacheConfigMetricsCollectorTests.java @@ -7,12 +7,15 @@ import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import java.io.Closeable; import java.io.IOException; import org.junit.After; import org.junit.Before; @@ -31,6 +34,7 @@ public class RTFCacheConfigMetricsCollectorTests extends OpenSearchSingleNodeTes private RTFCacheConfigMetricsCollector rtfCacheConfigMetricsCollector; private static MetricsRegistry metricsRegistry; private static MetricsRegistry metricsRegistry1; + PerformanceAnalyzerController mockController; private long startTimeInMills = 1153721339; @Before @@ -43,7 +47,7 @@ public void init() { IndicesService indicesService = getInstanceFromNode(IndicesService.class); OpenSearchResources.INSTANCE.setIndicesService(indicesService); ConfigOverridesWrapper mockWrapper = mock(ConfigOverridesWrapper.class); - PerformanceAnalyzerController mockController = mock(PerformanceAnalyzerController.class); + mockController = mock(PerformanceAnalyzerController.class); Mockito.when(mockController.isCollectorDisabled(any(), anyString())).thenReturn(false); rtfCacheConfigMetricsCollector = spy(new RTFCacheConfigMetricsCollector(mockController, mockWrapper)); @@ -74,4 +78,39 @@ public void testCollectMetricsRepeated() throws IOException { verify(metricsRegistry1, never()) .createGauge(anyString(), anyString(), anyString(), any(), any()); } + + @Test + public void testCollectMetricsDisableEnable() throws IOException { + createIndex(TEST_INDEX); + Closeable fieldCacheGaugeObservable = Mockito.mock(Closeable.class); + Closeable requestCacheGaugeObservable = Mockito.mock(Closeable.class); + + Mockito.when( + metricsRegistry.createGauge( + anyString(), anyString(), anyString(), any(), any())) + .thenReturn(requestCacheGaugeObservable, fieldCacheGaugeObservable); + rtfCacheConfigMetricsCollector.collectMetrics(startTimeInMills); + verify(metricsRegistry, times(2)) + .createGauge(anyString(), anyString(), anyString(), any(), any()); + + OpenSearchResources.INSTANCE.setMetricsRegistry(metricsRegistry1); + Mockito.when( + mockController.isCollectorDisabled( + any(), eq("RTFCacheConfigMetricsCollector"))) + .thenReturn(true); + rtfCacheConfigMetricsCollector.collectMetrics(startTimeInMills); + verify(fieldCacheGaugeObservable, Mockito.only()).close(); + verify(requestCacheGaugeObservable, Mockito.only()).close(); + verify(metricsRegistry1, never()) + .createGauge(anyString(), anyString(), anyString(), any(), any()); + + OpenSearchResources.INSTANCE.setMetricsRegistry(metricsRegistry1); + Mockito.when( + mockController.isCollectorDisabled( + any(), eq("RTFCacheConfigMetricsCollector"))) + .thenReturn(false); + rtfCacheConfigMetricsCollector.collectMetrics(startTimeInMills); + verify(metricsRegistry1, times(2)) + .createGauge(anyString(), anyString(), anyString(), any(), any()); + } }