Skip to content

Commit

Permalink
remove common-utils as a dependency
Browse files Browse the repository at this point in the history
Signed-off-by: Chenyang Ji <cyji@amazon.com>
  • Loading branch information
ansjcy committed Mar 6, 2024
1 parent 27ee977 commit 2688389
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 40 deletions.
1 change: 0 additions & 1 deletion gradle/code-coverage.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ repositories {
maven {
url = "https://ci.opensearch.org/ci/dbc/snapshots/lucene/"
}
maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" }
}

allprojects {
Expand Down
19 changes: 0 additions & 19 deletions plugins/query-insights/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,5 @@ opensearchplugin {
classname 'org.opensearch.plugin.insights.QueryInsightsPlugin'
}

dependencyLicenses.enabled = false

ext {
opensearch_version = versions.opensearch
// 3.0.0-SNAPSHOT -> 3.0.0.0
version_tokens = opensearch_version.tokenize('-')
common_utils_version = version_tokens[0] + '.0'
if (version_tokens.size() > 1) {
common_utils_version = common_utils_version + "-" + version_tokens[1]
}
}

repositories {
mavenLocal()
mavenCentral()
maven { url "https://aws.oss.sonatype.org/content/repositories/snapshots" }
}

dependencies {
implementation "org.opensearch:common-utils:${common_utils_version}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
import org.opensearch.action.search.SearchRequestOperationsListener;
import org.opensearch.cluster.service.ClusterService;
import org.opensearch.common.inject.Inject;
import org.opensearch.commons.authuser.User;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.rules.model.Attribute;
import org.opensearch.plugin.insights.rules.model.MetricType;
import org.opensearch.plugin.insights.rules.model.SearchQueryRecord;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;
import org.opensearch.plugin.insights.utils.ThreadContextParser;
import org.opensearch.threadpool.ThreadPool;

import java.util.Collections;
Expand Down Expand Up @@ -149,15 +148,7 @@ public void onRequestEnd(final SearchPhaseContext context, final SearchRequestCo
attributes.put(Attribute.INDICES, request.indices());
attributes.put(Attribute.PHASE_LATENCY_MAP, searchRequestContext.phaseTookMap());
// add user related information
Object userInfo = threadPool.getThreadContext().getTransient(QueryInsightsSettings.REQUEST_HEADER_USER_INFO);
if (userInfo != null) {
attributes.put(Attribute.USER, User.parse(userInfo.toString()));
}
// add remote ip address
Object remoteAddress = threadPool.getThreadContext().getTransient(QueryInsightsSettings.REQUEST_HEADER_REMOTE_ADDRESS);
if (remoteAddress != null) {
attributes.put(Attribute.REMOTE_ADDRESS, remoteAddress.toString());
}
attributes.putAll(ThreadContextParser.getUserInfoFromThreadContext(threadPool.getThreadContext()));
SearchQueryRecord record = new SearchQueryRecord(request.getOrCreateAbsoluteStartMillis(), measurements, attributes);
queryInsightsService.addRecord(record);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,21 @@ public enum Attribute {
*/
REMOTE_ADDRESS,
/**
* Information related to the user who sent this request
* Username of the user who sent this request
*/
USER;
USER_NAME,
/**
* Backend roles of the user who sent this request
*/
USER_BACKEND_ROLES,
/**
* Roles of the user who sent this request
*/
USER_ROLES,
/**
* Tenant info of the user who sent this request
*/
USER_TENANT;

/**
* Read an Attribute from a StreamInput
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.plugin.insights.utils;

import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.common.Strings;
import org.opensearch.plugin.insights.rules.model.Attribute;
import org.opensearch.plugin.insights.settings.QueryInsightsSettings;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* Helper class to parse information from the thread context
*/
public final class ThreadContextParser {

private ThreadContextParser() {}

/**
* Get User info from the thread context
*
* @param threadContext context of the thread
* @return Map of {@link Attribute} and the corresponding values
*/
public static Map<Attribute, Object> getUserInfoFromThreadContext(ThreadContext threadContext) {
Map<Attribute, Object> userInfoMap = new HashMap<>();
if (threadContext == null) {
return userInfoMap;
}
Object userInfoObj = threadContext.getTransient(QueryInsightsSettings.REQUEST_HEADER_USER_INFO);
if (userInfoObj == null) {
return userInfoMap;
}
String userInfoStr = userInfoObj.toString();
Object remoteAddressObj = threadContext.getTransient(QueryInsightsSettings.REQUEST_HEADER_REMOTE_ADDRESS);
if (remoteAddressObj != null) {
userInfoMap.put(Attribute.REMOTE_ADDRESS, remoteAddressObj.toString());
}

String[] userInfo = userInfoStr.split("\\|");
if ((userInfo.length == 0) || (Strings.isNullOrEmpty(userInfo[0]))) {
return userInfoMap;
}
userInfoMap.put(Attribute.USER_NAME, userInfo[0].trim());
if ((userInfo.length > 1) && !Strings.isNullOrEmpty(userInfo[1])) {
userInfoMap.put(Attribute.USER_BACKEND_ROLES, Arrays.asList(userInfo[1].split(",")));
}
if ((userInfo.length > 2) && !Strings.isNullOrEmpty(userInfo[2])) {
userInfoMap.put(Attribute.USER_ROLES, Arrays.asList(userInfo[2].split(",")));
}
if ((userInfo.length > 3) && !Strings.isNullOrEmpty(userInfo[3])) {
userInfoMap.put(Attribute.USER_TENANT, userInfo[3].trim());
}
return userInfoMap;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

/**
* Utils for Query Insights Plugin
*/
package org.opensearch.plugin.insights.utils;
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import org.opensearch.common.settings.ClusterSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.commons.InjectSecurity;
import org.opensearch.commons.authuser.User;
import org.opensearch.plugin.insights.core.service.QueryInsightsService;
import org.opensearch.plugin.insights.core.service.TopQueriesService;
import org.opensearch.plugin.insights.rules.model.Attribute;
Expand Down Expand Up @@ -60,7 +58,10 @@ public class QueryInsightsListenerTests extends OpenSearchTestCase {
private final Settings.Builder settingsBuilder = Settings.builder();
private final Settings settings = settingsBuilder.build();
private final String remoteAddress = "1.2.3.4";
private User user;
private final String userName = "user1";
private final List<String> userBackendRoles = List.of("bk-role1", "bk-role2");
private final List<String> userRoles = List.of("role1", "role2");
private final String userTenant = "tenant1";
private ClusterService clusterService;

@Before
Expand All @@ -75,9 +76,10 @@ public void setup() {

// inject user info
ThreadContext threadContext = new ThreadContext(settings);
user = new User("user-1", List.of("role1", "role2"), List.of("role3", "role4"), List.of());
InjectSecurity injector = new InjectSecurity("id", settings, threadContext);
injector.injectUserInfo(user);
threadContext.putTransient(
QueryInsightsSettings.REQUEST_HEADER_USER_INFO,
userName + '|' + String.join(",", userBackendRoles) + "|" + String.join(",", userRoles) + "|" + userTenant
);
threadContext.putTransient(QueryInsightsSettings.REQUEST_HEADER_REMOTE_ADDRESS, remoteAddress);
when(threadPool.getThreadContext()).thenReturn(threadContext);
}
Expand Down Expand Up @@ -120,7 +122,10 @@ public void testOnRequestEnd() {
assertEquals(numberOfShards, attrs.get(Attribute.TOTAL_SHARDS));
assertEquals(indices, attrs.get(Attribute.INDICES));
assertEquals(phaseLatencyMap, attrs.get(Attribute.PHASE_LATENCY_MAP));
assertEquals(user, attrs.get(Attribute.USER));
assertEquals(userName, attrs.get(Attribute.USER_NAME));
assertEquals(userBackendRoles, attrs.get(Attribute.USER_BACKEND_ROLES));
assertEquals(userRoles, attrs.get(Attribute.USER_ROLES));
assertEquals(userTenant, attrs.get(Attribute.USER_TENANT));
assertEquals(remoteAddress, attrs.get(Attribute.REMOTE_ADDRESS));
}

Expand Down

0 comments on commit 2688389

Please sign in to comment.