HBASE-27395 Adding description to Prometheus metrics (#4807)

Signed-off-by: Tamas Payer <payert@apache.org>
Signed-off-by: Balazs Meszaros <meszibalu@apache.org>
This commit is contained in:
Luca Kovács 2022-10-07 11:31:02 +02:00 committed by GitHub
parent 4b45256910
commit 46d37a70d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -41,7 +41,7 @@ public class PrometheusHadoopServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
writeMetrics(resp.getWriter()); writeMetrics(resp.getWriter(), "true".equals(req.getParameter("description")));
} }
static String toPrometheusName(String metricRecordName, String metricName) { static String toPrometheusName(String metricRecordName, String metricName) {
@ -57,15 +57,21 @@ 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) throws IOException { void writeMetrics(Writer writer, boolean desc) 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()) {
if (metrics.type() == MetricType.COUNTER || metrics.type() == MetricType.GAUGE) { if (metrics.type() == MetricType.COUNTER || metrics.type() == MetricType.GAUGE) {
String key = toPrometheusName(metricsRecord.name(), metrics.name()); String key = toPrometheusName(metricsRecord.name(), metrics.name());
if (desc) {
String description = metrics.description();
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
}
writer.append("# TYPE ").append(key).append(" ") writer.append("# TYPE ").append(key).append(" ")
.append(metrics.type().toString().toLowerCase()).append("\n").append(key).append("{"); .append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{");
/* add tags */ /* add tags */
String sep = ""; String sep = "";

View File

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