World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#758] feat(core): Add Prometheus support to Gravitino metrics system (#815)
FANNG7c7546697e18
diff --git a/core/build.gradle.kts b/core/build.gradle.ktsindex 036bc5ad04..414c20613c 100644--- a/core/build.gradle.kts+++ b/core/build.gradle.kts@@ -24,6 +24,7 @@ dependencies {implementation(libs.caffeine)implementation(libs.rocksdbjni)implementation(libs.bundles.metrics)+ implementation(libs.bundles.prometheus)compileOnly(libs.lombok)annotationProcessor(libs.lombok)diff --git a/core/src/main/java/com/datastrato/gravitino/metrics/MetricsSystem.java b/core/src/main/java/com/datastrato/gravitino/metrics/MetricsSystem.javaindex 2cb9ec4e7b..1d397ec627 100644--- a/core/src/main/java/com/datastrato/gravitino/metrics/MetricsSystem.java+++ b/core/src/main/java/com/datastrato/gravitino/metrics/MetricsSystem.java@@ -11,8 +11,15 @@ import com.codahale.metrics.Reporter;import com.codahale.metrics.jmx.JmxReporter;import com.datastrato.gravitino.metrics.source.MetricsSource;import com.google.common.annotations.VisibleForTesting;+import com.google.common.collect.ImmutableMap;+import io.prometheus.client.CollectorRegistry;+import io.prometheus.client.dropwizard.DropwizardExports;+import io.prometheus.client.dropwizard.samplebuilder.CustomMappingSampleBuilder;+import io.prometheus.client.dropwizard.samplebuilder.MapperConfig;+import io.prometheus.client.exporter.MetricsServlet;import java.io.Closeable;import java.io.IOException;+import java.util.Arrays;import java.util.HashMap;import java.util.LinkedList;import java.util.List;@@ -29,6 +36,7 @@ public class MetricsSystem implements Closeable {private final MetricRegistry metricRegistry;private HashMap<String, MetricsSource> metricSources = new HashMap<>();private List<Reporter> metricsReporters = new LinkedList<>();+ private CollectorRegistry prometheusRegistry;public MetricsSystem() {this("");@@ -37,6 +45,7 @@ public class MetricsSystem implements Closeable {public MetricsSystem(String name) {this.name = name;this.metricRegistry = new MetricRegistry();+ this.prometheusRegistry = new CollectorRegistry();}/**@@ -95,6 +104,7 @@ public class MetricsSystem implements Closeable {}public void start() {+ registerMetricsToPrometheusRegistry();initAndStartMetricsReporter();}@@ -114,4 +124,57 @@ public class MetricsSystem implements Closeable {}});}++ /*+ * Extract a metric name and labels from Dropwizard metrics for Prometheus.+ *+ * All extraction rules must be registered with the Prometheus registry before starting the Prometheus+ * servlet. At times, certain MetricsSources, like HiveCatalogMetricsSource, may not register with+ * MetricsSystem. Therefore, all rules are consolidated in MetricsSystem instead of being spread across+ * separate MetricsSources.+ *+ * If a metric name doesn't match any rules, it will be converted to the Prometheus metrics+ * name format. For example, "ab.c-a.d" transforms into "ab_c_a_d".+ *+ * The MapperConfig is utilized to extract Prometheus metricsName and labels with the following method:+ * `MapperConfig(final String match, final String name, final Map<String, String> labels)`+ * - `match` is a regex used to match the incoming metric name. It employs a simplified glob syntax where+ * only '*' is allowed.+ * - `name` is the new metric name, which can contain placeholders to be replaced with+ * actual values from the incoming metric name. Placeholders are in the ${n} format, where n+ * is the zero-based index of the group to extract from the original metric name.+ * - `labels` are the labels to be extracted, and they should also contain placeholders.+ * E.g.:+ * Match: gravitino.dispatcher.*.*+ * Name: dispatcher_events_total_${0}+ * Labels: label1: ${1}_t+ * A metric "gravitino.dispatcher.sp1.yay" will be converted to a new metric with name+ * "dispatcher_events_total_sp1" with label {label1: yay_t}.+ * Metric names MUST adhere to the regex [a-zA-Z_:]([a-zA-Z0-9_:])*.+ * Label names MUST adhere to the regex [a-zA-Z_]([a-zA-Z0-9_])*.+ * Label values MAY be any sequence of UTF-8 characters .+ */+ @VisibleForTesting+ static List<MapperConfig> getMetricNameAndLabelRules() {+ return Arrays.asList(+ new MapperConfig(+ MetricsSource.ICEBERG_REST_SERVER_METRIC_NAME + ".*.*",+ MetricsSource.ICEBERG_REST_SERVER_METRIC_NAME + "_${1}",+ ImmutableMap.of("operation", "${0}")),+ new MapperConfig(+ MetricsSource.GRAVITINO_SERVER_METRIC_NAME + ".*.*",+ MetricsSource.GRAVITINO_SERVER_METRIC_NAME + "_${1}",+ ImmutableMap.of("operation", "${0}")));+ }++ private void registerMetricsToPrometheusRegistry() {+ CustomMappingSampleBuilder sampleBuilder =+ new CustomMappingSampleBuilder(getMetricNameAndLabelRules());+ DropwizardExports dropwizardExports = new DropwizardExports(metricRegistry, sampleBuilder);+ dropwizardExports.register(prometheusRegistry);+ }++ public MetricsServlet getPrometheusServlet() {+ return new MetricsServlet(prometheusRegistry);+ }}diff --git a/core/src/test/java/com/datastrato/gravitino/metrics/TestExtractMetricNameAndLabel.java b/core/src/test/java/com/datastrato/gravitino/metrics/TestExtractMetricNameAndLabel.javanew file mode 100644index 0000000000..def8d2a107--- /dev/null+++ b/core/src/test/java/com/datastrato/gravitino/metrics/TestExtractMetricNameAndLabel.java@@ -0,0 +1,69 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */++package com.datastrato.gravitino.metrics;++import com.datastrato.gravitino.metrics.source.MetricsSource;+import com.google.common.collect.ImmutableMap;+import io.prometheus.client.Collector;+import io.prometheus.client.Collector.MetricFamilySamples.Sample;+import io.prometheus.client.dropwizard.samplebuilder.CustomMappingSampleBuilder;+import java.util.Arrays;+import java.util.HashSet;+import java.util.Map;+import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.Test;++public class TestExtractMetricNameAndLabel {++ CustomMappingSampleBuilder sampleBuilder =+ new CustomMappingSampleBuilder(MetricsSystem.getMetricNameAndLabelRules());++ private void checkResult(String dropwizardName, String metricName, Map<String, String> labels) {+ Sample sample =+ sampleBuilder.createSample(dropwizardName, "", Arrays.asList(), Arrays.asList(), 0);++ Assertions.assertEquals(metricName, sample.name);+ Assertions.assertEquals(labels.keySet(), new HashSet(sample.labelNames));+ Assertions.assertEquals(new HashSet(labels.values()), new HashSet(sample.labelValues));+ }++ @Test+ void testMapperConfig() {+ checkResult("jvm.total.used", "jvm_total_used", ImmutableMap.of());++ checkResult(+ MetricsSource.ICEBERG_REST_SERVER_METRIC_NAME + "." + MetricNames.SERVER_IDLE_THREAD_NUM,+ Collector.sanitizeMetricName(MetricsSource.ICEBERG_REST_SERVER_METRIC_NAME)+ + "_"+ + Collector.sanitizeMetricName(MetricNames.SERVER_IDLE_THREAD_NUM),+ ImmutableMap.of());++ checkResult(+ MetricsSource.GRAVITINO_SERVER_METRIC_NAME + "." + MetricNames.SERVER_IDLE_THREAD_NUM,+ Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)+ + "_"+ + Collector.sanitizeMetricName(MetricNames.SERVER_IDLE_THREAD_NUM),+ ImmutableMap.of());++ checkResult(+ MetricsSource.ICEBERG_REST_SERVER_METRIC_NAME+ + ".update-table."+ + MetricNames.HTTP_PROCESS_DURATION,+ Collector.sanitizeMetricName(MetricsSource.ICEBERG_REST_SERVER_METRIC_NAME)+ + "_"+ + Collector.sanitizeMetricName(MetricNames.HTTP_PROCESS_DURATION),+ ImmutableMap.of("operation", "update-table"));++ checkResult(+ MetricsSource.GRAVITINO_SERVER_METRIC_NAME+ + ".update-table."+ + MetricNames.HTTP_PROCESS_DURATION,+ Collector.sanitizeMetricName(MetricsSource.GRAVITINO_SERVER_METRIC_NAME)+ + "_"+ + Collector.sanitizeMetricName(MetricNames.HTTP_PROCESS_DURATION),+ ImmutableMap.of("operation", "update-table"));+ }+}diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.tomlindex c288aa1d88..ba4d888e65 100644--- a/gradle/libs.versions.toml+++ b/gradle/libs.versions.toml@@ -42,6 +42,7 @@ publish-plugin = '1.2.0'rat-plugin = '0.8.0'shadow-plugin = "8.1.1"metrics = "4.2.19"+prometheus = "0.16.0"[libraries]@@ -114,6 +115,9 @@ metrics-jersey2 = { group = "io.dropwizard.metrics", name = "metrics-jersey2", vmetrics-jvm = { group = "io.dropwizard.metrics", name = "metrics-jvm", version.ref = "metrics" }metrics-jmx = { group = "io.dropwizard.metrics", name = "metrics-jmx", version.ref = "metrics" }metrics-servlets = { group = "io.dropwizard.metrics", name = "metrics-servlets", version.ref = "metrics" }+prometheus-client = { group = "io.prometheus", name = "simpleclient", version.ref = "prometheus" }+prometheus-dropwizard = { group = "io.prometheus", name = "simpleclient_dropwizard", version.ref = "prometheus" }+prometheus-servlet = { group = "io.prometheus", name = "simpleclient_servlet", version.ref = "prometheus" }[bundles]log4j = ["slf4j-api", "log4j-slf4j2-impl", "log4j-api", "log4j-core", "log4j-12-api"]@@ -122,6 +126,7 @@ jersey = ["jersey-server", "jersey-container-servlet-core", "jersey-container-jeiceberg = ["iceberg-core", "iceberg-api"]jwt = ["jwt-api", "jwt-impl", "jwt-gson"]metrics = ["metrics-core", "metrics-jersey2", "metrics-jvm", "metrics-jmx", "metrics-servlets"]+prometheus = ["prometheus-servlet", "prometheus-dropwizard", "prometheus-client"][plugins]protobuf = { id = "com.google.protobuf", version.ref = "protobuf-plugin" }diff --git a/server-common/build.gradle.kts b/server-common/build.gradle.ktsindex c5fe5529a8..f3e85fda72 100644--- a/server-common/build.gradle.kts+++ b/server-common/build.gradle.kts@@ -24,6 +24,7 @@ dependencies {implementation(libs.bundles.jetty)implementation(libs.bundles.jwt)implementation(libs.bundles.metrics)+ implementation(libs.prometheus.servlet)testImplementation(libs.junit.jupiter.api)testImplementation(libs.junit.jupiter.params)diff --git a/server-common/src/main/java/com/datastrato/gravitino/server/web/JettyServer.java b/server-common/src/main/java/com/datastrato/gravitino/server/web/JettyServer.javaindex 2a35194ec4..ec609a1d23 100644--- a/server-common/src/main/java/com/datastrato/gravitino/server/web/JettyServer.java+++ b/server-common/src/main/java/com/datastrato/gravitino/server/web/JettyServer.java@@ -103,6 +103,9 @@ public final class JettyServer {servletContextHandler.setAttribute("com.codahale.metrics.servlets.MetricsServlet.registry", metricRegistry);servletContextHandler.addServlet(MetricsServlet.class, "/metrics");++ servletContextHandler.addServlet(+ new ServletHolder(metricsSystem.getPrometheusServlet()), "/prometheus/metrics");}HandlerCollection handlers = new HandlerCollection();