HBASE-27435 Make Prometheus metrics queryable (#4879)

Signed-off-by: Balazs Meszaros <meszibalu@apache.org>
This commit is contained in:
Luca Kovács 2022-12-12 11:59:03 +01:00 committed by GitHub
parent 4d70f94ffe
commit 37c82a6209
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 18 deletions

View File

@ -35,13 +35,13 @@ import org.apache.yetus.audience.InterfaceAudience;
@InterfaceAudience.Private @InterfaceAudience.Private
public class PrometheusHadoopServlet extends HttpServlet { public class PrometheusHadoopServlet extends HttpServlet {
private static final Pattern SPLIT_PATTERN = private static final Pattern SPLIT_PATTERN =
Pattern.compile("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=([A-Z][a-z]))|\\W|(_)+"); Pattern.compile("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=([A-Z][a-z]))|\\W|(_)+");
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
writeMetrics(resp.getWriter(), "true".equals(req.getParameter("description"))); writeMetrics(resp.getWriter(), "true".equals(req.getParameter("description")),
req.getParameter("qry"));
} }
static String toPrometheusName(String metricRecordName, String metricName) { static String toPrometheusName(String metricRecordName, String metricName) {
@ -57,7 +57,8 @@ public class PrometheusHadoopServlet extends HttpServlet {
*/ */
@RestrictedApi(explanation = "Should only be called in tests or self", link = "", @RestrictedApi(explanation = "Should only be called in tests or self", link = "",
allowedOnPath = ".*/src/test/.*|.*/PrometheusHadoopServlet\\.java") allowedOnPath = ".*/src/test/.*|.*/PrometheusHadoopServlet\\.java")
void writeMetrics(Writer writer, boolean desc) throws IOException { void writeMetrics(Writer writer, boolean descriptionEnabled, String queryParam)
throws IOException {
Collection<MetricsRecord> metricRecords = MetricsExportHelper.export(); Collection<MetricsRecord> metricRecords = MetricsExportHelper.export();
for (MetricsRecord metricsRecord : metricRecords) { for (MetricsRecord metricsRecord : metricRecords) {
for (AbstractMetric metrics : metricsRecord.metrics()) { for (AbstractMetric metrics : metricsRecord.metrics()) {
@ -65,7 +66,9 @@ public class PrometheusHadoopServlet extends HttpServlet {
String key = toPrometheusName(metricsRecord.name(), metrics.name()); String key = toPrometheusName(metricsRecord.name(), metrics.name());
if (desc) { if (queryParam == null || key.contains(queryParam)) {
if (descriptionEnabled) {
String description = metrics.description(); String description = metrics.description();
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n'); if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
} }
@ -85,6 +88,7 @@ public class PrometheusHadoopServlet extends HttpServlet {
} }
} }
} }
}
writer.flush(); writer.flush();
} }
} }

View File

@ -61,7 +61,7 @@ public class TestPrometheusServlet {
// WHEN // WHEN
PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet(); PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet();
// Test with no description // Test with no description
prom2Servlet.writeMetrics(writer, false); prom2Servlet.writeMetrics(writer, false, null);
// THEN // THEN
String writtenMetrics = stream.toString(UTF_8.name()); String writtenMetrics = stream.toString(UTF_8.name());