aajisaka commented on a change in pull request #3426:
URL: https://github.com/apache/hadoop/pull/3426#discussion_r709882234
##########
File path:
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/PrometheusMetricsSink.java
##########
@@ -129,6 +142,12 @@ public void writeMetrics(Writer writer) throws IOException
{
sep = ",";
}
}
+ if (customTags.size() > 0) {
Review comment:
Nit: I think it is clearer to use `#isEmpty()`.
```suggestion
if (!customTags.isEmpty()) {
```
##########
File path:
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/metrics2/sink/PrometheusMetricsSink.java
##########
@@ -98,22 +108,25 @@ public void writeMetrics(Writer writer) throws IOException
{
for (Map.Entry<String, Map<Collection<MetricsTag>, AbstractMetric>>
promMetric :
promMetrics.entrySet()) {
AbstractMetric firstMetric =
promMetric.getValue().values().iterator().next();
+ List<String> customTags = new ArrayList<>();
Review comment:
The ArrayList instance is created for each metric. Would you create an
instance outside of the for-loop and reuse it?
##########
File path:
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/metrics2/sink/TestPrometheusMetricsSink.java
##########
@@ -219,6 +226,62 @@ public void testNamingWhitespaces() {
sink.prometheusName(recordName, metricName));
}
+ /**
+ * testTopMetricsPublish.
+ */
+ @Test
+ public void testTopMetricsPublish() throws IOException {
+ MetricsSystem metrics = DefaultMetricsSystem.instance();
+
+ metrics.init("test");
+
+ //GIVEN
+ PrometheusMetricsSink sink = new PrometheusMetricsSink();
+
+ metrics.register("prometheus", "prometheus", sink);
+ TestTopMetrics topMetrics = new TestTopMetrics();
+ topMetrics.add("60000");
+ topMetrics.add("1500000");
+ metrics.register(TestTopMetrics.TOPMETRICS_METRICS_SOURCE_NAME,
+ "Top N operations by user", topMetrics);
+
+ metrics.start();
+
+ metrics.publishMetricsNow();
+ ByteArrayOutputStream stream = new ByteArrayOutputStream();
+ OutputStreamWriter writer = new OutputStreamWriter(stream, UTF_8);
+
+ //WHEN
+ sink.writeMetrics(writer);
+ writer.flush();
+
+ //THEN
+ String writtenMetrics = stream.toString(UTF_8.name());
+ System.out.println(writtenMetrics);
+ Assert.assertTrue(
+ "The expected metric line is missing from prometheus metrics output",
+ writtenMetrics.contains(
+
"nn_top_user_op_counts_window_ms_60000_total_count{context=\"dfs\"")
+ );
+ Assert.assertTrue(
+ "The expected metric line is missing from prometheus metrics output",
+ writtenMetrics.contains(
+ "nn_top_user_op_counts_window_ms_60000_count{"));
+
+ Assert.assertTrue(
+ "The expected metric line is missing from prometheus metrics output",
+ writtenMetrics.contains(
+ "nn_top_user_op_counts_window_ms_1500000_count{"));
+
+ Assert.assertTrue(
+ "The expected metric line is missing from prometheus metrics output",
+ writtenMetrics.contains(
+ "op=\"rename\",user=\"hadoop/[email protected]\""));
Review comment:
AssertJ can be used here. The API is more fluent and it can be written
as follows
```
Assertions.assertThat(writtenMetrics).contains("nn_top_user_op_counts_window_ms_60000_total_count{context=\"dfs\"");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]