From 64e4f37858e026b2fc61ab1cecb8a3fdb1e40c9e Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Wed, 21 Feb 2024 16:33:30 -0500 Subject: [PATCH 1/5] Adding user and session ID. --- .../java/org/opensearch/ubl/HeaderConstants.java | 2 ++ .../opensearch/ubl/UserBehaviorLoggingPlugin.java | 12 ++++++++++-- .../action/UserBehaviorLoggingActionFilter.java | 6 +++++- .../opensearch/ubl/backends/OpenSearchBackend.java | 8 +++++--- .../org/opensearch/ubl/model/QueryRequest.java | 14 +++++++++++++- 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/opensearch/ubl/HeaderConstants.java b/src/main/java/org/opensearch/ubl/HeaderConstants.java index b848641..37dbc1a 100644 --- a/src/main/java/org/opensearch/ubl/HeaderConstants.java +++ b/src/main/java/org/opensearch/ubl/HeaderConstants.java @@ -11,5 +11,7 @@ public class HeaderConstants { public static final String EVENT_STORE_HEADER = "X-ubi-store"; + public static final String USER_ID_HEADER = "X-ubi-user-id"; + public static final String SESSION_ID_HEADER = "X-ubi-session-id"; } diff --git a/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java b/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java index a34a1bf..ea6a605 100644 --- a/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java +++ b/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java @@ -53,12 +53,20 @@ public class UserBehaviorLoggingPlugin extends Plugin implements ActionPlugin { @Override public Collection getRestHeaders() { - return List.of(new RestHeaderDefinition(HeaderConstants.EVENT_STORE_HEADER, false)); + return List.of( + new RestHeaderDefinition(HeaderConstants.EVENT_STORE_HEADER, false), + new RestHeaderDefinition(HeaderConstants.SESSION_ID_HEADER, false), + new RestHeaderDefinition(HeaderConstants.USER_ID_HEADER, false) + ); } @Override public Collection getTaskHeaders() { - return List.of(HeaderConstants.EVENT_STORE_HEADER); + return List.of( + HeaderConstants.EVENT_STORE_HEADER, + HeaderConstants.SESSION_ID_HEADER, + HeaderConstants.USER_ID_HEADER + ); } @Override diff --git a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java index b56c65b..a52eeea 100644 --- a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java +++ b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java @@ -91,6 +91,10 @@ public void onResponse(Response response) { // Create a UUID for this search request. final String queryId = UUID.randomUUID().toString(); + // Get info from the headers. + final String userId = task.getHeader(HeaderConstants.USER_ID_HEADER); + final String sessionId = task.getHeader(HeaderConstants.SESSION_ID_HEADER); + // The query will be empty when there is no query, e.g. /_search final String query = searchRequest.source().toString(); @@ -110,7 +114,7 @@ public void onResponse(Response response) { // Persist the query to the backend. backend.persistQuery(eventStore, - new QueryRequest(queryId, query), + new QueryRequest(queryId, query, userId, sessionId), new QueryResponse(queryId, queryResponseId, queryResponseHitIds)); } catch (Exception ex) { diff --git a/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java b/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java index 7ea1e1c..95280e8 100644 --- a/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java +++ b/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java @@ -109,10 +109,12 @@ public void persistQuery(final String storeName, final QueryRequest queryRequest // What will be indexed - adheres to the queries-mapping.json final Map source = new HashMap<>(); source.put("timestamp", queryRequest.getTimestamp()); - source.put("queryId", queryRequest.getQueryId()); + source.put("query_id", queryRequest.getQueryId()); source.put("query", queryRequest.getQuery()); - source.put("queryResponseId", queryResponse.getQueryResponseId()); - source.put("queryResponseHitIds", queryResponse.getQueryResponseHitIds()); + source.put("query_response_id", queryResponse.getQueryResponseId()); + source.put("query_response_hit_ids", queryResponse.getQueryResponseHitIds()); + source.put("user_id", queryRequest.getUserId()); + source.put("session_id", queryRequest.getSessionId()); // Get the name of the queries. final String queriesIndexName = getQueriesIndexName(storeName); diff --git a/src/main/java/org/opensearch/ubl/model/QueryRequest.java b/src/main/java/org/opensearch/ubl/model/QueryRequest.java index a4ef2c5..7cd8f61 100644 --- a/src/main/java/org/opensearch/ubl/model/QueryRequest.java +++ b/src/main/java/org/opensearch/ubl/model/QueryRequest.java @@ -13,11 +13,15 @@ public class QueryRequest { private final long timestamp; private final String queryId; private final String query; + private final String userId; + private final String sessionId; - public QueryRequest(final String queryId, final String query) { + public QueryRequest(final String queryId, final String query, final String userId, final String sessionId) { this.timestamp = System.currentTimeMillis(); this.queryId = queryId; this.query = query; + this.userId = userId; + this.sessionId = sessionId; } public long getTimestamp() { @@ -32,4 +36,12 @@ public String getQuery() { return query; } + public String getUserId() { + return userId; + } + + public String getSessionId() { + return sessionId; + } + } From 5cfe45adbf8519f477a5ccb3666e25d965cb413c Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Wed, 21 Feb 2024 16:40:33 -0500 Subject: [PATCH 2/5] Changing headers to an enum. --- .../org/opensearch/ubl/HeaderConstants.java | 24 +++++++++++--- .../ubl/UserBehaviorLoggingPlugin.java | 12 +++---- .../UserBehaviorLoggingActionFilter.java | 33 ++++++++++--------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/opensearch/ubl/HeaderConstants.java b/src/main/java/org/opensearch/ubl/HeaderConstants.java index 37dbc1a..e247cac 100644 --- a/src/main/java/org/opensearch/ubl/HeaderConstants.java +++ b/src/main/java/org/opensearch/ubl/HeaderConstants.java @@ -8,10 +8,26 @@ package org.opensearch.ubl; -public class HeaderConstants { +public enum HeaderConstants { - public static final String EVENT_STORE_HEADER = "X-ubi-store"; - public static final String USER_ID_HEADER = "X-ubi-user-id"; - public static final String SESSION_ID_HEADER = "X-ubi-session-id"; + QUERY_ID_HEADER("X-ubi-query-id"), + EVENT_STORE_HEADER("X-ubi-store"), + USER_ID_HEADER("X-ubi-user-id"), + SESSION_ID_HEADER("X-ubi-session-id"); + + private final String header; + + private HeaderConstants(String header) { + this.header = header; + } + + public String getHeader() { + return header; + } + + @Override + public String toString() { + return header; + } } diff --git a/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java b/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java index ea6a605..84fd8bd 100644 --- a/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java +++ b/src/main/java/org/opensearch/ubl/UserBehaviorLoggingPlugin.java @@ -54,18 +54,18 @@ public class UserBehaviorLoggingPlugin extends Plugin implements ActionPlugin { @Override public Collection getRestHeaders() { return List.of( - new RestHeaderDefinition(HeaderConstants.EVENT_STORE_HEADER, false), - new RestHeaderDefinition(HeaderConstants.SESSION_ID_HEADER, false), - new RestHeaderDefinition(HeaderConstants.USER_ID_HEADER, false) + new RestHeaderDefinition(HeaderConstants.EVENT_STORE_HEADER.getHeader(), false), + new RestHeaderDefinition(HeaderConstants.SESSION_ID_HEADER.getHeader(), false), + new RestHeaderDefinition(HeaderConstants.USER_ID_HEADER.getHeader(), false) ); } @Override public Collection getTaskHeaders() { return List.of( - HeaderConstants.EVENT_STORE_HEADER, - HeaderConstants.SESSION_ID_HEADER, - HeaderConstants.USER_ID_HEADER + HeaderConstants.EVENT_STORE_HEADER.getHeader(), + HeaderConstants.SESSION_ID_HEADER.getHeader(), + HeaderConstants.USER_ID_HEADER.getHeader() ); } diff --git a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java index a52eeea..d1cf82a 100644 --- a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java +++ b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java @@ -64,19 +64,8 @@ public void app @Override public void onResponse(Response response) { - //LOGGER.info("Query ID header: " + task.getHeader("query-id")); - final long startTime = System.currentTimeMillis(); - String eventStore = task.getHeader(HeaderConstants.EVENT_STORE_HEADER); - - // If there is no event store header we'll use a "default" store. - if(eventStore == null || eventStore.trim().isEmpty()) { - eventStore = "default"; - } - - LOGGER.info("Using UBL event store: {}", eventStore); - // Get the search itself. final SearchRequest searchRequest = (SearchRequest) request; @@ -88,12 +77,11 @@ public void onResponse(Response response) { // Get all search hits from the response. if (response instanceof SearchResponse) { - // Create a UUID for this search request. - final String queryId = UUID.randomUUID().toString(); - // Get info from the headers. - final String userId = task.getHeader(HeaderConstants.USER_ID_HEADER); - final String sessionId = task.getHeader(HeaderConstants.SESSION_ID_HEADER); + final String queryId = getHeaderValue(HeaderConstants.QUERY_ID_HEADER, UUID.randomUUID().toString(), task); + final String eventStore = getHeaderValue(HeaderConstants.EVENT_STORE_HEADER, "default", task); + final String userId = getHeaderValue(HeaderConstants.USER_ID_HEADER, "", task); + final String sessionId = getHeaderValue(HeaderConstants.SESSION_ID_HEADER, "", task); // The query will be empty when there is no query, e.g. /_search final String query = searchRequest.source().toString(); @@ -144,4 +132,17 @@ public void onFailure(Exception ex) { } + private String getHeaderValue(final HeaderConstants header, final String defaultValue, Task task) { + + String value = task.getHeader(header.getHeader()); + + // If there is no event store header we'll use a "default" store. + if(value == null || value.trim().isEmpty()) { + return defaultValue; + } else { + return value; + } + + } + } From 0bb5985feb80e4d2ae47c2dde3c836d59e1a99e2 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Wed, 21 Feb 2024 16:43:10 -0500 Subject: [PATCH 3/5] Cleanup and improvements. --- .../ubl/action/UserBehaviorLoggingActionFilter.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java index d1cf82a..a2abd65 100644 --- a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java +++ b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java @@ -20,7 +20,6 @@ import org.opensearch.core.action.ActionResponse; import org.opensearch.tasks.Task; import org.opensearch.threadpool.ThreadPool; -import org.opensearch.transport.Header; import org.opensearch.ubl.HeaderConstants; import org.opensearch.ubl.backends.Backend; import org.opensearch.ubl.model.QueryRequest; @@ -90,7 +89,6 @@ public void onResponse(Response response) { final String queryResponseId = UUID.randomUUID().toString(); final List queryResponseHitIds = new LinkedList<>(); - final SearchResponse searchResponse = (SearchResponse) response; // Add each hit to the list of query responses. @@ -132,9 +130,9 @@ public void onFailure(Exception ex) { } - private String getHeaderValue(final HeaderConstants header, final String defaultValue, Task task) { + private String getHeaderValue(final HeaderConstants header, final String defaultValue, final Task task) { - String value = task.getHeader(header.getHeader()); + final String value = task.getHeader(header.getHeader()); // If there is no event store header we'll use a "default" store. if(value == null || value.trim().isEmpty()) { From 3a61361965b47ce4572792a8f9abcbc9992a6498 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Wed, 21 Feb 2024 16:46:16 -0500 Subject: [PATCH 4/5] Cleanup and improvements. --- .../ubl/action/UserBehaviorLoggingActionFilter.java | 5 +---- .../java/org/opensearch/ubl/backends/OpenSearchBackend.java | 3 ++- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java index a2abd65..41e30fa 100644 --- a/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java +++ b/src/main/java/org/opensearch/ubl/action/UserBehaviorLoggingActionFilter.java @@ -92,9 +92,7 @@ public void onResponse(Response response) { final SearchResponse searchResponse = (SearchResponse) response; // Add each hit to the list of query responses. - searchResponse.getHits().forEach(hit -> { - queryResponseHitIds.add(String.valueOf(hit.docId())); - }); + searchResponse.getHits().forEach(hit -> queryResponseHitIds.add(String.valueOf(hit.docId()))); try { @@ -134,7 +132,6 @@ private String getHeaderValue(final HeaderConstants header, final String default final String value = task.getHeader(header.getHeader()); - // If there is no event store header we'll use a "default" store. if(value == null || value.trim().isEmpty()) { return defaultValue; } else { diff --git a/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java b/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java index 95280e8..1fba79d 100644 --- a/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java +++ b/src/main/java/org/opensearch/ubl/backends/OpenSearchBackend.java @@ -84,6 +84,7 @@ public void delete(String storeName) { final String eventsIndexName = getEventsIndexName(storeName); final String queriesIndexName = getQueriesIndexName(storeName); final DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(eventsIndexName, queriesIndexName); + client.admin().indices().delete(deleteIndexRequest); } @@ -102,7 +103,7 @@ public void persistEvent(String storeName, String eventJson) { } @Override - public void persistQuery(final String storeName, final QueryRequest queryRequest, QueryResponse queryResponse) throws Exception { + public void persistQuery(final String storeName, final QueryRequest queryRequest, QueryResponse queryResponse) { LOGGER.info("Writing query ID {} with response ID {}", queryRequest.getQueryId(), queryResponse.getQueryResponseId()); From 2f8e3fe14fcd0db01fd1d7712494f5ff237518f3 Mon Sep 17 00:00:00 2001 From: jzonthemtn Date: Wed, 21 Feb 2024 17:11:57 -0500 Subject: [PATCH 5/5] Renaming headers from ubi to ubl. --- src/main/java/org/opensearch/ubl/HeaderConstants.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/opensearch/ubl/HeaderConstants.java b/src/main/java/org/opensearch/ubl/HeaderConstants.java index e247cac..ba41e6f 100644 --- a/src/main/java/org/opensearch/ubl/HeaderConstants.java +++ b/src/main/java/org/opensearch/ubl/HeaderConstants.java @@ -10,10 +10,10 @@ public enum HeaderConstants { - QUERY_ID_HEADER("X-ubi-query-id"), - EVENT_STORE_HEADER("X-ubi-store"), - USER_ID_HEADER("X-ubi-user-id"), - SESSION_ID_HEADER("X-ubi-session-id"); + QUERY_ID_HEADER("X-ubl-query-id"), + EVENT_STORE_HEADER("X-ubl-store"), + USER_ID_HEADER("X-ubl-user-id"), + SESSION_ID_HEADER("X-ubl-session-id"); private final String header;