3232import java .util .Map ;
3333import java .util .concurrent .TimeUnit ;
3434
35- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_CPU_QUERIES_ENABLED ;
36- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_CPU_QUERIES_SIZE ;
37- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_CPU_QUERIES_WINDOW_SIZE ;
38- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_LATENCY_QUERIES_ENABLED ;
39- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_LATENCY_QUERIES_SIZE ;
40- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_LATENCY_QUERIES_WINDOW_SIZE ;
41- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_MEMORY_QUERIES_ENABLED ;
42- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_MEMORY_QUERIES_SIZE ;
43- import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_MEMORY_QUERIES_WINDOW_SIZE ;
35+ import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNEnabledSetting ;
36+ import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNSizeSetting ;
37+ import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .getTopNWindowSizeSetting ;
4438
4539/**
4640 * The listener for query insights services.
@@ -67,63 +61,30 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
6761 public QueryInsightsListener (final ClusterService clusterService , final QueryInsightsService queryInsightsService ) {
6862 this .clusterService = clusterService ;
6963 this .queryInsightsService = queryInsightsService ;
70- clusterService .getClusterSettings ()
71- .addSettingsUpdateConsumer (TOP_N_LATENCY_QUERIES_ENABLED , v -> this .setEnableTopQueries (MetricType .LATENCY , v ));
72- clusterService .getClusterSettings ()
73- .addSettingsUpdateConsumer (
74- TOP_N_LATENCY_QUERIES_SIZE ,
75- v -> this .queryInsightsService .getTopQueriesService (MetricType .LATENCY ).setTopNSize (v ),
76- v -> this .queryInsightsService .getTopQueriesService (MetricType .LATENCY ).validateTopNSize (v )
77- );
78- clusterService .getClusterSettings ()
79- .addSettingsUpdateConsumer (
80- TOP_N_LATENCY_QUERIES_WINDOW_SIZE ,
81- v -> this .queryInsightsService .getTopQueriesService (MetricType .LATENCY ).setWindowSize (v ),
82- v -> this .queryInsightsService .getTopQueriesService (MetricType .LATENCY ).validateWindowSize (v )
83- );
84- clusterService .getClusterSettings ()
85- .addSettingsUpdateConsumer (TOP_N_CPU_QUERIES_ENABLED , v -> this .setEnableTopQueries (MetricType .CPU , v ));
86- clusterService .getClusterSettings ()
87- .addSettingsUpdateConsumer (
88- TOP_N_CPU_QUERIES_SIZE ,
89- v -> this .queryInsightsService .getTopQueriesService (MetricType .CPU ).setTopNSize (v ),
90- v -> this .queryInsightsService .getTopQueriesService (MetricType .CPU ).validateTopNSize (v )
91- );
92- clusterService .getClusterSettings ()
93- .addSettingsUpdateConsumer (
94- TOP_N_CPU_QUERIES_WINDOW_SIZE ,
95- v -> this .queryInsightsService .getTopQueriesService (MetricType .CPU ).setWindowSize (v ),
96- v -> this .queryInsightsService .getTopQueriesService (MetricType .CPU ).validateWindowSize (v )
97- );
98- clusterService .getClusterSettings ()
99- .addSettingsUpdateConsumer (TOP_N_MEMORY_QUERIES_ENABLED , v -> this .setEnableTopQueries (MetricType .MEMORY , v ));
100- clusterService .getClusterSettings ()
101- .addSettingsUpdateConsumer (
102- TOP_N_MEMORY_QUERIES_SIZE ,
103- v -> this .queryInsightsService .getTopQueriesService (MetricType .MEMORY ).setTopNSize (v ),
104- v -> this .queryInsightsService .getTopQueriesService (MetricType .MEMORY ).validateTopNSize (v )
105- );
106- clusterService .getClusterSettings ()
107- .addSettingsUpdateConsumer (
108- TOP_N_MEMORY_QUERIES_WINDOW_SIZE ,
109- v -> this .queryInsightsService .getTopQueriesService (MetricType .MEMORY ).setWindowSize (v ),
110- v -> this .queryInsightsService .getTopQueriesService (MetricType .MEMORY ).validateWindowSize (v )
111- );
112- this .setEnableTopQueries (MetricType .LATENCY , clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_ENABLED ));
113- this .queryInsightsService .getTopQueriesService (MetricType .LATENCY )
114- .setTopNSize (clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_SIZE ));
115- this .queryInsightsService .getTopQueriesService (MetricType .LATENCY )
116- .setWindowSize (clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_WINDOW_SIZE ));
117- this .setEnableTopQueries (MetricType .CPU , clusterService .getClusterSettings ().get (TOP_N_CPU_QUERIES_ENABLED ));
118- this .queryInsightsService .getTopQueriesService (MetricType .CPU )
119- .setTopNSize (clusterService .getClusterSettings ().get (TOP_N_CPU_QUERIES_SIZE ));
120- this .queryInsightsService .getTopQueriesService (MetricType .CPU )
121- .setWindowSize (clusterService .getClusterSettings ().get (TOP_N_CPU_QUERIES_WINDOW_SIZE ));
122- this .setEnableTopQueries (MetricType .MEMORY , clusterService .getClusterSettings ().get (TOP_N_MEMORY_QUERIES_ENABLED ));
123- this .queryInsightsService .getTopQueriesService (MetricType .MEMORY )
124- .setTopNSize (clusterService .getClusterSettings ().get (TOP_N_MEMORY_QUERIES_SIZE ));
125- this .queryInsightsService .getTopQueriesService (MetricType .MEMORY )
126- .setWindowSize (clusterService .getClusterSettings ().get (TOP_N_MEMORY_QUERIES_WINDOW_SIZE ));
64+ // Setting endpoints set up for top n queries, including enabling top n queries, window size and top n size
65+ // Expected metricTypes are Latency, CPU and Memory.
66+ for (MetricType type : MetricType .allMetricTypes ()) {
67+ clusterService .getClusterSettings ()
68+ .addSettingsUpdateConsumer (getTopNEnabledSetting (type ), v -> this .setEnableTopQueries (type , v ));
69+ clusterService .getClusterSettings ()
70+ .addSettingsUpdateConsumer (
71+ getTopNSizeSetting (type ),
72+ v -> this .queryInsightsService .setTopNSize (type , v ),
73+ v -> this .queryInsightsService .validateTopNSize (type , v )
74+ );
75+ clusterService .getClusterSettings ()
76+ .addSettingsUpdateConsumer (
77+ getTopNWindowSizeSetting (type ),
78+ v -> this .queryInsightsService .setWindowSize (type , v ),
79+ v -> this .queryInsightsService .validateWindowSize (type , v )
80+ );
81+
82+ this .setEnableTopQueries (type , clusterService .getClusterSettings ().get (getTopNEnabledSetting (type )));
83+ this .queryInsightsService .validateTopNSize (type , clusterService .getClusterSettings ().get (getTopNSizeSetting (type )));
84+ this .queryInsightsService .setTopNSize (type , clusterService .getClusterSettings ().get (getTopNSizeSetting (type )));
85+ this .queryInsightsService .validateWindowSize (type , clusterService .getClusterSettings ().get (getTopNWindowSizeSetting (type )));
86+ this .queryInsightsService .setWindowSize (type , clusterService .getClusterSettings ().get (getTopNWindowSizeSetting (type )));
87+ }
12788 }
12889
12990 /**
@@ -175,6 +136,7 @@ public void onRequestStart(SearchRequestContext searchRequestContext) {}
175136 public void onRequestEnd (final SearchPhaseContext context , final SearchRequestContext searchRequestContext ) {
176137 constructSearchQueryRecord (context , searchRequestContext );
177138 }
139+
178140 @ Override
179141 public void onRequestFailure (final SearchPhaseContext context , final SearchRequestContext searchRequestContext ) {
180142 constructSearchQueryRecord (context , searchRequestContext );
@@ -220,7 +182,7 @@ private void constructSearchQueryRecord(final SearchPhaseContext context, final
220182 attributes .put (Attribute .TOTAL_SHARDS , context .getNumShards ());
221183 attributes .put (Attribute .INDICES , request .indices ());
222184 attributes .put (Attribute .PHASE_LATENCY_MAP , searchRequestContext .phaseTookMap ());
223- attributes .put (Attribute .TASKS_RESOURCE_USAGES , tasksResourceUsages );
185+ attributes .put (Attribute .TASK_RESOURCE_USAGES , tasksResourceUsages );
224186
225187 Map <String , Object > labels = new HashMap <>();
226188 // Retrieve user provided label if exists
0 commit comments