Skip to content

Commit

Permalink
use injected dependency instead of NamedXContentRegistry.EMPTY
Browse files Browse the repository at this point in the history
  • Loading branch information
ansjcy committed Aug 16, 2024
1 parent 1851668 commit aca5a6f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public Collection<Object> createComponents(
clusterService.getClusterSettings(),
threadPool,
client,
metricsRegistry
metricsRegistry,
xContentRegistry
);
return List.of(queryInsightsService, new QueryInsightsListener(clusterService, queryInsightsService));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.query.QueryBuilders;
Expand All @@ -45,16 +46,18 @@ public final class LocalIndexReader implements QueryInsightsReader {
private final Logger logger = LogManager.getLogger();
private final Client client;
private DateTimeFormatter indexPattern;
private NamedXContentRegistry namedXContentRegistry;

/**
* Constructor of LocalIndexReader
*
* @param client OS client
* @param indexPattern the pattern of index to read from
*/
public LocalIndexReader(final Client client, final DateTimeFormatter indexPattern) {
public LocalIndexReader(final Client client, final DateTimeFormatter indexPattern, NamedXContentRegistry namedXContentRegistry) {
this.indexPattern = indexPattern;
this.client = client;
this.namedXContentRegistry = namedXContentRegistry;
}

/**
Expand Down Expand Up @@ -97,7 +100,7 @@ public List<SearchQueryRecord> read(final String from, final String to) {
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest).actionGet();
for (SearchHit hit : searchResponse.getHits()) {
SearchQueryRecord record = SearchQueryRecord.getRecord(hit);
SearchQueryRecord record = SearchQueryRecord.getRecord(hit, namedXContentRegistry);
records.add(record);
}
curr = curr.plusDays(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.joda.time.format.DateTimeFormat;
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.NamedXContentRegistry;

import static org.opensearch.plugin.insights.settings.QueryInsightsSettings.*;

Expand Down Expand Up @@ -67,8 +68,8 @@ public void validateReaderConfig(final Settings settings) throws IllegalArgument
* @param indexPattern the index pattern if creating an index Reader
* @return QueryInsightsReader the created Reader
*/
public QueryInsightsReader createReader(String indexPattern) {
QueryInsightsReader Reader = new LocalIndexReader(client, DateTimeFormat.forPattern(indexPattern));
public QueryInsightsReader createReader(String indexPattern, NamedXContentRegistry namedXContentRegistry) {
QueryInsightsReader Reader = new LocalIndexReader(client, DateTimeFormat.forPattern(indexPattern), namedXContentRegistry);
this.Readers.add(Reader);
return Reader;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.plugin.insights.core.exporter.QueryInsightsExporterFactory;
import org.opensearch.plugin.insights.core.reader.QueryInsightsReaderFactory;
import org.opensearch.plugin.insights.core.service.categorizer.SearchQueryCategorizer;
Expand Down Expand Up @@ -83,6 +84,7 @@ public class QueryInsightsService extends AbstractLifecycleComponent {
private volatile boolean searchQueryMetricsEnabled;

private SearchQueryCategorizer searchQueryCategorizer;
private NamedXContentRegistry namedXContentRegistry;

/**
* Constructor of the QueryInsightsService
Expand All @@ -97,13 +99,15 @@ public QueryInsightsService(
final ClusterSettings clusterSettings,
final ThreadPool threadPool,
final Client client,
final MetricsRegistry metricsRegistry
) {
final MetricsRegistry metricsRegistry,
final NamedXContentRegistry namedXContentRegistry
) {
enableCollect = new HashMap<>();
queryRecordsQueue = new LinkedBlockingQueue<>(QueryInsightsSettings.QUERY_RECORD_QUEUE_CAPACITY);
this.threadPool = threadPool;
this.queryInsightsExporterFactory = new QueryInsightsExporterFactory(client);
this.queryInsightsReaderFactory = new QueryInsightsReaderFactory(client);
this.namedXContentRegistry = namedXContentRegistry;
// initialize top n queries services and configurations consumers
topQueriesServices = new HashMap<>();
for (MetricType metricType : MetricType.allMetricTypes()) {
Expand Down Expand Up @@ -311,7 +315,7 @@ public void setExporterReader(final MetricType type, final Settings settings) {
if (topQueriesServices.containsKey(type)) {
TopQueriesService tqs = topQueriesServices.get(type);
tqs.setExporter(settings);
tqs.setReader(settings);
tqs.setReader(settings, namedXContentRegistry);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.logging.log4j.Logger;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.plugin.insights.core.exporter.QueryInsightsExporter;
import org.opensearch.plugin.insights.core.exporter.QueryInsightsExporterFactory;
import org.opensearch.plugin.insights.core.exporter.SinkType;
Expand Down Expand Up @@ -248,8 +249,8 @@ public void setExporter(final Settings settings) {
*
* @param settings reader config {@link Settings}
*/
public void setReader(final Settings settings) {
this.reader = queryInsightsReaderFactory.createReader(settings.get(EXPORT_INDEX, DEFAULT_TOP_N_QUERIES_INDEX_PATTERN));
public void setReader(final Settings settings, NamedXContentRegistry namedXContentRegistry) {
this.reader = queryInsightsReaderFactory.createReader(settings.get(EXPORT_INDEX, DEFAULT_TOP_N_QUERIES_INDEX_PATTERN), namedXContentRegistry);
queryInsightsReaderFactory.updateReader(reader, settings.get(EXPORT_INDEX, DEFAULT_TOP_N_QUERIES_INDEX_PATTERN));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ public SearchQueryRecord(final long timestamp, Map<MetricType, Number> measureme
this.timestamp = timestamp;
}

public static SearchQueryRecord getRecord(SearchHit hit) throws IOException {
public static SearchQueryRecord getRecord(SearchHit hit, NamedXContentRegistry namedXContentRegistry) throws IOException {
long timestamp = 0L;
Map<MetricType, Number> measurements = new HashMap<>();
Map<Attribute, Object> attributes = new HashMap<>();
XContentParser parser = XContentType.JSON.xContent().createParser(
NamedXContentRegistry.EMPTY,
// NamedXContentRegistry.EMPTY,
namedXContentRegistry,
LoggingDeprecationHandler.INSTANCE,
hit.getSourceAsString()
);
Expand Down

0 comments on commit aca5a6f

Please sign in to comment.