Github user xcangCRM commented on a diff in the pull request: https://github.com/apache/phoenix/pull/315#discussion_r207053000 --- Diff: phoenix-core/src/main/java/org/apache/phoenix/monitoring/GlobalMetricRegistriesAdapter.java --- @@ -0,0 +1,167 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.phoenix.monitoring; + +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.hbase.metrics.Counter; +import org.apache.hadoop.hbase.metrics.Gauge; +import org.apache.hadoop.hbase.metrics.Histogram; +import org.apache.hadoop.hbase.metrics.Meter; +import org.apache.hadoop.hbase.metrics.Metric; +import org.apache.hadoop.hbase.metrics.MetricRegistry; +import org.apache.hadoop.hbase.metrics.MetricRegistryInfo; +import org.apache.hadoop.hbase.metrics.Timer; +import org.apache.hadoop.metrics2.MetricsCollector; +import org.apache.hadoop.metrics2.MetricsInfo; +import org.apache.hadoop.metrics2.MetricsRecordBuilder; +import org.apache.hadoop.metrics2.MetricsSource; +import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem; +import org.apache.hadoop.metrics2.lib.Interns; +import org.apache.hadoop.metrics2.lib.MutableHistogram; +import org.apache.hadoop.metrics2.source.JvmMetrics; + +/** + * Contents mostly copied from GlobalMetricRegistriesAdapter class from hbase-hadoop2-compat + * The adapter attaches HBase's MetricRegistry to Hadoop's DefaultMetricsSystem + * Doesn't handle dynamic attach/detach of registries + */ +public class GlobalMetricRegistriesAdapter { + + private static final Log LOG = LogFactory.getLog(GlobalMetricRegistriesAdapter.class); + private static GlobalMetricRegistriesAdapter INSTANCE; + + private GlobalMetricRegistriesAdapter() { + DefaultMetricsSystem.initialize("Phoenix"); + JvmMetrics.initSingleton("Phoenix", ""); + } + + public static GlobalMetricRegistriesAdapter getInstance() { + if (INSTANCE == null) { + INSTANCE = new GlobalMetricRegistriesAdapter(); + } + return INSTANCE; + } + + public void registerMetricRegistry(MetricRegistry registry) { + if (registry == null) { + LOG.warn("Registry cannot be registered with Hadoop Metrics 2 since it is null."); + return; + } + + HBaseMetrics2HadoopMetricsAdapter adapter = new HBaseMetrics2HadoopMetricsAdapter(registry); + registerToDefaultMetricsSystem(registry, adapter); + } + + private void registerToDefaultMetricsSystem(MetricRegistry registry, HBaseMetrics2HadoopMetricsAdapter adapter) { + MetricRegistryInfo info = registry.getMetricRegistryInfo(); + LOG.info("Registering " + info.getMetricsJmxContext() + " " + info.getMetricsDescription() + " into DefaultMetricsSystem"); + DefaultMetricsSystem.instance().register(info.getMetricsJmxContext(), info.getMetricsDescription(), adapter); + } + + /** + * Class to convert HBase Metric Objects to Hadoop Metrics2 Metric Objects + */ + private static class HBaseMetrics2HadoopMetricsAdapter implements MetricsSource { + private static final Log LOG = LogFactory.getLog(HBaseMetrics2HadoopMetricsAdapter.class); + private final MetricRegistry registry; + private HBaseMetrics2HadoopMetricsAdapter(MetricRegistry registry) { + this.registry = registry; + } + + public void snapshotAllMetrics(MetricRegistry metricRegistry, MetricsCollector collector) { + MetricRegistryInfo info = metricRegistry.getMetricRegistryInfo(); + MetricsRecordBuilder builder = collector.addRecord(Interns.info(info.getMetricsName(), info.getMetricsDescription())); + builder.setContext(info.getMetricsContext()); + this.snapshotAllMetrics(metricRegistry, builder); + } + + public void snapshotAllMetrics(MetricRegistry metricRegistry, MetricsRecordBuilder builder) { + Map<String, Metric> metrics = metricRegistry.getMetrics(); + Iterator i$ = metrics.entrySet().iterator(); --- End diff -- why do you need this? i$
---