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

feat(api): support metric API Prometheus format & add statistic metric api #2286

Merged
merged 16 commits into from
Oct 8, 2023
Merged
48 changes: 23 additions & 25 deletions hugegraph-api/src/main/java/org/apache/hugegraph/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,53 @@
import java.util.concurrent.Callable;
import java.util.function.Consumer;

import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import jakarta.ws.rs.core.MediaType;

import org.apache.hugegraph.HugeException;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.core.GraphManager;
import org.apache.hugegraph.define.Checkable;
import org.apache.hugegraph.metrics.MetricsUtil;
import org.slf4j.Logger;

import org.apache.hugegraph.HugeException;
import org.apache.hugegraph.HugeGraph;
import org.apache.hugegraph.util.E;
import org.apache.hugegraph.util.JsonUtil;
import org.apache.hugegraph.util.Log;
import org.slf4j.Logger;

import com.codahale.metrics.Meter;
import com.google.common.collect.ImmutableMap;

public class API {
import jakarta.ws.rs.ForbiddenException;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.NotSupportedException;
import jakarta.ws.rs.core.MediaType;

protected static final Logger LOG = Log.logger(API.class);
public class API {

public static final String CHARSET = "UTF-8";

public static final String TEXT_PLAIN = MediaType.TEXT_PLAIN;
public static final String APPLICATION_JSON = MediaType.APPLICATION_JSON;
public static final String APPLICATION_JSON_WITH_CHARSET =
APPLICATION_JSON + ";charset=" + CHARSET;
APPLICATION_JSON + ";charset=" + CHARSET;
public static final String APPLICATION_TEXT_WITH_CHARSET =
MediaType.TEXT_PLAIN + ";charset=" + CHARSET;
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
public static final String JSON = MediaType.APPLICATION_JSON_TYPE
.getSubtype();

.getSubtype();
public static final String ACTION_APPEND = "append";
public static final String ACTION_ELIMINATE = "eliminate";
public static final String ACTION_CLEAR = "clear";

protected static final Logger LOG = Log.logger(API.class);
private static final Meter SUCCEED_METER =
MetricsUtil.registerMeter(API.class, "commit-succeed");
MetricsUtil.registerMeter(API.class, "commit-succeed");
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
private static final Meter ILLEGAL_ARG_ERROR_METER =
MetricsUtil.registerMeter(API.class, "illegal-arg");
MetricsUtil.registerMeter(API.class, "illegal-arg");
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
private static final Meter EXPECTED_ERROR_METER =
MetricsUtil.registerMeter(API.class, "expected-error");
MetricsUtil.registerMeter(API.class, "expected-error");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

private static final Meter UNKNOWN_ERROR_METER =
MetricsUtil.registerMeter(API.class, "unknown-error");
MetricsUtil.registerMeter(API.class, "unknown-error");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


public static HugeGraph graph(GraphManager manager, String graph) {
HugeGraph g = manager.graph(graph);
if (g == null) {
throw new NotFoundException(String.format(
"Graph '%s' does not exist", graph));
"Graph '%s' does not exist", graph));
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
}
return g;
}
Expand All @@ -97,7 +95,7 @@ public static <R> R commit(HugeGraph g, Callable<R> callable) {
SUCCEED_METER.mark();
return result;
} catch (IllegalArgumentException | NotFoundException |
ForbiddenException e) {
ForbiddenException e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not addressed comment

ILLEGAL_ARG_ERROR_METER.mark();
rollback.accept(null);
throw e;
Expand Down Expand Up @@ -141,7 +139,7 @@ protected static void checkUpdatingBody(Checkable body) {
}

protected static void checkCreatingBody(
Collection<? extends Checkable> bodies) {
Collection<? extends Checkable> bodies) {
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
E.checkArgumentNotNull(bodies, "The request body can't be empty");
for (Checkable body : bodies) {
E.checkArgument(body != null,
Expand All @@ -151,7 +149,7 @@ protected static void checkCreatingBody(
}

protected static void checkUpdatingBody(
Collection<? extends Checkable> bodies) {
Collection<? extends Checkable> bodies) {
E.checkArgumentNotNull(bodies, "The request body can't be empty");
for (Checkable body : bodies) {
E.checkArgumentNotNull(body,
Expand Down Expand Up @@ -187,7 +185,7 @@ public static boolean checkAndParseAction(String action) {
return false;
} else {
throw new NotSupportedException(
String.format("Not support action '%s'", action));
String.format("Not support action '%s'", action));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package org.apache.hugegraph.api.filter;
javeme marked this conversation as resolved.
Show resolved Hide resolved

import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_FAILED_COUNTER;
import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_RESPONSE_TIME_HISTOGRAM;
import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_SUCCESS_COUNTER;
import static org.apache.hugegraph.metrics.MetricsUtil.METRICS_PATH_TOTAL_COUNTER;

import java.io.IOException;

import org.apache.hugegraph.metrics.MetricsUtil;

import jakarta.inject.Singleton;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerResponseContext;
import jakarta.ws.rs.container.ContainerResponseFilter;
import jakarta.ws.rs.ext.Provider;


@Provider
@Singleton
public class AccessLogFilter implements ContainerResponseFilter {


SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
private static final String DELIMETER = "/";

/**
* Use filter to log request info
*
* @param requestContext requestContext
* @param responseContext responseContext
*/
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext)
throws IOException {
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
// Grab corresponding request / response info from context;
String method = requestContext.getRequest().getMethod();
String path = requestContext.getUriInfo().getPath();
String metricsName = join(path, method);

MetricsUtil.registerCounter(
join(metricsName, METRICS_PATH_TOTAL_COUNTER))
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
.inc();
if (responseContext.getStatus() == 200 ||
responseContext.getStatus() == 201 ||
responseContext.getStatus() == 202) {
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
MetricsUtil.registerCounter(
join(metricsName, METRICS_PATH_SUCCESS_COUNTER))
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
.inc();
} else {
MetricsUtil.registerCounter(
join(metricsName, METRICS_PATH_FAILED_COUNTER))
.inc();
}

// 抓取响应的时间 单位/ms
long now = System.currentTimeMillis();
long resposeTime = (now - (long) requestContext.getProperty("RequestTime"));

MetricsUtil.registerHistogram(
join(metricsName, METRICS_PATH_RESPONSE_TIME_HISTOGRAM))
.update(resposeTime);

}

private String join(String path1, String path2) {
return String.join(DELIMETER, path1, path2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.apache.hugegraph.api.filter;

import java.io.IOException;

import jakarta.inject.Singleton;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.container.PreMatching;
import jakarta.ws.rs.ext.Provider;

@Provider
@Singleton
@PreMatching
public class PathFilter implements ContainerRequestFilter {


javeme marked this conversation as resolved.
Show resolved Hide resolved
@Override
public void filter(ContainerRequestContext context)
throws IOException {

context.setProperty("RequestTime", System.currentTimeMillis());
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove a blank line

SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading