Skip to content

Commit

Permalink
Log clientId tag if it is in query params (#18)
Browse files Browse the repository at this point in the history
* Pick up latest version of io.mantisrx, use shaded ObjectMapper to serialize/deserialize objects

* Remove jackson dependency

* Handle NPE in websocket upgrade check

* Fix breaking changes from zuul-core 2.1.8

* Make ./gradlew run work again after zuul version upgrade

* Log clientId if it is in query params

* revert build.gradle

Co-authored-by: Calvin Cheung <[email protected]>
  • Loading branch information
calvin681 and calvin681 authored Nov 10, 2020
1 parent bf3f4fe commit c91a05c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ dependencies {
api "org.projectlombok:lombok:$lombokVersion"
annotationProcessor "org.projectlombok:lombok:$lombokVersion"

testImplementation "junit:junit:$junitVersion"
testImplementation"org.projectlombok:lombok:$lombokVersion"
testAnnotationProcessor "org.projectlombok:lombok:$lombokVersion"
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/mantisrx/api/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Constants {
public static final String TunnelPingParamName = "MantisApiTunnelPingEnabled";

public static final String OriginRegionTagName = "originRegion";
public static final String ClientIdTagName = "clientId";
public static final String TagsParamName = "MantisApiTag";
public static final String TagNameValDelimiter = ":";

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/io/mantisrx/api/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@
import java.util.*;
import java.util.concurrent.TimeUnit;

import static io.mantisrx.api.Constants.TagNameValDelimiter;
import static io.mantisrx.api.Constants.TagsParamName;
import static io.mantisrx.api.Constants.*;

@UtilityClass
@Slf4j
Expand Down Expand Up @@ -63,10 +62,11 @@ public static String[] getTaglist(String uri, String id) {
public static String[] getTaglist(String uri, String id, String region) {
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri);
Map<String, List<String>> queryParameters = queryStringDecoder.parameters();
boolean isClientIdSet = false;

final List<String> tags = new LinkedList<>();
if (queryParameters != null) {
final List<String> tagVals = queryParameters.get(TagsParamName);
List<String> tagVals = queryParameters.get(TagsParamName);
if (tagVals != null) {
for (String s : tagVals) {
StringTokenizer tokenizer = new StringTokenizer(s, TagNameValDelimiter);
Expand All @@ -76,10 +76,18 @@ public static String[] getTaglist(String uri, String id, String region) {
if (s1 != null && !s1.isEmpty() && s2 != null && !s2.isEmpty()) {
tags.add(s1);
tags.add(s2);
if (ClientIdTagName.equals(s1)) {
isClientIdSet = true;
}
}
}
}
}
tagVals = queryParameters.get(ClientIdTagName);
if (!isClientIdSet && tagVals != null && !tagVals.isEmpty()) {
tags.add(ClientIdTagName);
tags.add(tagVals.get(0));
}
}

tags.add("SessionId");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ private void handleRemoteSse(ChannelHandlerContext ctx, FullHttpRequest request)
log.info("Initiating remote SSE connection to {} in {}.", uri, regions);
PushConnectionDetails pcd = PushConnectionDetails.from(uri, regions);

String[] tags = Util.getTaglist(request.uri(), getRegion(request.uri()));
String[] tags = Util.getTaglist(request.uri(), pcd.target, getRegion(request.uri()));

Counter numDroppedBytesCounter = SpectatorUtils.newCounter(Constants.numDroppedBytesCounterName, pcd.target, tags);
Counter numDroppedMessagesCounter = SpectatorUtils.newCounter(Constants.numDroppedMessagesCounterName, pcd.target, tags);
Expand Down
34 changes: 34 additions & 0 deletions src/test/java/io/mantisrx/api/UtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.mantisrx.api;

import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;

public class UtilTest {

@Test
public void testGetTagList() {
String[] tags = Util.getTaglist("/jobconnectbyname/rx-sps-tracker?clientId=testClientId", "testTargetId", "us-east-1");
assertArrayEquals(new String[]{
"clientId", "testClientId",
"SessionId", "testTargetId",
"urlPath", "/jobconnectbyname/rx-sps-tracker",
"region", "us-east-1"}, tags);

tags = Util.getTaglist("/jobconnectbyname/rx-sps-tracker?clientId=testClientId&MantisApiTag=tag1:value1", "testTargetId", "us-east-1");
assertArrayEquals(new String[]{
"tag1", "value1",
"clientId", "testClientId",
"SessionId", "testTargetId",
"urlPath", "/jobconnectbyname/rx-sps-tracker",
"region", "us-east-1"}, tags);

tags = Util.getTaglist("/jobconnectbyname/rx-sps-tracker?clientId=testClientId&MantisApiTag=tag1:value1&MantisApiTag=clientId:testClientId2", "testTargetId", "us-east-1");
assertArrayEquals(new String[]{
"tag1", "value1",
"clientId", "testClientId2",
"SessionId", "testTargetId",
"urlPath", "/jobconnectbyname/rx-sps-tracker",
"region", "us-east-1"}, tags);
}
}

0 comments on commit c91a05c

Please sign in to comment.