diff --git a/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java b/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java index a105cfdf..c5c65fd6 100644 --- a/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java +++ b/src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java @@ -12,10 +12,9 @@ import java.util.List; import java.util.function.Supplier; import org.opensearch.action.ActionRequest; -import org.opensearch.client.Client; import org.opensearch.cluster.metadata.IndexNameExpressionResolver; import org.opensearch.cluster.node.DiscoveryNodes; -import org.opensearch.cluster.service.ClusterService; +import org.opensearch.common.lifecycle.LifecycleComponent; import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.IndexScopedSettings; import org.opensearch.common.settings.Setting; @@ -24,12 +23,7 @@ import org.opensearch.common.unit.TimeValue; import org.opensearch.common.util.concurrent.OpenSearchExecutors; import org.opensearch.core.action.ActionResponse; -import org.opensearch.core.common.io.stream.NamedWriteableRegistry; -import org.opensearch.core.xcontent.NamedXContentRegistry; -import org.opensearch.env.Environment; -import org.opensearch.env.NodeEnvironment; import org.opensearch.plugin.insights.core.listener.QueryInsightsListener; -import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter; import org.opensearch.plugin.insights.core.service.QueryInsightsService; import org.opensearch.plugin.insights.rules.action.health_stats.HealthStatsAction; import org.opensearch.plugin.insights.rules.action.top_queries.TopQueriesAction; @@ -42,16 +36,10 @@ import org.opensearch.plugins.ActionPlugin; import org.opensearch.plugins.Plugin; import org.opensearch.plugins.TelemetryAwarePlugin; -import org.opensearch.repositories.RepositoriesService; import org.opensearch.rest.RestController; import org.opensearch.rest.RestHandler; -import org.opensearch.script.ScriptService; -import org.opensearch.telemetry.metrics.MetricsRegistry; -import org.opensearch.telemetry.tracing.Tracer; import org.opensearch.threadpool.ExecutorBuilder; import org.opensearch.threadpool.ScalingExecutorBuilder; -import org.opensearch.threadpool.ThreadPool; -import org.opensearch.watcher.ResourceWatcherService; /** * Plugin class for Query Insights. @@ -63,32 +51,8 @@ public class QueryInsightsPlugin extends Plugin implements ActionPlugin, Telemet public QueryInsightsPlugin() {} @Override - public Collection createComponents( - final Client client, - final ClusterService clusterService, - final ThreadPool threadPool, - final ResourceWatcherService resourceWatcherService, - final ScriptService scriptService, - final NamedXContentRegistry xContentRegistry, - final Environment environment, - final NodeEnvironment nodeEnvironment, - final NamedWriteableRegistry namedWriteableRegistry, - final IndexNameExpressionResolver indexNameExpressionResolver, - final Supplier repositoriesServiceSupplier, - final Tracer tracer, - final MetricsRegistry metricsRegistry - ) { - // initialize operational metrics counters - OperationalMetricsCounter.initialize(clusterService.getClusterName().toString(), metricsRegistry); - // create top n queries service - final QueryInsightsService queryInsightsService = new QueryInsightsService( - clusterService.getClusterSettings(), - threadPool, - client, - metricsRegistry, - xContentRegistry - ); - return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService, false)); + public Collection> getGuiceServiceClasses() { + return List.of(QueryInsightsService.class, QueryInsightsListener.class); } @Override diff --git a/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java b/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java index 33ea5338..995d4ef1 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java +++ b/src/main/java/org/opensearch/plugin/insights/core/listener/QueryInsightsListener.java @@ -15,6 +15,7 @@ import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNSizeSetting; import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.getTopNWindowSizeSetting; +import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -30,8 +31,10 @@ import org.opensearch.action.search.SearchTask; import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; +import org.opensearch.common.lifecycle.AbstractLifecycleComponent; import org.opensearch.core.tasks.resourcetracker.TaskResourceInfo; import org.opensearch.core.xcontent.ToXContent; +import org.opensearch.indices.IndicesService; import org.opensearch.plugin.insights.core.metrics.OperationalMetric; import org.opensearch.plugin.insights.core.metrics.OperationalMetricsCounter; import org.opensearch.plugin.insights.core.service.QueryInsightsService; @@ -47,7 +50,7 @@ * It forwards query-related data to the appropriate query insights stores, * either for each request or for each phase. */ -public final class QueryInsightsListener extends SearchRequestOperationsListener { +public final class QueryInsightsListener extends SearchRequestOperationsListener implements AbstractLifecycleComponent { private static final ToXContent.Params FORMAT_PARAMS = new ToXContent.MapParams(Collections.singletonMap("pretty", "false")); private static final Logger log = LogManager.getLogger(QueryInsightsListener.class); @@ -60,10 +63,15 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener * * @param clusterService The Node's cluster service. * @param queryInsightsService The topQueriesByLatencyService associated with this listener + * @param indicesService The domain's indices service */ @Inject - public QueryInsightsListener(final ClusterService clusterService, final QueryInsightsService queryInsightsService) { - this(clusterService, queryInsightsService, false); + public QueryInsightsListener( + final ClusterService clusterService, + final QueryInsightsService queryInsightsService, + final IndicesService indicesService + ) { + this(clusterService, queryInsightsService, false, indicesService); } /** @@ -72,11 +80,14 @@ public QueryInsightsListener(final ClusterService clusterService, final QueryIns * @param clusterService The Node's cluster service. * @param queryInsightsService The topQueriesByLatencyService associated with this listener * @param initiallyEnabled Is the listener initially enabled/disabled + * @param indicesService The domain's indices service */ + @Inject public QueryInsightsListener( final ClusterService clusterService, final QueryInsightsService queryInsightsService, - boolean initiallyEnabled + boolean initiallyEnabled, + final IndicesService indicesService ) { super(initiallyEnabled); this.clusterService = clusterService; @@ -268,4 +279,18 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final } } + @Override + protected void doStart() { + + } + + @Override + protected void doStop() { + + } + + @Override + protected void doClose() throws IOException { + + } } diff --git a/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java b/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java index 150f5e3a..73392162 100644 --- a/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java +++ b/src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java @@ -23,9 +23,9 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.opensearch.client.Client; +import org.opensearch.cluster.service.ClusterService; import org.opensearch.common.inject.Inject; import org.opensearch.common.lifecycle.AbstractLifecycleComponent; -import org.opensearch.common.settings.ClusterSettings; import org.opensearch.common.settings.Settings; import org.opensearch.common.unit.TimeValue; import org.opensearch.core.xcontent.NamedXContentRegistry; @@ -102,7 +102,7 @@ public class QueryInsightsService extends AbstractLifecycleComponent { /** * Constructor of the QueryInsightsService * - * @param clusterSettings OpenSearch cluster level settings + * @param clusterService OpenSearch cluster service * @param threadPool The OpenSearch thread pool to run async tasks * @param client OS client * @param metricsRegistry Opentelemetry Metrics registry @@ -110,7 +110,7 @@ public class QueryInsightsService extends AbstractLifecycleComponent { */ @Inject public QueryInsightsService( - final ClusterSettings clusterSettings, + final ClusterService clusterService, final ThreadPool threadPool, final Client client, final MetricsRegistry metricsRegistry, @@ -132,16 +132,20 @@ public QueryInsightsService( ); } for (MetricType type : MetricType.allMetricTypes()) { - clusterSettings.addSettingsUpdateConsumer( - getExporterSettings(type), - (settings -> setExporterAndReader(type, settings)), - (settings -> validateExporterAndReaderConfig(type, settings)) - ); + clusterService.getClusterSettings() + .addSettingsUpdateConsumer( + getExporterSettings(type), + (settings -> setExporterAndReader(type, settings)), + (settings -> validateExporterAndReaderConfig(type, settings)) + ); } this.searchQueryCategorizer = SearchQueryCategorizer.getInstance(metricsRegistry); this.enableSearchQueryMetricsFeature(false); this.groupingType = DEFAULT_GROUPING_TYPE; + + // initialize operational metrics counters + OperationalMetricsCounter.initialize(clusterService.getClusterName().toString(), metricsRegistry); } /**