Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #43 from o19s/query_id
Browse files Browse the repository at this point in the history
Adding query_id to the response headers
  • Loading branch information
jzonthemtn authored Feb 19, 2024
2 parents 3352880 + 120170e commit f26f334
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.rest.RestHeaderDefinition;
import org.opensearch.ubl.action.UserBehaviorLoggingRestHandler;
import org.opensearch.ubl.action.UserBehaviorLoggingSearchFilter;
import org.opensearch.action.support.ActionFilter;
import org.opensearch.ubl.backends.Backend;
import org.opensearch.ubl.backends.OpenSearchBackend;
import org.opensearch.client.Client;
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
import org.opensearch.cluster.node.DiscoveryNodes;
Expand All @@ -25,14 +20,19 @@
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.ubl.events.EventManager;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.repositories.RepositoriesService;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestHeaderDefinition;
import org.opensearch.script.ScriptService;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.ubl.action.UserBehaviorLoggingActionFilter;
import org.opensearch.ubl.action.UserBehaviorLoggingRestHandler;
import org.opensearch.ubl.backends.Backend;
import org.opensearch.ubl.backends.OpenSearchBackend;
import org.opensearch.ubl.events.EventManager;
import org.opensearch.watcher.ResourceWatcherService;

import java.util.ArrayList;
Expand Down Expand Up @@ -109,7 +109,7 @@ public Collection<Object> createComponents(
) {

this.backend = new OpenSearchBackend(client);
this.userBehaviorLoggingFilter = new UserBehaviorLoggingSearchFilter(backend, environment.settings());
this.userBehaviorLoggingFilter = new UserBehaviorLoggingActionFilter(backend, environment.settings(), threadPool);

LOGGER.info("Creating scheduled task");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.lucene.queryparser.flexible.core.util.StringUtils;
import org.opensearch.action.ActionRequest;
import org.opensearch.action.search.SearchRequest;
import org.opensearch.action.search.SearchResponse;
Expand All @@ -19,24 +18,29 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.action.ActionResponse;
import org.opensearch.tasks.Task;
import org.opensearch.threadpool.ThreadPool;
import org.opensearch.ubl.HeaderConstants;
import org.opensearch.ubl.model.QueryResponse;
import org.opensearch.ubl.backends.Backend;
import org.opensearch.ubl.model.QueryRequest;
import org.opensearch.tasks.Task;
import org.opensearch.ubl.model.QueryResponse;

import java.util.*;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

public class UserBehaviorLoggingSearchFilter implements ActionFilter {
public class UserBehaviorLoggingActionFilter implements ActionFilter {

private static final Logger LOGGER = LogManager.getLogger(UserBehaviorLoggingSearchFilter.class);
private static final Logger LOGGER = LogManager.getLogger(UserBehaviorLoggingActionFilter.class);

private final Backend backend;
private final Settings settings;
private final ThreadPool threadPool;

public UserBehaviorLoggingSearchFilter(final Backend backend, final Settings settings) {
public UserBehaviorLoggingActionFilter(final Backend backend, final Settings settings, ThreadPool threadPool) {
this.backend = backend;
this.settings = settings;
this.threadPool = threadPool;
}

@Override
Expand All @@ -59,6 +63,8 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
@Override
public void onResponse(Response response) {

LOGGER.info("Query ID header: " + task.getHeader("query-id"));

final long startTime = System.currentTimeMillis();

final String eventStore = task.getHeader(HeaderConstants.EVENT_STORE_HEADER);
Expand All @@ -74,19 +80,19 @@ public void onResponse(Response response) {
//final Set<String> indicesToLog = new HashSet<>(Arrays.asList(settings.get(SettingsConstants.INDEX_NAMES).split(",")));
//if(indicesToLog.containsAll(indices)) {

// Create a UUID for this search request.
final String queryId = UUID.randomUUID().toString();
// Get all search hits from the response.
if (response instanceof SearchResponse) {

// The query will be empty when there is no query, e.g. /_search
final String query = searchRequest.source().toString();
// Create a UUID for this search request.
final String queryId = UUID.randomUUID().toString();

// Create a UUID for this search response.
final String queryResponseId = UUID.randomUUID().toString();
// The query will be empty when there is no query, e.g. /_search
final String query = searchRequest.source().toString();

final List<String> queryResponseHitIds = new LinkedList<>();
// Create a UUID for this search response.
final String queryResponseId = UUID.randomUUID().toString();

// Get all search hits from the response.
if (response instanceof SearchResponse) {
final List<String> queryResponseHitIds = new LinkedList<>();

final SearchResponse searchResponse = (SearchResponse) response;

Expand All @@ -107,6 +113,8 @@ public void onResponse(Response response) {
LOGGER.error("Unable to persist query.", ex);
}

threadPool.getThreadContext().addResponseHeader("query_id", queryId);

}

//}
Expand Down

0 comments on commit f26f334

Please sign in to comment.