Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): Filter dynamice path(PUT/GET/DELETE) with params cause OOM #2569

Merged
merged 8 commits into from
Nov 6, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import java.io.IOException;
import java.net.URI;
import java.util.Map;

import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
Expand All @@ -39,6 +40,7 @@
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.Provider;

// TODO: should add test for this class
Expand Down Expand Up @@ -77,6 +79,27 @@ private String join(String path1, String path2) {
return String.join(DELIMITER, path1, path2);
}

private String normalizePath(ContainerRequestContext requestContext) {
// Replace variable parts of the path with placeholders
String requestPath = requestContext.getUriInfo().getPath();
// get uri params
MultivaluedMap<String, String> pathParameters = requestContext.getUriInfo()
.getPathParameters();

String newPath = requestPath;
for (Map.Entry<String, java.util.List<String>> entry : pathParameters.entrySet()) {
String key = entry.getKey();
String value = entry.getValue().get(0);
if ("graph".equals(key)) {
newPath = newPath.replace(key, value);
}
newPath = newPath.replace(value, key);
}

LOG.trace("Original Path: {} New Path: {}", requestPath, newPath);
return newPath;
}

/**
* Use filter to log request info
*
Expand All @@ -88,15 +111,23 @@ public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
// Grab corresponding request / response info from context;
URI uri = requestContext.getUriInfo().getRequestUri();
String path = uri.getRawPath();
String method = requestContext.getMethod();
String path = normalizePath(requestContext);
String metricsName = join(path, method);

MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_TOTAL_COUNTER)).inc();
int status = responseContext.getStatus();
if (status != 500 && status != 415) {
MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_TOTAL_COUNTER)).inc();
}

if (statusOk(responseContext.getStatus())) {
MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_SUCCESS_COUNTER)).inc();
} else {
MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_FAILED_COUNTER)).inc();
//TODO: The return codes for compatibility need to be further detailed.
LOG.trace("Failed Status: {}", status);
if (status != 500 && status != 415) {
MetricsUtil.registerCounter(join(metricsName, METRICS_PATH_FAILED_COUNTER)).inc();
}
}

Object requestTime = requestContext.getProperty(REQUEST_TIME);
Expand All @@ -105,8 +136,11 @@ public void filter(ContainerRequestContext requestContext,
long start = (Long) requestTime;
long executeTime = now - start;

MetricsUtil.registerHistogram(join(metricsName, METRICS_PATH_RESPONSE_TIME_HISTOGRAM))
.update(executeTime);
if (status != 500 && status != 415) {
MetricsUtil.registerHistogram(join(metricsName,
METRICS_PATH_RESPONSE_TIME_HISTOGRAM))
.update(executeTime);
}

HugeConfig config = configProvider.get();
long timeThreshold = config.get(ServerOptions.SLOW_QUERY_LOG_TIME_THRESHOLD);
Expand Down
Loading