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; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.io.ByteArrayOutputStream; 024import java.io.IOException; 025import java.io.OutputStreamWriter; 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.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035 036/** 037 * Test prometheus Sink. 038 */ 039@Tag(SmallTests.TAG) 040@Tag(MiscTests.TAG) 041public class TestPrometheusServlet { 042 043 @Test 044 public void testPublish() throws IOException { 045 // GIVEN 046 MetricsSystem metrics = DefaultMetricsSystem.instance(); 047 metrics.init("test"); 048 TestMetrics testMetrics = metrics.register("TestMetrics", "Testing metrics", new TestMetrics()); 049 metrics.start(); 050 051 testMetrics.numBucketCreateFails.incr(); 052 metrics.publishMetricsNow(); 053 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 054 OutputStreamWriter writer = new OutputStreamWriter(stream, UTF_8); 055 056 // WHEN 057 PrometheusHadoopServlet prom2Servlet = new PrometheusHadoopServlet(); 058 // Test with no description 059 prom2Servlet.writeMetrics(writer, false, null); 060 061 // THEN 062 String writtenMetrics = stream.toString(UTF_8.name()); 063 System.out.println(writtenMetrics); 064 assertTrue(writtenMetrics.contains("test_metrics_num_bucket_create_fails{context=\"dfs\""), 065 "The expected metric line is missing from prometheus metrics output"); 066 067 metrics.stop(); 068 metrics.shutdown(); 069 } 070 071 /** 072 * Example metric pojo. 073 */ 074 @Metrics(about = "Test Metrics", context = "dfs") 075 private static class TestMetrics { 076 077 @Metric 078 private MutableCounterLong numBucketCreateFails; 079 } 080}