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


##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java:
##########
@@ -158,4 +177,247 @@ 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 the blank line



##########
hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java:
##########
@@ -158,4 +177,247 @@ 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() {
+        StringBuilder promMetric = new StringBuilder();
+        ServerReporter reporter = ServerReporter.instance();
+        String helpName = "hugegraph_info";
+        //version
+        promMetric.append(STR_HELP)
+                  .append(helpName).append(END_LSTR);
+        promMetric.append(STR_TYPE)
+                  .append(helpName)
+                  .append(SPACE_STR + UNTYPED + END_LSTR);
+        promMetric.append(helpName)
+                  .append("{version=\"")
+                  .append(ApiVersion.VERSION.toString()).append("\",}")
+                  .append(SPACE_STR + "1.0" + END_LSTR);
+
+        //gauges
+        for (String key : reporter.gauges().keySet()) {
+            final com.codahale.metrics.Gauge<?> gauge
+                    = reporter.gauges().get(key);
+            if (gauge != null) {
+                helpName = replaceDotDashInKey(key);
+                promMetric.append(STR_HELP)
+                          .append(helpName).append(END_LSTR);
+                promMetric.append(STR_TYPE)
+                          .append(helpName).append(SPACE_STR + GAUGE_TYPE + 
END_LSTR);
+                promMetric.append(helpName)
+                          .append(SPACE_STR + gauge.getValue() + END_LSTR);
+            }
+        }
+
+        //histograms
+        for (String histogramkey : reporter.histograms().keySet()) {
+            final Histogram histogram = 
reporter.histograms().get(histogramkey);
+            if (histogram != null) {
+                helpName = replaceDotDashInKey(histogramkey);
+                promMetric.append(STR_HELP)
+                          .append(helpName).append(END_LSTR);
+                promMetric.append(STR_TYPE)
+                          .append(helpName)
+                          .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promMetric.append(helpName)
+                          .append(COUNT_ATTR)
+                          .append(histogram.getCount() + END_LSTR);
+                promMetric.append(
+                        exportSnapshot(helpName, histogram.getSnapshot()));
+            }
+        }
+
+        //meters
+        for (String meterkey : reporter.meters().keySet()) {
+            final Meter metric = reporter.meters().get(meterkey);
+            if (metric != null) {
+                helpName = replaceDotDashInKey(meterkey);
+                promMetric.append(STR_HELP)
+                          .append(helpName).append(END_LSTR);
+                promMetric.append(STR_TYPE)
+                          .append(helpName)
+                          .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promMetric.append(helpName)
+                          .append(COUNT_ATTR)
+                          .append(metric.getCount() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(MEAN_RATE_ATRR)
+                          .append(metric.getMeanRate() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(ONE_MIN_RATE_ATRR)
+                          .append(metric.getOneMinuteRate() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(FIVE_MIN_RATE_ATRR)
+                          .append(metric.getFiveMinuteRate() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(FIFT_MIN_RATE_ATRR)
+                          .append(metric.getFifteenMinuteRate() + END_LSTR);
+            }
+        }
+
+        //timer
+        for (String timerkey : reporter.timers().keySet()) {
+            final com.codahale.metrics.Timer timer = reporter.timers()
+                                                             .get(timerkey);
+            if (timer != null) {
+                helpName = replaceDotDashInKey(timerkey);
+                promMetric.append(STR_HELP)
+                          .append(helpName).append(END_LSTR);
+                promMetric.append(STR_TYPE)
+                          .append(helpName)
+                          .append(SPACE_STR + HISTOGRAM_TYPE + END_LSTR);
+
+                promMetric.append(helpName)
+                          .append(COUNT_ATTR)
+                          .append(timer.getCount() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(ONE_MIN_RATE_ATRR)
+                          .append(timer.getOneMinuteRate() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(FIVE_MIN_RATE_ATRR)
+                          .append(timer.getFiveMinuteRate() + END_LSTR);
+                promMetric.append(helpName)
+                          .append(FIFT_MIN_RATE_ATRR)
+                          .append(timer.getFifteenMinuteRate() + END_LSTR);
+                promMetric.append(
+                        exportSnapshot(helpName, timer.getSnapshot()));
+            }
+        }
+
+        MetricsUtil.writePrometheus(promMetric,
+                                    MetricManager.INSTANCE.getRegistry());
+
+        return promMetric.toString();
+    }
+
+

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";

Review Comment:
   rename the var name to statisticsPath style



##########
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/metrics/MetricsUtil.java:
##########
@@ -40,15 +73,145 @@ 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-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java:
##########
@@ -40,15 +73,145 @@ 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 "path,"



##########
hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsKeys.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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?



-- 
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