001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.http.prometheus; 019 020import static java.nio.charset.StandardCharsets.UTF_8; 021 022import java.io.ByteArrayOutputStream; 023import java.io.IOException; 024import java.io.OutputStreamWriter; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.testclassification.MiscTests; 027import org.apache.hadoop.hbase.testclassification.SmallTests; 028import org.apache.hadoop.metrics2.MetricsSystem; 029import org.apache.hadoop.metrics2.annotation.Metric; 030import org.apache.hadoop.metrics2.annotation.Metrics; 031import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; 032import org.apache.hadoop.metrics2.lib.MutableCounterLong; 033import org.junit.Assert; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037 038/** 039 * Test prometheus Sink. 040 */ 041@Category({ SmallTests.class, MiscTests.class }) 042public class TestPrometheusServlet { 043 044 @ClassRule 045 public static final HBaseClassTestRule CLASS_TEST_RULE = 046 HBaseClassTestRule.forClass(TestPrometheusServlet.class); 047 048 @Test 049 public void testPublish() throws IOException { 050 // GIVEN 051 MetricsSystem metrics = DefaultMetricsSystem.instance(); 052 metrics.init("test"); 053 TestMetrics testMetrics = metrics.register("TestMetrics", "Testing metrics", new TestMetrics()); 054 metrics.start(); 055 056 testMetrics.numBucketCreateFails.incr(); 057 metrics.publishMetricsNow(); 058 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 059 OutputStreamWriter writer = new OutputStreamWriter(stream, UTF_8); 060 061 // WHEN 062 PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet(); 063 // Test with no description 064 prom2Servlet.writeMetrics(writer, false, null); 065 066 // THEN 067 String writtenMetrics = stream.toString(UTF_8.name()); 068 System.out.println(writtenMetrics); 069 Assert.assertTrue("The expected metric line is missing from prometheus metrics output", 070 writtenMetrics.contains("test_metrics_num_bucket_create_fails{context=\"dfs\"")); 071 072 metrics.stop(); 073 metrics.shutdown(); 074 } 075 076 /** 077 * Example metric pojo. 078 */ 079 @Metrics(about = "Test Metrics", context = "dfs") 080 private static class TestMetrics { 081 082 @Metric 083 private MutableCounterLong numBucketCreateFails; 084 } 085}