Skip to content

Commit f5df769

Browse files
lucakovacsmeszibalu
authored andcommitted
HBASE-27395 Adding description to Prometheus metrics (#4807)
Signed-off-by: Tamas Payer <payert@apache.org> Signed-off-by: Balazs Meszaros <meszibalu@apache.org>
1 parent 397ab69 commit f5df769

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class PrometheusHadoopServlet extends HttpServlet {
4141

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

4747
static String toPrometheusName(String metricRecordName, String metricName) {
@@ -57,15 +57,21 @@ 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) throws IOException {
60+
void writeMetrics(Writer writer, boolean desc) throws IOException {
6161
Collection<MetricsRecord> metricRecords = MetricsExportHelper.export();
6262
for (MetricsRecord metricsRecord : metricRecords) {
6363
for (AbstractMetric metrics : metricsRecord.metrics()) {
6464
if (metrics.type() == MetricType.COUNTER || metrics.type() == MetricType.GAUGE) {
6565

6666
String key = toPrometheusName(metricsRecord.name(), metrics.name());
67+
68+
if (desc) {
69+
String description = metrics.description();
70+
if (!description.isEmpty()) writer.append("# HELP ").append(description).append('\n');
71+
}
72+
6773
writer.append("# TYPE ").append(key).append(" ")
68-
.append(metrics.type().toString().toLowerCase()).append("\n").append(key).append("{");
74+
.append(metrics.type().toString().toLowerCase()).append('\n').append(key).append("{");
6975

7076
/* add tags */
7177
String sep = "";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public void testPublish() throws IOException {
6060

6161
// WHEN
6262
PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet();
63-
prom2Servlet.writeMetrics(writer);
63+
// Test with no description
64+
prom2Servlet.writeMetrics(writer, false);
6465

6566
// THEN
6667
String writtenMetrics = stream.toString(UTF_8.name());

0 commit comments

Comments
 (0)