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
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
return JsonUtil.toJson(results);
}


@GET
@Timed
@Path("gauges")
Expand Down Expand Up @@ -178,7 +177,6 @@
return JsonUtil.toJson(reporter.timers());
}


@GET
@Timed
@Produces(APPLICATION_TEXT_WITH_CHARSET)
Expand All @@ -202,7 +200,7 @@
Map<String, Map<String, Object>> metricMap = statistics();

if (type != null && type.equals(JSON_STR)) {
return JsonUtil.toJson(metricMap);

Check warning on line 203 in hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/api/metrics/MetricsAPI.java#L203

Added line #L203 was not covered by tests
}
return statisticsProm(metricMap);
}
Expand Down Expand Up @@ -325,13 +323,12 @@
}
}

MetricsUtil.writePrometheus(promMetric,
MetricsUtil.writePrometheusFormat(promMetric,
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
MetricManager.INSTANCE.getRegistry());

return promMetric.toString();
}


private Map<String, Map<String, Object>> statistics() {
Map<String, Map<String, Object>> metricsMap = new HashMap<>();
ServerReporter reporter = ServerReporter.instance();
Expand Down Expand Up @@ -386,7 +383,6 @@
return metricsMap;
}


private String statisticsProm(Map<String, Map<String, Object>> metricMap) {
StringBuilder promMetric = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,16 @@
package org.apache.hugegraph.metrics;

public enum MetricsKeys {
// 最大响应时间 Maximum response time
// 平均响应时间 Mean response time
// 请求总数 Total Requests
// 失败数 Failed Requests
// 成功数 Success Requests

MAX_RESPONSE_TIME(1, "MAX_RESPONSE_TIME"),
MAX_RESPONSE_TIME(1, "max_response_time"),

MEAN_RESPONSE_TIME(2, "MEAN_RESPONSE_TIME"),
MEAN_RESPONSE_TIME(2, "mean_response_time"),

TOTAL_REQUEST(3, "TOTAL_REQUEST"),
TOTAL_REQUEST(3, "total_request"),

FAILED_REQUEST(4, "FAILED_REQUEST"),
FAILED_REQUEST(4, "failed_request"),

SUCCESS_REQUEST(5, "SUCCESS_REQUEST"),
;
SUCCESS_REQUEST(5, "success_request");

private final byte code;
private final String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
return orgKey.replace("/", "_");
}

public static void writePrometheus(StringBuilder promeMetrics,
public static void writePrometheusFormat(StringBuilder promeMetrics,
SunnyBoy-WYH marked this conversation as resolved.
Show resolved Hide resolved
MetricRegistry registry) {
// gauges
registry.getGauges().forEach((key, gauge) -> {
Expand Down Expand Up @@ -189,7 +189,7 @@

public static String exportSnapshot(final String helpName, final Snapshot snapshot) {
if (snapshot == null) {
return "";

Check warning on line 192 in hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-api/src/main/java/org/apache/hugegraph/metrics/MetricsUtil.java#L192

Added line #L192 was not covered by tests
}
StringBuilder snapMetrics = new StringBuilder();
snapMetrics.append(helpName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class MetricsApiTest extends BaseApiTest {

private static final String path = "/metrics";
private static final String statistics_path = path + "/statistics";
private static final String statisticsPath = path + "/statistics";

@Test
public void testBaseMetricsAll() {
Expand Down Expand Up @@ -59,7 +59,7 @@ public void testStatisticsMetricsAll() {

@Test
public void testStatisticsMetricsPromAll() {
Response r = client().get(statistics_path);
Response r = client().get(statisticsPath);
assertResponseStatus(200, r);
}

Expand Down
Loading