Skip to content

Commit

Permalink
use regex pattern to determine messages requests;
Browse files Browse the repository at this point in the history
moved normalizePath method to TraceUtils;

Signed-off-by: Stefan Maute <stefan.maute@bosch.io>
  • Loading branch information
Stefan Maute committed Oct 19, 2021
1 parent d4a8b85 commit 6d18d98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public final class TraceUriGenerator implements Function<String, TraceInformatio
static final String MESSAGES_PATH_SUFFIX = "/messages";
static final String FALLBACK_PATH = "/other";

private static final String SLASH = "/";

private static final int FIRST_API_VERSION = JsonSchemaVersion.V_2.toInt();
private static final int LATEST_API_VERSION = JsonSchemaVersion.LATEST.toInt();
Expand All @@ -66,7 +65,6 @@ public final class TraceUriGenerator implements Function<String, TraceInformatio
private static final List<String> PATHS_EXACT = Arrays.asList("ws/2", "health", "status", "status/health",
"overall/status/health", "devops/logging", "devops/config");
private static final String PATHS_EXACT_REGEX_TEMPLATE = "(?<" + PATHS_EXACT_LENGTH_GROUP + ">^/({0}))/?$";
private static final Pattern DUPLICATE_SLASH_PATTERN = Pattern.compile("\\/+");
private static final Pattern messagePattern = Pattern.compile("(.*/messages/.*)|(.*/claim)");

@Nullable private static TraceUriGenerator instance = null;
Expand Down Expand Up @@ -130,7 +128,7 @@ public TraceInformation apply(final String path) {
}

private TraceInformation extractTraceInformation(final String path) {
final String normalizedPath = normalizePath(path);
final String normalizedPath = TraceUtils.normalizePath(path);
final Matcher messageMatcher = messagePattern.matcher(normalizedPath);
final Matcher matcher = pathPattern.matcher(normalizedPath);

Expand Down Expand Up @@ -192,27 +190,6 @@ private Optional<String> tryGetCapturingGroup(final Matcher matcher, final Strin
return Optional.empty();
}

private static String normalizePath(final String path) {
if (path.isEmpty()) {
return SLASH;
}

// remove duplicate slashes
String normalized = DUPLICATE_SLASH_PATTERN.matcher(path).replaceAll(SLASH);

// strip trailing slash if necessary
if (normalized.length() > 1 && normalized.endsWith(SLASH)) {
normalized = normalized.substring(0, normalized.length() - 1);
}

// add leading slash if necessary
if (!normalized.startsWith(SLASH)) {
normalized = SLASH + normalized;
}

return normalized;
}

@Override
public boolean equals(final Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.concurrent.Immutable;

Expand All @@ -30,11 +31,14 @@
public final class TraceUtils {

private static final String TRACING_FILTER_DELIMITER = "_";
private static final String SLASH = "/";
private static final Pattern DUPLICATE_SLASH_PATTERN = Pattern.compile("\\/+");

private static final String HTTP_ROUNDTRIP_METRIC_NAME = "roundtrip_http";
private static final String FILTER_AUTH_METRIC_NAME = "filter_auth";
private static final String LIVE_CHANNEL_NAME = "live";
private static final String TWIN_CHANNEL_NAME = "twin";
private static final Pattern messagePattern = Pattern.compile("(.*/messages/.*)|(.*/claim)");

private TraceUtils() {
throw new AssertionError();
Expand Down Expand Up @@ -120,7 +124,8 @@ private static String determineChannel(final HttpRequest request) {
.filter(LIVE_CHANNEL_NAME::equals)
.isPresent();
// messages are always live commands
final boolean messageRequest = request.getUri().path().contains("messages");
final String normalizePath = normalizePath(request.getUri().path());
final boolean messageRequest = messagePattern.matcher(normalizePath).matches();

return (liveHeaderPresent || liveQueryPresent || messageRequest) ? LIVE_CHANNEL_NAME : TWIN_CHANNEL_NAME;
}
Expand All @@ -132,4 +137,28 @@ public static String metricizeTraceUri(final String traceUri) {
return traceUri.replaceAll("[./:-]", TRACING_FILTER_DELIMITER);
}

/**
* Normalizes the path and removes duplicate slashes.
*/
public static String normalizePath(final String path) {
if (path.isEmpty()) {
return SLASH;
}

// remove duplicate slashes
String normalized = DUPLICATE_SLASH_PATTERN.matcher(path).replaceAll(SLASH);

// strip trailing slash if necessary
if (normalized.length() > 1 && normalized.endsWith(SLASH)) {
normalized = normalized.substring(0, normalized.length() - 1);
}

// add leading slash if necessary
if (!normalized.startsWith(SLASH)) {
normalized = SLASH + normalized;
}

return normalized;
}

}

0 comments on commit 6d18d98

Please sign in to comment.