diff --git a/hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoopServlet.java b/hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoopServlet.java index db5952e2fa78..25bd57927d3d 100644 --- a/hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoopServlet.java +++ b/hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoopServlet.java @@ -35,13 +35,13 @@ @InterfaceAudience.Private public class PrometheusHadoopServlet extends HttpServlet { - private static final Pattern SPLIT_PATTERN = Pattern.compile("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=([A-Z][a-z]))|\\W|(_)+"); @Override 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) { @@ -57,7 +57,8 @@ static String toPrometheusName(String metricRecordName, String metricName) { */ @RestrictedApi(explanation = "Should only be called in tests or self", link = "", allowedOnPath = ".*/src/test/.*|.*/PrometheusHadoopServlet\\.java") - void writeMetrics(Writer writer, boolean desc) throws IOException { + void writeMetrics(Writer writer, boolean descriptionEnabled, String queryParam) + throws IOException { Collection metricRecords = MetricsExportHelper.export(); for (MetricsRecord metricsRecord : metricRecords) { for (AbstractMetric metrics : metricsRecord.metrics()) { @@ -65,23 +66,26 @@ void writeMetrics(Writer writer, boolean desc) throws IOException { String key = toPrometheusName(metricsRecord.name(), metrics.name()); - if (desc) { - String description = metrics.description(); - if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n'); - } + if (queryParam == null || key.contains(queryParam)) { + + if (descriptionEnabled) { + String description = metrics.description(); + if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n'); + } - writer.append("# TYPE ").append(key).append(" ") - .append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{"); + writer.append("# TYPE ").append(key).append(" ") + .append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{"); - /* add tags */ - String sep = ""; - for (MetricsTag tag : metricsRecord.tags()) { - String tagName = tag.name().toLowerCase(); - writer.append(sep).append(tagName).append("=\"").append(tag.value()).append("\""); - sep = ","; + /* add tags */ + String sep = ""; + for (MetricsTag tag : metricsRecord.tags()) { + String tagName = tag.name().toLowerCase(); + writer.append(sep).append(tagName).append("=\"").append(tag.value()).append("\""); + sep = ","; + } + writer.append("} "); + writer.append(metrics.value().toString()).append('\n'); } - writer.append("} "); - writer.append(metrics.value().toString()).append('\n'); } } } diff --git a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/prometheus/TestPrometheusServlet.java b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/prometheus/TestPrometheusServlet.java index 0ca021bc12bb..276a2b9ad296 100644 --- a/hbase-http/src/test/java/org/apache/hadoop/hbase/http/prometheus/TestPrometheusServlet.java +++ b/hbase-http/src/test/java/org/apache/hadoop/hbase/http/prometheus/TestPrometheusServlet.java @@ -61,7 +61,7 @@ public void testPublish() throws IOException { // WHEN PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet(); // Test with no description - prom2Servlet.writeMetrics(writer, false); + prom2Servlet.writeMetrics(writer, false, null); // THEN String writtenMetrics = stream.toString(UTF_8.name());