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