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.coprocessor;
019
020import org.apache.hadoop.hbase.metrics.MetricRegistries;
021import org.apache.hadoop.hbase.metrics.MetricRegistry;
022import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
023import org.apache.yetus.audience.InterfaceAudience;
024
025/**
026 * Utility class for tracking metrics for various types of coprocessors. Each coprocessor instance
027 * creates its own MetricRegistry which is exported as an individual MetricSource. MetricRegistries
028 * are ref counted using the hbase-metric module interfaces.
029 */
030@InterfaceAudience.Private
031public class MetricsCoprocessor {
032
033  // Master coprocessor metrics
034  private static final String MASTER_COPROC_METRICS_NAME = "Coprocessor.Master";
035  private static final String MASTER_COPROC_METRICS_CONTEXT = "master";
036  private static final String MASTER_COPROC_METRICS_DESCRIPTION =
037    "Metrics about HBase MasterObservers";
038  private static final String MASTER_COPROC_METRICS_JMX_CONTEXT =
039    "Master,sub=" + MASTER_COPROC_METRICS_NAME;
040
041  // RegionServer coprocessor metrics
042  private static final String RS_COPROC_METRICS_NAME = "Coprocessor.RegionServer";
043  private static final String RS_COPROC_METRICS_CONTEXT = "regionserver";
044  private static final String RS_COPROC_METRICS_DESCRIPTION =
045    "Metrics about HBase RegionServerObservers";
046  private static final String RS_COPROC_METRICS_JMX_CONTEXT =
047    "RegionServer,sub=" + RS_COPROC_METRICS_NAME;
048
049  // Region coprocessor metrics
050  private static final String REGION_COPROC_METRICS_NAME = "Coprocessor.Region";
051  private static final String REGION_COPROC_METRICS_CONTEXT = "regionserver";
052  private static final String REGION_COPROC_METRICS_DESCRIPTION =
053    "Metrics about HBase RegionObservers";
054  private static final String REGION_COPROC_METRICS_JMX_CONTEXT =
055    "RegionServer,sub=" + REGION_COPROC_METRICS_NAME;
056
057  // WAL coprocessor metrics
058  private static final String WAL_COPROC_METRICS_NAME = "Coprocessor.WAL";
059  private static final String WAL_COPROC_METRICS_CONTEXT = "regionserver";
060  private static final String WAL_COPROC_METRICS_DESCRIPTION = "Metrics about HBase WALObservers";
061  private static final String WAL_COPROC_METRICS_JMX_CONTEXT =
062    "RegionServer,sub=" + WAL_COPROC_METRICS_NAME;
063
064  private static String suffix(String metricName, String cpName) {
065    return new StringBuilder(metricName).append(".").append("CP_").append(cpName).toString();
066  }
067
068  static MetricRegistryInfo createRegistryInfoForMasterCoprocessor(String clazz) {
069    return new MetricRegistryInfo(suffix(MASTER_COPROC_METRICS_NAME, clazz),
070      MASTER_COPROC_METRICS_DESCRIPTION, suffix(MASTER_COPROC_METRICS_JMX_CONTEXT, clazz),
071      MASTER_COPROC_METRICS_CONTEXT, false);
072  }
073
074  public static MetricRegistry createRegistryForMasterCoprocessor(String clazz) {
075    return MetricRegistries.global().create(createRegistryInfoForMasterCoprocessor(clazz));
076  }
077
078  static MetricRegistryInfo createRegistryInfoForRSCoprocessor(String clazz) {
079    return new MetricRegistryInfo(suffix(RS_COPROC_METRICS_NAME, clazz),
080      RS_COPROC_METRICS_DESCRIPTION, suffix(RS_COPROC_METRICS_JMX_CONTEXT, clazz),
081      RS_COPROC_METRICS_CONTEXT, false);
082  }
083
084  public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
085    return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
086  }
087
088  public static MetricRegistryInfo createRegistryInfoForRegionCoprocessor(String clazz) {
089    return new MetricRegistryInfo(suffix(REGION_COPROC_METRICS_NAME, clazz),
090      REGION_COPROC_METRICS_DESCRIPTION, suffix(REGION_COPROC_METRICS_JMX_CONTEXT, clazz),
091      REGION_COPROC_METRICS_CONTEXT, false);
092  }
093
094  public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
095    return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
096  }
097
098  public static MetricRegistryInfo createRegistryInfoForWALCoprocessor(String clazz) {
099    return new MetricRegistryInfo(suffix(WAL_COPROC_METRICS_NAME, clazz),
100      WAL_COPROC_METRICS_DESCRIPTION, suffix(WAL_COPROC_METRICS_JMX_CONTEXT, clazz),
101      WAL_COPROC_METRICS_CONTEXT, false);
102  }
103
104  public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
105    return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
106  }
107
108  public static void removeRegistry(MetricRegistry registry) {
109    if (registry == null) {
110      return;
111    }
112    MetricRegistries.global().remove(registry.getMetricRegistryInfo());
113  }
114}