HBASE-27435 Make Prometheus metrics queryable (#4879)
Signed-off-by: Balazs Meszaros <meszibalu@apache.org>
This commit is contained in:
parent
ed53549104
commit
98d66828bd
@ -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,23 +66,26 @@ 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)) {
|
||||||
String description = metrics.description();
|
|
||||||
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.append("# TYPE ").append(key).append(" ")
|
if (descriptionEnabled) {
|
||||||
.append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{");
|
String description = metrics.description();
|
||||||
|
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
|
||||||
|
}
|
||||||
|
|
||||||
/* add tags */
|
writer.append("# TYPE ").append(key).append(" ")
|
||||||
String sep = "";
|
.append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{");
|
||||||
for (MetricsTag tag : metricsRecord.tags()) {
|
|
||||||
String tagName = tag.name().toLowerCase();
|
/* add tags */
|
||||||
writer.append(sep).append(tagName).append("=\"").append(tag.value()).append("\"");
|
String sep = "";
|
||||||
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');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user