Skip to content

Commit

Permalink
fix metrics OOM
Browse files Browse the repository at this point in the history
  • Loading branch information
JackyYangPassion committed Jul 11, 2024
1 parent aa41e44 commit d55382d
Showing 1 changed file with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@

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

import jakarta.ws.rs.core.UriInfo;
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 @@ -82,15 +83,25 @@ private String join(String path1, String path2) {
return String.join(DELIMITER, path1, path2);
}

private String normalizePath(String path, String method) {
private String normalizePath(ContainerRequestContext requestContext) {
// Replace variable parts of the path with placeholders
//TODO: Jersey Filter Determine if the method parameter is on the path.
if (method.equals("PUT") || method.equals("GET") || method.equals("DELETE")) {
path = ID_PATTERN.matcher(path).replaceAll(method);
path = QUOTED_STRING_PATTERN.matcher(path).replaceAll(method);
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);
}

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

return newPath;
}

/**
Expand All @@ -105,16 +116,24 @@ public void filter(ContainerRequestContext requestContext,
// Grab corresponding request / response info from context;
URI uri = requestContext.getUriInfo().getRequestUri();
String method = requestContext.getMethod();
UriInfo uriInfo = requestContext.getUriInfo();

String path = normalizePath(uriInfo.getPath(),method);
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 @@ -123,8 +142,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

0 comments on commit d55382d

Please sign in to comment.