8
8
9
9
package org .opensearch .plugin .insights .core .listener ;
10
10
11
+ import static org .opensearch .plugin .insights .settings .QueryCategorizationSettings .SEARCH_QUERY_METRICS_ENABLED_SETTING ;
11
12
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNEnabledSetting ;
12
13
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNSizeSetting ;
13
14
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNWindowSizeSetting ;
@@ -56,10 +57,27 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
56
57
*/
57
58
@ Inject
58
59
public QueryInsightsListener (final ClusterService clusterService , final QueryInsightsService queryInsightsService ) {
60
+ this (clusterService , queryInsightsService , false );
61
+ }
62
+
63
+ /**
64
+ * Constructor for QueryInsightsListener
65
+ *
66
+ * @param clusterService The Node's cluster service.
67
+ * @param queryInsightsService The topQueriesByLatencyService associated with this listener
68
+ * @param initiallyEnabled Is the listener initially enabled/disabled
69
+ */
70
+ public QueryInsightsListener (
71
+ final ClusterService clusterService ,
72
+ final QueryInsightsService queryInsightsService ,
73
+ boolean initiallyEnabled
74
+ ) {
75
+ super (initiallyEnabled );
59
76
this .clusterService = clusterService ;
60
77
this .queryInsightsService = queryInsightsService ;
61
- // Setting endpoints set up for top n queries, including enabling top n queries, window size and top n size
62
- // Expected metricTypes are Latency, CPU and Memory.
78
+
79
+ // Setting endpoints set up for top n queries, including enabling top n queries, window size, and top n size
80
+ // Expected metricTypes are Latency, CPU, and Memory.
63
81
for (MetricType type : MetricType .allMetricTypes ()) {
64
82
clusterService .getClusterSettings ()
65
83
.addSettingsUpdateConsumer (getTopNEnabledSetting (type ), v -> this .setEnableTopQueries (type , v ));
@@ -82,31 +100,48 @@ public QueryInsightsListener(final ClusterService clusterService, final QueryIns
82
100
this .queryInsightsService .validateWindowSize (type , clusterService .getClusterSettings ().get (getTopNWindowSizeSetting (type )));
83
101
this .queryInsightsService .setWindowSize (type , clusterService .getClusterSettings ().get (getTopNWindowSizeSetting (type )));
84
102
}
103
+
104
+ clusterService .getClusterSettings ()
105
+ .addSettingsUpdateConsumer (SEARCH_QUERY_METRICS_ENABLED_SETTING , v -> setSearchQueryMetricsEnabled (v ));
106
+ setSearchQueryMetricsEnabled (clusterService .getClusterSettings ().get (SEARCH_QUERY_METRICS_ENABLED_SETTING ));
85
107
}
86
108
87
109
/**
88
- * Enable or disable top queries insights collection for {@link MetricType}
110
+ * Enable or disable top queries insights collection for {@link MetricType}.
89
111
* This function will enable or disable the corresponding listeners
90
112
* and query insights services.
91
113
*
92
114
* @param metricType {@link MetricType}
93
115
* @param isCurrentMetricEnabled boolean
94
116
*/
95
117
public void setEnableTopQueries (final MetricType metricType , final boolean isCurrentMetricEnabled ) {
96
- boolean isTopNFeaturePreviouslyDisabled = !queryInsightsService .isTopNFeatureEnabled ();
97
118
this .queryInsightsService .enableCollection (metricType , isCurrentMetricEnabled );
98
- boolean isTopNFeatureCurrentlyDisabled = !queryInsightsService .isTopNFeatureEnabled ();
119
+ updateQueryInsightsState ();
120
+ }
99
121
100
- if (isTopNFeatureCurrentlyDisabled ) {
101
- super .setEnabled (false );
102
- if (!isTopNFeaturePreviouslyDisabled ) {
103
- queryInsightsService .checkAndStopQueryInsights ();
104
- }
105
- } else {
122
+ /**
123
+ * Set search query metrics enabled to enable collection of search query categorization metrics.
124
+ * @param searchQueryMetricsEnabled boolean flag
125
+ */
126
+ public void setSearchQueryMetricsEnabled (boolean searchQueryMetricsEnabled ) {
127
+ this .queryInsightsService .enableSearchQueryMetricsFeature (searchQueryMetricsEnabled );
128
+ updateQueryInsightsState ();
129
+ }
130
+
131
+ /**
132
+ * Update the query insights service state based on the enabled features.
133
+ * If any feature is enabled, it starts the service. If no features are enabled, it stops the service.
134
+ */
135
+ private void updateQueryInsightsState () {
136
+ boolean anyFeatureEnabled = queryInsightsService .isAnyFeatureEnabled ();
137
+
138
+ if (anyFeatureEnabled && !super .isEnabled ()) {
106
139
super .setEnabled (true );
107
- if (isTopNFeaturePreviouslyDisabled ) {
108
- queryInsightsService .checkAndRestartQueryInsights ();
109
- }
140
+ queryInsightsService .stop (); // Ensures a clean restart
141
+ queryInsightsService .start ();
142
+ } else if (!anyFeatureEnabled && super .isEnabled ()) {
143
+ super .setEnabled (false );
144
+ queryInsightsService .stop ();
110
145
}
111
146
}
112
147
0 commit comments