Skip to content

Commit fbc54da

Browse files
committed
Add default index template for query insights local index
Signed-off-by: Chenyang Ji <cyji@amazon.com>
1 parent 5ef5437 commit fbc54da

9 files changed

+587
-137
lines changed

src/main/java/org/opensearch/plugin/insights/QueryInsightsPlugin.java

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public List<Setting<?>> getSettings() {
148148
QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING,
149149
QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER,
150150
QueryInsightsSettings.TOP_N_EXPORTER_TYPE,
151+
QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY,
151152
QueryCategorizationSettings.SEARCH_QUERY_FIELD_TYPE_CACHE_SIZE_KEY
152153
);
153154
}

src/main/java/org/opensearch/plugin/insights/core/exporter/LocalIndexExporter.java

+225-66
Large diffs are not rendered by default.

src/main/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterFactory.java

+22-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
package org.opensearch.plugin.insights.core.exporter;
1010

11+
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.DEFAULT_TEMPLATE_PRIORITY;
12+
1113
import java.io.IOException;
1214
import java.time.format.DateTimeFormatter;
1315
import java.util.HashMap;
@@ -74,20 +76,34 @@ public void validateExporterType(final String exporterType) throws IllegalArgume
7476
* @param id id of the exporter so that exporters can be retrieved and reused across services
7577
* @param indexPattern the index pattern if creating an index exporter
7678
* @param indexMapping index mapping file
79+
* @param templatePriority the priority value for the template
7780
* @return LocalIndexExporter the created exporter sink
7881
*/
79-
public LocalIndexExporter createLocalIndexExporter(String id, String indexPattern, String indexMapping) {
82+
public LocalIndexExporter createLocalIndexExporter(String id, String indexPattern, String indexMapping, long templatePriority) {
8083
LocalIndexExporter exporter = new LocalIndexExporter(
8184
client,
8285
clusterService,
8386
DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT),
8487
indexMapping,
8588
id
8689
);
90+
exporter.setTemplatePriority(templatePriority);
8791
this.exporters.put(id, exporter);
8892
return exporter;
8993
}
9094

95+
/**
96+
* Create a local index exporter based on provided parameters, using default template priority
97+
*
98+
* @param id id of the exporter so that exporters can be retrieved and reused across services
99+
* @param indexPattern the index pattern if creating an index exporter
100+
* @param indexMapping index mapping file
101+
* @return LocalIndexExporter the created exporter sink
102+
*/
103+
public LocalIndexExporter createLocalIndexExporter(String id, String indexPattern, String indexMapping) {
104+
return createLocalIndexExporter(id, indexPattern, indexMapping, DEFAULT_TEMPLATE_PRIORITY);
105+
}
106+
91107
/**
92108
* Create a debug exporter based on provided parameters
93109
*
@@ -105,11 +121,14 @@ public DebugExporter createDebugExporter(String id) {
105121
*
106122
* @param exporter The exporter to update
107123
* @param indexPattern the index pattern if creating a index exporter
124+
* @param templatePriority the priority value for the template (for LocalIndexExporter)
108125
* @return QueryInsightsExporter the updated exporter sink
109126
*/
110-
public QueryInsightsExporter updateExporter(QueryInsightsExporter exporter, String indexPattern) {
127+
public QueryInsightsExporter updateExporter(QueryInsightsExporter exporter, String indexPattern, long templatePriority) {
111128
if (exporter.getClass() == LocalIndexExporter.class) {
112-
((LocalIndexExporter) exporter).setIndexPattern(DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT));
129+
LocalIndexExporter localExporter = (LocalIndexExporter) exporter;
130+
localExporter.setIndexPattern(DateTimeFormatter.ofPattern(indexPattern, Locale.ROOT));
131+
localExporter.setTemplatePriority(templatePriority);
113132
}
114133
return exporter;
115134
}

src/main/java/org/opensearch/plugin/insights/core/service/QueryInsightsService.java

+36
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.MIN_DELETE_AFTER_VALUE;
1818
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.QUERY_INSIGHTS_EXECUTOR;
1919
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER;
20+
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY;
2021
import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.TOP_N_EXPORTER_TYPE;
2122

2223
import java.io.IOException;
@@ -174,9 +175,12 @@ public QueryInsightsService(
174175
(this::setExporterDeleteAfterAndDelete),
175176
(this::validateExporterDeleteAfter)
176177
);
178+
clusterService.getClusterSettings()
179+
.addSettingsUpdateConsumer(TOP_N_EXPORTER_TEMPLATE_PRIORITY, this::setTemplatePriority, this::validateTemplatePriority);
177180

178181
this.setExporterDeleteAfterAndDelete(clusterService.getClusterSettings().get(TOP_N_EXPORTER_DELETE_AFTER));
179182
this.setExporterAndReaderType(SinkType.parse(clusterService.getClusterSettings().get(TOP_N_EXPORTER_TYPE)));
183+
this.setTemplatePriority(clusterService.getClusterSettings().get(TOP_N_EXPORTER_TEMPLATE_PRIORITY));
180184

181185
this.searchQueryCategorizer = SearchQueryCategorizer.getInstance(metricsRegistry);
182186
this.enableSearchQueryMetricsFeature(false);
@@ -527,6 +531,38 @@ public void validateExporterType(final String exporterType) {
527531
queryInsightsExporterFactory.validateExporterType(exporterType);
528532
}
529533

534+
/**
535+
* Set the template priority for all top queries exporters
536+
*
537+
* @param templatePriority the priority value to use for templates
538+
*/
539+
public void setTemplatePriority(final long templatePriority) {
540+
logger.info("Setting query insights index template priority to [{}]", templatePriority);
541+
final QueryInsightsExporter topQueriesExporter = queryInsightsExporterFactory.getExporter(TOP_QUERIES_EXPORTER_ID);
542+
if (topQueriesExporter != null && topQueriesExporter.getClass() == LocalIndexExporter.class) {
543+
logger.debug("Updating query insights index template priority for top queries exporter to [{}]", templatePriority);
544+
queryInsightsExporterFactory.updateExporter(
545+
topQueriesExporter,
546+
QueryInsightsSettings.DEFAULT_TOP_N_QUERIES_INDEX_PATTERN,
547+
templatePriority
548+
);
549+
}
550+
}
551+
552+
/**
553+
* Validate the template priority
554+
*
555+
* @param templatePriority the priority value to validate
556+
*/
557+
public void validateTemplatePriority(final long templatePriority) {
558+
if (templatePriority < 0) {
559+
OperationalMetricsCounter.getInstance().incrementCounter(OperationalMetric.INVALID_EXPORTER_TYPE_FAILURES);
560+
throw new IllegalArgumentException(
561+
String.format(Locale.ROOT, "Invalid template priority setting [%d], value should be a non-negative long.", templatePriority)
562+
);
563+
}
564+
}
565+
530566
@Override
531567
protected void doStart() {
532568
if (isAnyFeatureEnabled()) {

src/main/java/org/opensearch/plugin/insights/settings/QueryInsightsSettings.java

+20
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ public class QueryInsightsSettings {
230230
* Default exporter type of top queries
231231
*/
232232
public static final String DEFAULT_TOP_QUERIES_EXPORTER_TYPE = SinkType.LOCAL_INDEX.toString();
233+
/**
234+
* Default template priority for top queries indices
235+
*/
236+
public static final long DEFAULT_TEMPLATE_PRIORITY = 1847L;
233237
/**
234238
* Default Top N local indices retention period in days
235239
*/
@@ -266,6 +270,22 @@ public class QueryInsightsSettings {
266270
Setting.Property.Dynamic
267271
);
268272

273+
/**
274+
* Index pattern glob for matching top query indices
275+
*/
276+
public static final String TOP_QUERIES_INDEX_PATTERN_GLOB = "top_queries-*";
277+
278+
/**
279+
* Setting for Top N local indices template priority
280+
*/
281+
public static final Setting<Long> TOP_N_EXPORTER_TEMPLATE_PRIORITY = Setting.longSetting(
282+
TOP_N_QUERIES_EXPORTER_PREFIX + ".template_priority",
283+
DEFAULT_TEMPLATE_PRIORITY,
284+
0L, // min value is 0
285+
Setting.Property.Dynamic,
286+
Setting.Property.NodeScope
287+
);
288+
269289
/**
270290
* Get the enabled setting based on type
271291
* @param type MetricType

src/test/java/org/opensearch/plugin/insights/QueryInsightsPluginTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public void testGetSettings() {
8181
QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING,
8282
QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER,
8383
QueryInsightsSettings.TOP_N_EXPORTER_TYPE,
84+
QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY,
8485
QueryCategorizationSettings.SEARCH_QUERY_FIELD_TYPE_CACHE_SIZE_KEY
8586
),
8687
queryInsightsPlugin.getSettings()

src/test/java/org/opensearch/plugin/insights/QueryInsightsTestUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public static void registerAllQueryInsightsSettings(ClusterSettings clusterSetti
354354
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_NAME);
355355
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_QUERIES_GROUPING_FIELD_TYPE);
356356
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_EXPORTER_DELETE_AFTER);
357+
clusterSettings.registerSetting(QueryInsightsSettings.TOP_N_EXPORTER_TEMPLATE_PRIORITY);
357358
clusterSettings.registerSetting(QueryCategorizationSettings.SEARCH_QUERY_METRICS_ENABLED_SETTING);
358359
}
359360
}

0 commit comments

Comments
 (0)