diff --git a/src/main/java/org/opensearch/performanceanalyzer/listener/PerformanceAnalyzerSearchListener.java b/src/main/java/org/opensearch/performanceanalyzer/listener/PerformanceAnalyzerSearchListener.java index fcf7893b..1c011383 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/listener/PerformanceAnalyzerSearchListener.java +++ b/src/main/java/org/opensearch/performanceanalyzer/listener/PerformanceAnalyzerSearchListener.java @@ -32,14 +32,14 @@ public class PerformanceAnalyzerSearchListener private static final SearchListener NO_OP_SEARCH_LISTENER = new NoOpSearchListener(); private static final int KEYS_PATH_LENGTH = 4; private final PerformanceAnalyzerController controller; - private final MetricsRegistry metricsRegistry; + private MetricsRegistry metricsRegistry; private final Counter searchCPUUtilizationCounter; public PerformanceAnalyzerSearchListener(final PerformanceAnalyzerController controller) { this.controller = controller; - this.metricsRegistry = - OpenSearchResources.INSTANCE.getClusterService().getMetricsRegistry(); - if (metricsRegistry != null) { + if (OpenSearchResources.INSTANCE.getClusterService() != null && OpenSearchResources.INSTANCE.getClusterService().getMetricsRegistry() != null) { + this.metricsRegistry = + OpenSearchResources.INSTANCE.getClusterService().getMetricsRegistry(); searchCPUUtilizationCounter = metricsRegistry.createCounter( "pa.core.search.cpuUtilization", "test counter", "1"); diff --git a/src/main/java/org/opensearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java b/src/main/java/org/opensearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java index a80e172b..1afca65c 100644 --- a/src/main/java/org/opensearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java +++ b/src/main/java/org/opensearch/performanceanalyzer/transport/PerformanceAnalyzerTransportRequestHandler.java @@ -33,17 +33,17 @@ public class PerformanceAnalyzerTransportRequestHandler actualHandler, PerformanceAnalyzerController controller) { this.actualHandler = actualHandler; this.controller = controller; + this.metricsRegistry = clusterService.getMetricsRegistry(); this.cpuUtilizationCounter = metricsRegistry.createCounter( "pa.core.cpuUtilizationCounter", "cpuUtilizationCounter", "time"); - metricsRegistry = clusterService.getMetricsRegistry(); } PerformanceAnalyzerTransportRequestHandler set(TransportRequestHandler actualHandler) {