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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 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,8 +25,10 @@

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

import org.apache.hugegraph.auth.HugeAuthenticator;
import jakarta.ws.rs.core.MultivaluedMap;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.config.ServerOptions;
import org.apache.hugegraph.core.GraphManager;
Expand Down Expand Up @@ -54,6 +56,7 @@ public class AccessLogFilter implements ContainerResponseFilter {
private static final String GREMLIN = "gremlin";
private static final String CYPHER = "cypher";


@Context
private jakarta.inject.Provider<HugeConfig> configProvider;

Expand All @@ -78,6 +81,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(key.equals("graph")){
newPath = newPath.replace(key, value);
}
newPath = newPath.replace(value, key);
}

LOG.debug("Original Path: " + requestPath + " New Path: " + newPath);

return newPath;
}

/**
* Use filter to log request info
*
Expand All @@ -89,15 +113,25 @@ 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);
int status = responseContext.getStatus();

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

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.debug("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 @@ -106,8 +140,10 @@ 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