Skip to content

Commit 37c82a6

Browse files
authored
HBASE-27435 Make Prometheus metrics queryable (#4879)
Signed-off-by: Balazs Meszaros <meszibalu@apache.org>
1 parent 4d70f94 commit 37c82a6

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

hbase-http/src/main/java/org/apache/hadoop/hbase/http/prometheus/PrometheusHadoopServlet.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@
3535

3636
@InterfaceAudience.Private
3737
public class PrometheusHadoopServlet extends HttpServlet {
38-
3938
private static final Pattern SPLIT_PATTERN =
4039
Pattern.compile("(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=([A-Z][a-z]))|\\W|(_)+");
4140

4241
@Override
4342
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
44-
writeMetrics(resp.getWriter(), "true".equals(req.getParameter("description")));
43+
writeMetrics(resp.getWriter(), "true".equals(req.getParameter("description")),
44+
req.getParameter("qry"));
4545
}
4646

4747
static String toPrometheusName(String metricRecordName, String metricName) {
@@ -57,31 +57,35 @@ static String toPrometheusName(String metricRecordName, String metricName) {
5757
*/
5858
@RestrictedApi(explanation = "Should only be called in tests or self", link = "",
5959
allowedOnPath = ".*/src/test/.*|.*/PrometheusHadoopServlet\\.java")
60-
void writeMetrics(Writer writer, boolean desc) throws IOException {
60+
void writeMetrics(Writer writer, boolean descriptionEnabled, String queryParam)
61+
throws IOException {
6162
Collection<MetricsRecord> metricRecords = MetricsExportHelper.export();
6263
for (MetricsRecord metricsRecord : metricRecords) {
6364
for (AbstractMetric metrics : metricsRecord.metrics()) {
6465
if (metrics.type() == MetricType.COUNTER || metrics.type() == MetricType.GAUGE) {
6566

6667
String key = toPrometheusName(metricsRecord.name(), metrics.name());
6768

68-
if (desc) {
69-
String description = metrics.description();
70-
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
71-
}
69+
if (queryParam == null || key.contains(queryParam)) {
70+
71+
if (descriptionEnabled) {
72+
String description = metrics.description();
73+
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
74+
}
7275

73-
writer.append("# TYPE ").append(key).append(" ")
74-
.append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{");
76+
writer.append("# TYPE ").append(key).append(" ")
77+
.append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{");
7578

76-
/* add tags */
77-
String sep = "";
78-
for (MetricsTag tag : metricsRecord.tags()) {
79-
String tagName = tag.name().toLowerCase();
80-
writer.append(sep).append(tagName).append("=\"").append(tag.value()).append("\"");
81-
sep = ",";
79+
/* add tags */
80+
String sep = "";
81+
for (MetricsTag tag : metricsRecord.tags()) {
82+
String tagName = tag.name().toLowerCase();
83+
writer.append(sep).append(tagName).append("=\"").append(tag.value()).append("\"");
84+
sep = ",";
85+
}
86+
writer.append("} ");
87+
writer.append(metrics.value().toString()).append('\n');
8288
}
83-
writer.append("} ");
84-
writer.append(metrics.value().toString()).append('\n');
8589
}
8690
}
8791
}

hbase-http/src/test/java/org/apache/hadoop/hbase/http/prometheus/TestPrometheusServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void testPublish() throws IOException {
6161
// WHEN
6262
PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet();
6363
// Test with no description
64-
prom2Servlet.writeMetrics(writer, false);
64+
prom2Servlet.writeMetrics(writer, false, null);
6565

6666
// THEN
6767
String writtenMetrics = stream.toString(UTF_8.name());

0 commit comments

Comments
 (0)