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.metrics; 019 020import static org.junit.jupiter.api.Assertions.assertTrue; 021 022import java.util.Collection; 023import org.apache.hadoop.hbase.testclassification.MetricsTests; 024import org.apache.hadoop.hbase.testclassification.SmallTests; 025import org.apache.hadoop.metrics2.AbstractMetric; 026import org.apache.hadoop.metrics2.MetricsRecord; 027import org.apache.hadoop.metrics2.impl.MetricsExportHelper; 028import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; 029import org.junit.jupiter.api.Tag; 030import org.junit.jupiter.api.Test; 031 032@Tag(MetricsTests.TAG) 033@Tag(SmallTests.TAG) 034public class TestMetricsExportHelper { 035 036 @Test 037 public void testExportHelper() { 038 DefaultMetricsSystem.initialize("exportHelperTestSystem"); 039 DefaultMetricsSystem.instance().start(); 040 041 String metricsName = "exportMetricsTestGrp"; 042 String gaugeName = "exportMetricsTestGauge"; 043 String counterName = "exportMetricsTestCounter"; 044 045 BaseSourceImpl baseSource = new BaseSourceImpl(metricsName, "", metricsName, metricsName); 046 047 baseSource.setGauge(gaugeName, 0); 048 baseSource.incCounters(counterName, 1); 049 050 Collection<MetricsRecord> metrics = MetricsExportHelper.export(); 051 DefaultMetricsSystem.instance().stop(); 052 053 assertTrue(metrics.stream().anyMatch(mr -> mr.name().equals(metricsName))); 054 assertTrue(contains(metrics, metricsName, gaugeName), gaugeName + " is missing in the export"); 055 assertTrue(contains(metrics, metricsName, counterName), 056 counterName + " is missing in the export"); 057 } 058 059 private boolean contains(Collection<MetricsRecord> metrics, String metricsName, 060 String metricName) { 061 return metrics.stream().filter(mr -> mr.name().equals(metricsName)).anyMatch(mr -> { 062 for (AbstractMetric metric : mr.metrics()) { 063 if (metric.name().equals(metricName)) { 064 return true; 065 } 066 } 067 return false; 068 }); 069 } 070}