19
19
import org .opensearch .core .xcontent .ToXContent ;
20
20
import org .opensearch .plugin .insights .core .service .QueryInsightsService ;
21
21
import org .opensearch .plugin .insights .rules .model .Attribute ;
22
- import org .opensearch .plugin .insights .rules .model .Measurement ;
23
22
import org .opensearch .plugin .insights .rules .model .MetricType ;
24
23
import org .opensearch .plugin .insights .rules .model .SearchQueryRecord ;
25
24
34
33
import static org .opensearch .plugin .insights .settings .QueryInsightsSettings .TOP_N_LATENCY_QUERIES_WINDOW_SIZE ;
35
34
36
35
/**
37
- * The listener for top N queries by latency
36
+ * The listener for query insights services.
37
+ * It forwards query-related data to the appropriate query insights stores,
38
+ * either for each request or for each phase.
38
39
*
39
40
* @opensearch.internal
40
41
*/
@@ -52,47 +53,53 @@ public final class QueryInsightsListener extends SearchRequestOperationsListener
52
53
* @param queryInsightsService The topQueriesByLatencyService associated with this listener
53
54
*/
54
55
@ Inject
55
- public QueryInsightsListener (ClusterService clusterService , QueryInsightsService queryInsightsService ) {
56
+ public QueryInsightsListener (final ClusterService clusterService , final QueryInsightsService queryInsightsService ) {
56
57
this .queryInsightsService = queryInsightsService ;
57
58
clusterService .getClusterSettings ()
58
- .addSettingsUpdateConsumer (TOP_N_LATENCY_QUERIES_ENABLED , v -> this .setEnabled (MetricType .LATENCY , v ));
59
+ .addSettingsUpdateConsumer (TOP_N_LATENCY_QUERIES_ENABLED , v -> this .setEnableTopQueries (MetricType .LATENCY , v ));
59
60
clusterService .getClusterSettings ()
60
61
.addSettingsUpdateConsumer (
61
62
TOP_N_LATENCY_QUERIES_SIZE ,
62
- this .queryInsightsService :: setTopNSize ,
63
- this .queryInsightsService :: validateTopNSize
63
+ v -> this .queryInsightsService . getTopQueriesService ( MetricType . LATENCY ). setTopNSize ( v ) ,
64
+ v -> this .queryInsightsService . getTopQueriesService ( MetricType . LATENCY ). validateTopNSize ( v )
64
65
);
65
66
clusterService .getClusterSettings ()
66
67
.addSettingsUpdateConsumer (
67
68
TOP_N_LATENCY_QUERIES_WINDOW_SIZE ,
68
- this .queryInsightsService :: setWindowSize ,
69
- this .queryInsightsService :: validateWindowSize
69
+ v -> this .queryInsightsService . getTopQueriesService ( MetricType . LATENCY ). setWindowSize ( v ) ,
70
+ v -> this .queryInsightsService . getTopQueriesService ( MetricType . LATENCY ). validateWindowSize ( v )
70
71
);
71
- this .setEnabled (MetricType .LATENCY , clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_ENABLED ));
72
- this .queryInsightsService .setTopNSize (clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_SIZE ));
73
- this .queryInsightsService .setWindowSize (clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_WINDOW_SIZE ));
72
+ this .setEnableTopQueries (MetricType .LATENCY , clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_ENABLED ));
73
+ this .queryInsightsService .getTopQueriesService (MetricType .LATENCY )
74
+ .setTopNSize (clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_SIZE ));
75
+ this .queryInsightsService .getTopQueriesService (MetricType .LATENCY )
76
+ .setWindowSize (clusterService .getClusterSettings ().get (TOP_N_LATENCY_QUERIES_WINDOW_SIZE ));
74
77
}
75
78
76
79
/**
77
- * Enable or disable metric collection for {@link MetricType}
80
+ * Enable or disable top queries insights collection for {@link MetricType}
81
+ * This function will enable or disable the corresponding listeners
82
+ * and query insights services.
78
83
*
79
84
* @param metricType {@link MetricType}
80
85
* @param enabled boolean
81
86
*/
82
- public void setEnabled (MetricType metricType , boolean enabled ) {
83
- this .queryInsightsService .enableCollection (metricType , enabled );
84
-
85
- // disable QueryInsightsListener only if collection for all metrics are disabled.
87
+ public void setEnableTopQueries (final MetricType metricType , final boolean enabled ) {
86
88
if (!enabled ) {
89
+ // disable QueryInsightsListener only if all metrics collections are disabled.
87
90
for (MetricType t : MetricType .allMetricTypes ()) {
88
91
if (this .queryInsightsService .isCollectionEnabled (t )) {
92
+ this .queryInsightsService .getTopQueriesService (metricType ).setEnabled (false );
89
93
return ;
90
94
}
91
95
}
92
96
super .setEnabled (false );
97
+ this .queryInsightsService .stop ();
93
98
} else {
94
99
super .setEnabled (true );
100
+ this .queryInsightsService .start ();
95
101
}
102
+ this .queryInsightsService .enableCollection (metricType , enabled );
96
103
}
97
104
98
105
@ Override
@@ -113,17 +120,14 @@ public void onPhaseFailure(SearchPhaseContext context) {}
113
120
public void onRequestStart (SearchRequestContext searchRequestContext ) {}
114
121
115
122
@ Override
116
- public void onRequestEnd (SearchPhaseContext context , SearchRequestContext searchRequestContext ) {
117
- SearchRequest request = context .getRequest ();
123
+ public void onRequestEnd (final SearchPhaseContext context , final SearchRequestContext searchRequestContext ) {
124
+ final SearchRequest request = context .getRequest ();
118
125
try {
119
- Map <MetricType , Measurement <? extends Number > > measurements = new HashMap <>();
126
+ Map <MetricType , Number > measurements = new HashMap <>();
120
127
if (queryInsightsService .isCollectionEnabled (MetricType .LATENCY )) {
121
128
measurements .put (
122
129
MetricType .LATENCY ,
123
- new Measurement <>(
124
- MetricType .LATENCY .name (),
125
- TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - searchRequestContext .getAbsoluteStartNanos ())
126
- )
130
+ TimeUnit .NANOSECONDS .toMillis (System .nanoTime () - searchRequestContext .getAbsoluteStartNanos ())
127
131
);
128
132
}
129
133
Map <Attribute , Object > attributes = new HashMap <>();
0 commit comments