Skip to content

Commit

Permalink
HBASE-27435 Make Prometheus metrics queryable (#4879)
Browse files Browse the repository at this point in the history
Signed-off-by: Balazs Meszaros <[email protected]>
  • Loading branch information
lucakovacs authored Dec 12, 2022
1 parent 4d70f94 commit 37c82a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -57,31 +57,35 @@ 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<MetricsRecord> metricRecords = MetricsExportHelper.export();
for (MetricsRecord metricsRecord : metricRecords) {
for (AbstractMetric metrics : metricsRecord.metrics()) {
if (metrics.type() == MetricType.COUNTER || metrics.type() == MetricType.GAUGE) {

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');
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 37c82a6

Please sign in to comment.