2121import java .io .IOException ;
2222import java .io .OutputStreamWriter ;
2323
24+ import java .util .ArrayList ;
25+ import java .util .List ;
26+ import org .apache .commons .lang3 .StringUtils ;
27+ import org .apache .hadoop .metrics2 .MetricsCollector ;
28+ import org .apache .hadoop .metrics2 .MetricsRecordBuilder ;
29+ import org .apache .hadoop .metrics2 .MetricsSource ;
2430import org .apache .hadoop .metrics2 .MetricsSystem ;
2531import org .apache .hadoop .metrics2 .annotation .Metric ;
2632import org .apache .hadoop .metrics2 .annotation .Metrics ;
2733import org .apache .hadoop .metrics2 .annotation .Metric .Type ;
2834import org .apache .hadoop .metrics2 .lib .DefaultMetricsSystem ;
35+ import org .apache .hadoop .metrics2 .lib .Interns ;
2936import org .apache .hadoop .metrics2 .lib .MutableCounterLong ;
3037
3138import org .junit .Assert ;
@@ -219,6 +226,62 @@ public void testNamingWhitespaces() {
219226 sink .prometheusName (recordName , metricName ));
220227 }
221228
229+ /**
230+ * testTopMetricsPublish.
231+ */
232+ @ Test
233+ public void testTopMetricsPublish () throws IOException {
234+ MetricsSystem metrics = DefaultMetricsSystem .instance ();
235+
236+ metrics .init ("test" );
237+
238+ //GIVEN
239+ PrometheusMetricsSink sink = new PrometheusMetricsSink ();
240+
241+ metrics .register ("prometheus" , "prometheus" , sink );
242+ TestTopMetrics topMetrics = new TestTopMetrics ();
243+ topMetrics .add ("60000" );
244+ topMetrics .add ("1500000" );
245+ metrics .register (TestTopMetrics .TOPMETRICS_METRICS_SOURCE_NAME ,
246+ "Top N operations by user" , topMetrics );
247+
248+ metrics .start ();
249+
250+ metrics .publishMetricsNow ();
251+ ByteArrayOutputStream stream = new ByteArrayOutputStream ();
252+ OutputStreamWriter writer = new OutputStreamWriter (stream , UTF_8 );
253+
254+ //WHEN
255+ sink .writeMetrics (writer );
256+ writer .flush ();
257+
258+ //THEN
259+ String writtenMetrics = stream .toString (UTF_8 .name ());
260+ System .out .println (writtenMetrics );
261+ Assert .assertTrue (
262+ "The expected metric line is missing from prometheus metrics output" ,
263+ writtenMetrics .contains (
264+ "nn_top_user_op_counts_window_ms_60000_total_count{context=\" dfs\" " )
265+ );
266+ Assert .assertTrue (
267+ "The expected metric line is missing from prometheus metrics output" ,
268+ writtenMetrics .contains (
269+ "nn_top_user_op_counts_window_ms_60000_count{" ));
270+
271+ Assert .assertTrue (
272+ "The expected metric line is missing from prometheus metrics output" ,
273+ writtenMetrics .contains (
274+ "nn_top_user_op_counts_window_ms_1500000_count{" ));
275+
276+ Assert .assertTrue (
277+ "The expected metric line is missing from prometheus metrics output" ,
278+ writtenMetrics .contains (
279+ "op=\" rename\" ,user=\" hadoop/[email protected] \" " ));
280+
281+ metrics .stop ();
282+ metrics .shutdown ();
283+ }
284+
222285 /**
223286 * Example metric pojo.
224287 */
@@ -242,4 +305,38 @@ String testTag1() {
242305 @ Metric
243306 private MutableCounterLong numBucketCreateFails ;
244307 }
308+
309+ /**
310+ * Example metric TopMetrics.
311+ */
312+ private class TestTopMetrics implements MetricsSource {
313+
314+ public static final String TOPMETRICS_METRICS_SOURCE_NAME =
315+ "NNTopUserOpCounts" ;
316+ private final List <String > windowMsNames = new ArrayList <>();
317+
318+ public void add (String windowMs ) {
319+ windowMsNames .add (String .format (".windowMs=%s" , windowMs ));
320+ }
321+
322+ @ Override
323+ public void getMetrics (MetricsCollector collector , boolean all ) {
324+ for (String windowMs : windowMsNames ) {
325+ MetricsRecordBuilder rb = collector
326+ .addRecord (TOPMETRICS_METRICS_SOURCE_NAME + windowMs )
327+ .setContext ("dfs" );
328+ rb .addCounter (
329+ Interns .info ("op=" + StringUtils .deleteWhitespace ("rename" )
330+ + ".TotalCount" , "Total operation count" ), 2 );
331+ rb .addCounter (
332+ Interns .info ("op=" + StringUtils .deleteWhitespace ("rename" )
333+ +
".user=" +
"hadoop/[email protected] " 334+ + ".count" , "Total operations performed by user" ), 3 );
335+ rb .addCounter (
336+ Interns .info ("op=" + StringUtils .deleteWhitespace ("delete" )
337+ + ".user=" + "test_user2"
338+ + ".count" , "Total operations performed by user" ), 4 );
339+ }
340+ }
341+ }
245342}
0 commit comments