javeme commented on code in PR #2286:
URL: 
https://github.com/apache/incubator-hugegraph/pull/2286#discussion_r1320685171


##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java:
##########
@@ -40,15 +73,147 @@ public static Counter registerCounter(Class<?> clazz, 
String name) {
         return REGISTRY.counter(MetricRegistry.name(clazz, name));
     }
 
+    public static Counter registerCounter(String name) {
+        return REGISTRY.counter(MetricRegistry.name(name));
+    }
+
     public static Histogram registerHistogram(Class<?> clazz, String name) {
         return REGISTRY.histogram(MetricRegistry.name(clazz, name));
     }
 
+    public static Histogram registerHistogram(String name) {
+        return REGISTRY.histogram(name);
+    }
+
+
     public static Meter registerMeter(Class<?> clazz, String name) {
         return REGISTRY.meter(MetricRegistry.name(clazz, name));
     }
 
     public static Timer registerTimer(Class<?> clazz, String name) {
         return REGISTRY.timer(MetricRegistry.name(clazz, name));
     }
+
+    public static String replaceDotDashInKey(String orgKey) {
+        return orgKey.replace(".", "_").replace("-", "_");
+    }
+
+    public static String replaceSlashInKey(String orgKey) {
+        return orgKey.replace("/", "_");
+    }
+
+    public static void writePrometheus(StringBuilder promeMetrics,
+                                       MetricRegistry registry) {
+        // gauges
+        registry.getGauges().forEach((key, gauge) -> {
+            if (gauge != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName).append(SPACE_STR + GAUGE_TYPE + 
END_LSTR);
+                
promeMetrics.append(helpName).append(SPACE_STR).append(gauge.getValue())
+                            .append(END_LSTR);
+            }
+        });
+
+        // histograms
+        registry.getHistograms().forEach((key, histogram) -> {
+            if (histogram != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName)
+                            .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promeMetrics.append(helpName)
+                            
.append(COUNT_ATTR).append(histogram.getCount()).append(END_LSTR);
+                promeMetrics.append(
+                        exportSnapshort(helpName, histogram.getSnapshot()));
+            }
+        });
+
+        // meters
+        registry.getMeters().forEach((key, metric) -> {
+            if (metric != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName)
+                            .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promeMetrics.append(helpName)
+                            
.append(COUNT_ATTR).append(metric.getCount()).append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(MEAN_RATE_ATRR).append(metric.getMeanRate()).append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(ONE_MIN_RATE_ATRR).append(metric.getOneMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIVE_MIN_RATE_ATRR).append(metric.getFiveMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIFT_MIN_RATE_ATRR).append(metric.getFifteenMinuteRate())
+                            .append(END_LSTR);
+            }
+        });
+
+        // timer
+        registry.getTimers().forEach((key, timer) -> {
+            if (timer != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName)
+                            .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promeMetrics.append(helpName)
+                            
.append(COUNT_ATTR).append(timer.getCount()).append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(ONE_MIN_RATE_ATRR).append(timer.getOneMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIVE_MIN_RATE_ATRR).append(timer.getFiveMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIFT_MIN_RATE_ATRR).append(timer.getFifteenMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(
+                        exportSnapshort(helpName, timer.getSnapshot()));
+            }
+        });
+    }
+
+    public static String exportSnapshort(final String helpName, final Snapshot 
snapshot) {
+        if (snapshot != null) {
+            StringBuilder snapMetrics = new StringBuilder();
+            snapMetrics.append(helpName)
+                       
.append(MIN_ATTR).append(snapshot.getMin()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(MAX_ATTR).append(snapshot.getMax()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(MEAN_ATTR).append(snapshot.getMean()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(STDDEV_ATTR).append(snapshot.getStdDev()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(P50_ATTR).append(snapshot.getMedian()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(P75_ATTR).append(snapshot.get75thPercentile()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(P95_ATTR).append(snapshot.get95thPercentile()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(P98_ATTR).append(snapshot.get98thPercentile()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(P99_ATTR).append(snapshot.get99thPercentile()).append(END_LSTR);
+            snapMetrics.append(helpName)
+                       
.append(P999_ATTR).append(snapshot.get999thPercentile()).append(END_LSTR);
+            return snapMetrics.toString();
+        }
+        return "";
+    }
+
+

Review Comment:
   remove the 2 blank lines



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java:
##########
@@ -158,4 +177,253 @@ public String timers() {
         ServerReporter reporter = ServerReporter.instance();
         return JsonUtil.toJson(reporter.timers());
     }
+
+
+    @GET
+    @Timed
+    @Produces(APPLICATION_TEXT_WITH_CHARSET)
+    @RolesAllowed({"admin", "$owner= $action=metrics_read"})
+    public String all(@Context GraphManager manager,
+                      @QueryParam("type") String type) {
+
+        if (type != null && type.equals(JSON_STR)) {
+            return baseMetricAll();
+        } else {
+            return baseMetricPrometheusAll();
+        }
+    }
+
+    @GET
+    @Path("statistics")
+    @Timed
+    @Produces(APPLICATION_TEXT_WITH_CHARSET)
+    @RolesAllowed({"admin", "$owner= $action=metrics_read"})
+    public String statistics(@QueryParam("type") String type) {
+        Map<String, Map<String, Object>> metricMap = statistics();
+
+        if (type != null && type.equals(JSON_STR)) {
+            return JsonUtil.toJson(metricMap);
+        }
+        return statisticsProm(metricMap);
+
+    }
+
+

Review Comment:
   remove a blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.api.filter;
+
+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 {
+
+
+    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 {
+        // 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))

Review Comment:
   seems we can move to the previous line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+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 {
+
+
+    @Override
+    public void filter(ContainerRequestContext context)
+            throws IOException {
+
+        context.setProperty("RequestTime", System.currentTimeMillis());
+

Review Comment:
   remove a blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsKeys.java:
##########
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.metrics;
+
+public enum MetricsKeys {
+    // 最大响应时间 Maximum response time
+    // 平均响应时间 Mean response time
+    // 请求总数 Total Requests
+    // 失败数   Failed Requests
+    // 成功数   Success Requests

Review Comment:
   remove unused comments or translate them?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java:
##########
@@ -40,15 +73,147 @@ public static Counter registerCounter(Class<?> clazz, 
String name) {
         return REGISTRY.counter(MetricRegistry.name(clazz, name));
     }
 
+    public static Counter registerCounter(String name) {
+        return REGISTRY.counter(MetricRegistry.name(name));
+    }
+
     public static Histogram registerHistogram(Class<?> clazz, String name) {
         return REGISTRY.histogram(MetricRegistry.name(clazz, name));
     }
 
+    public static Histogram registerHistogram(String name) {
+        return REGISTRY.histogram(name);
+    }
+
+
     public static Meter registerMeter(Class<?> clazz, String name) {
         return REGISTRY.meter(MetricRegistry.name(clazz, name));
     }
 
     public static Timer registerTimer(Class<?> clazz, String name) {
         return REGISTRY.timer(MetricRegistry.name(clazz, name));
     }
+
+    public static String replaceDotDashInKey(String orgKey) {
+        return orgKey.replace(".", "_").replace("-", "_");
+    }
+
+    public static String replaceSlashInKey(String orgKey) {
+        return orgKey.replace("/", "_");
+    }
+
+    public static void writePrometheus(StringBuilder promeMetrics,
+                                       MetricRegistry registry) {
+        // gauges
+        registry.getGauges().forEach((key, gauge) -> {
+            if (gauge != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName).append(SPACE_STR + GAUGE_TYPE + 
END_LSTR);
+                
promeMetrics.append(helpName).append(SPACE_STR).append(gauge.getValue())
+                            .append(END_LSTR);
+            }
+        });
+
+        // histograms
+        registry.getHistograms().forEach((key, histogram) -> {
+            if (histogram != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName)
+                            .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promeMetrics.append(helpName)
+                            
.append(COUNT_ATTR).append(histogram.getCount()).append(END_LSTR);
+                promeMetrics.append(
+                        exportSnapshort(helpName, histogram.getSnapshot()));
+            }
+        });
+
+        // meters
+        registry.getMeters().forEach((key, metric) -> {
+            if (metric != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName)
+                            .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promeMetrics.append(helpName)
+                            
.append(COUNT_ATTR).append(metric.getCount()).append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(MEAN_RATE_ATRR).append(metric.getMeanRate()).append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(ONE_MIN_RATE_ATRR).append(metric.getOneMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIVE_MIN_RATE_ATRR).append(metric.getFiveMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIFT_MIN_RATE_ATRR).append(metric.getFifteenMinuteRate())
+                            .append(END_LSTR);
+            }
+        });
+
+        // timer
+        registry.getTimers().forEach((key, timer) -> {
+            if (timer != null) {
+                String helpName = replaceDotDashInKey(key);
+                promeMetrics.append(STR_HELP)
+                            .append(helpName).append(END_LSTR);
+                promeMetrics.append(STR_TYPE)
+                            .append(helpName)
+                            .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promeMetrics.append(helpName)
+                            
.append(COUNT_ATTR).append(timer.getCount()).append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(ONE_MIN_RATE_ATRR).append(timer.getOneMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIVE_MIN_RATE_ATRR).append(timer.getFiveMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(helpName)
+                            
.append(FIFT_MIN_RATE_ATRR).append(timer.getFifteenMinuteRate())
+                            .append(END_LSTR);
+                promeMetrics.append(
+                        exportSnapshort(helpName, timer.getSnapshot()));
+            }
+        });
+    }
+
+    public static String exportSnapshort(final String helpName, final Snapshot 
snapshot) {

Review Comment:
   exportSnapshort => exportSnapshot



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.api.filter;
+
+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 {
+
+
+    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 {

Review Comment:
   prefer to align with ContainerResponseContext



##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java:
##########
@@ -40,15 +73,147 @@ public static Counter registerCounter(Class<?> clazz, 
String name) {
         return REGISTRY.counter(MetricRegistry.name(clazz, name));
     }
 
+    public static Counter registerCounter(String name) {
+        return REGISTRY.counter(MetricRegistry.name(name));
+    }
+
     public static Histogram registerHistogram(Class<?> clazz, String name) {
         return REGISTRY.histogram(MetricRegistry.name(clazz, name));
     }
 
+    public static Histogram registerHistogram(String name) {
+        return REGISTRY.histogram(name);
+    }
+
+

Review Comment:
   remove a blank line



##########
hugegraph-test/src/main/java/org/apache/hugegraph/api/MetricsApiTest.java:
##########
@@ -17,20 +17,24 @@
 
 package org.apache.hugegraph.api;
 
+import java.util.HashMap;
 import java.util.Map;
 
-import jakarta.ws.rs.core.Response;
+import org.apache.hugegraph.testutil.Assert;
 import org.junit.Test;
 
-import org.apache.hugegraph.testutil.Assert;
+import jakarta.ws.rs.core.Response;
 
 public class MetricsApiTest extends BaseApiTest {
 
-    private static String path = "/metrics";
+    private static final String path = "/metrics";
+    private static final String statistics_path = path + "/statistics";
 
     @Test
-    public void testMetricsAll() {
-        Response r = client().get(path);
+    public void testBaseMetricsAll() {
+        Map<String, Object> params = new HashMap<>();
+        params.put("type", "json");
+        Response r = client().get(path,params);

Review Comment:
   expect a space after the ","



##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java:
##########
@@ -24,12 +24,45 @@
 import com.codahale.metrics.Histogram;
 import com.codahale.metrics.Meter;
 import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.Snapshot;
 import com.codahale.metrics.Timer;
 
 public class MetricsUtil {
 
-    private static final MetricRegistry REGISTRY =
-                                        MetricManager.INSTANCE.getRegistry();
+    public static final String METRICS_PATH_TOTAL_COUNTER = "TOTAL_COUNTER";

Review Comment:
   does these name will be showed to users? if yes please rename to 
total_counter style?



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java:
##########
@@ -158,4 +177,253 @@ public String timers() {
         ServerReporter reporter = ServerReporter.instance();
         return JsonUtil.toJson(reporter.timers());
     }
+
+
+    @GET
+    @Timed
+    @Produces(APPLICATION_TEXT_WITH_CHARSET)
+    @RolesAllowed({"admin", "$owner= $action=metrics_read"})
+    public String all(@Context GraphManager manager,
+                      @QueryParam("type") String type) {
+

Review Comment:
   remove a blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java:
##########
@@ -158,4 +177,253 @@ public String timers() {
         ServerReporter reporter = ServerReporter.instance();
         return JsonUtil.toJson(reporter.timers());
     }
+
+
+    @GET
+    @Timed
+    @Produces(APPLICATION_TEXT_WITH_CHARSET)
+    @RolesAllowed({"admin", "$owner= $action=metrics_read"})
+    public String all(@Context GraphManager manager,
+                      @QueryParam("type") String type) {
+
+        if (type != null && type.equals(JSON_STR)) {
+            return baseMetricAll();
+        } else {
+            return baseMetricPrometheusAll();
+        }
+    }
+
+    @GET
+    @Path("statistics")
+    @Timed
+    @Produces(APPLICATION_TEXT_WITH_CHARSET)
+    @RolesAllowed({"admin", "$owner= $action=metrics_read"})
+    public String statistics(@QueryParam("type") String type) {
+        Map<String, Map<String, Object>> metricMap = statistics();
+
+        if (type != null && type.equals(JSON_STR)) {
+            return JsonUtil.toJson(metricMap);
+        }
+        return statisticsProm(metricMap);
+
+    }
+
+
+    public String baseMetricAll() {
+        ServerReporter reporter = ServerReporter.instance();
+        Map<String, Map<String, ? extends Metric>> result = new 
LinkedHashMap<>();
+        result.put("gauges", reporter.gauges());
+        result.put("counters", reporter.counters());
+        result.put("histograms", reporter.histograms());
+        result.put("meters", reporter.meters());
+        result.put("timers", reporter.timers());
+        return JsonUtil.toJson(result);
+    }
+
+    private String baseMetricPrometheusAll() {
+

Review Comment:
   remove a blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java:
##########
@@ -40,15 +73,147 @@ public static Counter registerCounter(Class<?> clazz, 
String name) {
         return REGISTRY.counter(MetricRegistry.name(clazz, name));
     }
 
+    public static Counter registerCounter(String name) {
+        return REGISTRY.counter(MetricRegistry.name(name));
+    }
+
     public static Histogram registerHistogram(Class<?> clazz, String name) {
         return REGISTRY.histogram(MetricRegistry.name(clazz, name));
     }
 
+    public static Histogram registerHistogram(String name) {
+        return REGISTRY.histogram(name);
+    }
+
+
     public static Meter registerMeter(Class<?> clazz, String name) {
         return REGISTRY.meter(MetricRegistry.name(clazz, name));
     }
 
     public static Timer registerTimer(Class<?> clazz, String name) {
         return REGISTRY.timer(MetricRegistry.name(clazz, name));
     }
+
+    public static String replaceDotDashInKey(String orgKey) {
+        return orgKey.replace(".", "_").replace("-", "_");
+    }
+
+    public static String replaceSlashInKey(String orgKey) {
+        return orgKey.replace("/", "_");
+    }
+
+    public static void writePrometheus(StringBuilder promeMetrics,

Review Comment:
   writePrometheusFormat



##########
hugegraph-test/src/main/java/org/apache/hugegraph/api/MetricsApiTest.java:
##########
@@ -17,20 +17,24 @@
 
 package org.apache.hugegraph.api;
 
+import java.util.HashMap;
 import java.util.Map;
 
-import jakarta.ws.rs.core.Response;
+import org.apache.hugegraph.testutil.Assert;
 import org.junit.Test;
 
-import org.apache.hugegraph.testutil.Assert;
+import jakarta.ws.rs.core.Response;
 
 public class MetricsApiTest extends BaseApiTest {
 
-    private static String path = "/metrics";
+    private static final String path = "/metrics";
+    private static final String statistics_path = path + "/statistics";

Review Comment:
   statisticsPath



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+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 {
+
+

Review Comment:
   remove a blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.api.filter;
+
+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 {
+
+
+    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 {
+        // 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))
+                   .inc();
+        if (responseContext.getStatus() == 200 ||
+            responseContext.getStatus() == 201 ||
+            responseContext.getStatus() == 202) {
+            MetricsUtil.registerCounter(
+                               join(metricsName, METRICS_PATH_SUCCESS_COUNTER))

Review Comment:
   ditto



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.api.filter;
+
+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 {
+
+
+    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 {
+        // 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))
+                   .inc();
+        if (responseContext.getStatus() == 200 ||
+            responseContext.getStatus() == 201 ||
+            responseContext.getStatus() == 202) {
+            MetricsUtil.registerCounter(
+                               join(metricsName, METRICS_PATH_SUCCESS_COUNTER))
+                       .inc();
+        } else {
+            MetricsUtil.registerCounter(
+                               join(metricsName, METRICS_PATH_FAILED_COUNTER))
+                       .inc();
+        }
+
+        //  get responseTime

Review Comment:
   expect just one space after "//"



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/PathFilter.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+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 {
+
+
+    @Override
+    public void filter(ContainerRequestContext context)
+            throws IOException {
+
+        context.setProperty("RequestTime", System.currentTimeMillis());

Review Comment:
   expect request_time style



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/filter/AccessLogFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with this
+ * work for additional information regarding copyright ownership. The ASF
+ * licenses this file to You under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.hugegraph.api.filter;
+
+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 {
+
+
+    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 {
+        // 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))
+                   .inc();
+        if (responseContext.getStatus() == 200 ||
+            responseContext.getStatus() == 201 ||
+            responseContext.getStatus() == 202) {

Review Comment:
   add a method `boolean statusOK(status)`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]


Reply via email to