Skip to content

HBASE-27395 Adding description to Prometheus metrics #4807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PrometheusHadoopServlet extends HttpServlet {

@Override
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) {
Expand All @@ -57,15 +57,21 @@ 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) throws IOException {
void writeMetrics(Writer writer, boolean desc) throws IOException {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 'describe' is better argument name than 'desc'.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use 'desc', because 'description' is used as a local variable name in the function

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');
}

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 */
String sep = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public void testPublish() throws IOException {

// WHEN
PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet();
prom2Servlet.writeMetrics(writer);
// Test with no description
prom2Servlet.writeMetrics(writer, false);

// THEN
String writtenMetrics = stream.toString(UTF_8.name());
Expand Down