001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020
021package org.apache.hadoop.hbase.coprocessor;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.hadoop.hbase.metrics.MetricRegistries;
025import org.apache.hadoop.hbase.metrics.MetricRegistry;
026import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
027
028/**
029 * Utility class for tracking metrics for various types of coprocessors. Each coprocessor instance
030 * creates its own MetricRegistry which is exported as an individual MetricSource. MetricRegistries
031 * are ref counted using the hbase-metric module interfaces.
032 */
033@InterfaceAudience.Private
034public class MetricsCoprocessor {
035
036  // Master coprocessor metrics
037  private static final String MASTER_COPROC_METRICS_NAME = "Coprocessor.Master";
038  private static final String MASTER_COPROC_METRICS_CONTEXT = "master";
039  private static final String MASTER_COPROC_METRICS_DESCRIPTION
040      = "Metrics about HBase MasterObservers";
041  private static final String MASTER_COPROC_METRICS_JMX_CONTEXT
042      = "Master,sub=" + MASTER_COPROC_METRICS_NAME;
043
044  // RegionServer coprocessor metrics
045  private static final String RS_COPROC_METRICS_NAME = "Coprocessor.RegionServer";
046  private static final String RS_COPROC_METRICS_CONTEXT = "regionserver";
047  private static final String RS_COPROC_METRICS_DESCRIPTION
048      = "Metrics about HBase RegionServerObservers";
049  private static final String RS_COPROC_METRICS_JMX_CONTEXT = "RegionServer,sub="
050      + RS_COPROC_METRICS_NAME;
051
052  // Region coprocessor metrics
053  private static final String REGION_COPROC_METRICS_NAME = "Coprocessor.Region";
054  private static final String REGION_COPROC_METRICS_CONTEXT = "regionserver";
055  private static final String REGION_COPROC_METRICS_DESCRIPTION
056      = "Metrics about HBase RegionObservers";
057  private static final String REGION_COPROC_METRICS_JMX_CONTEXT
058      = "RegionServer,sub=" + REGION_COPROC_METRICS_NAME;
059
060  // WAL coprocessor metrics
061  private static final String WAL_COPROC_METRICS_NAME = "Coprocessor.WAL";
062  private static final String WAL_COPROC_METRICS_CONTEXT = "regionserver";
063  private static final String WAL_COPROC_METRICS_DESCRIPTION
064      = "Metrics about HBase WALObservers";
065  private static final String WAL_COPROC_METRICS_JMX_CONTEXT
066      = "RegionServer,sub=" + WAL_COPROC_METRICS_NAME;
067
068  private static String suffix(String metricName, String cpName) {
069    return new StringBuilder(metricName)
070        .append(".")
071        .append("CP_")
072        .append(cpName)
073        .toString();
074  }
075
076  static MetricRegistryInfo createRegistryInfoForMasterCoprocessor(String clazz) {
077    return new MetricRegistryInfo(
078        suffix(MASTER_COPROC_METRICS_NAME, clazz),
079        MASTER_COPROC_METRICS_DESCRIPTION,
080        suffix(MASTER_COPROC_METRICS_JMX_CONTEXT, clazz),
081        MASTER_COPROC_METRICS_CONTEXT, false);
082  }
083
084  public static MetricRegistry createRegistryForMasterCoprocessor(String clazz) {
085    return MetricRegistries.global().create(createRegistryInfoForMasterCoprocessor(clazz));
086  }
087
088  static MetricRegistryInfo createRegistryInfoForRSCoprocessor(String clazz) {
089    return new MetricRegistryInfo(
090        suffix(RS_COPROC_METRICS_NAME, clazz),
091        RS_COPROC_METRICS_DESCRIPTION,
092        suffix(RS_COPROC_METRICS_JMX_CONTEXT, clazz),
093        RS_COPROC_METRICS_CONTEXT, false);
094  }
095
096  public static MetricRegistry createRegistryForRSCoprocessor(String clazz) {
097    return MetricRegistries.global().create(createRegistryInfoForRSCoprocessor(clazz));
098  }
099
100  public static MetricRegistryInfo createRegistryInfoForRegionCoprocessor(String clazz) {
101    return new MetricRegistryInfo(
102        suffix(REGION_COPROC_METRICS_NAME, clazz),
103        REGION_COPROC_METRICS_DESCRIPTION,
104        suffix(REGION_COPROC_METRICS_JMX_CONTEXT, clazz),
105        REGION_COPROC_METRICS_CONTEXT, false);
106  }
107
108  public static MetricRegistry createRegistryForRegionCoprocessor(String clazz) {
109    return MetricRegistries.global().create(createRegistryInfoForRegionCoprocessor(clazz));
110  }
111
112  public static MetricRegistryInfo createRegistryInfoForWALCoprocessor(String clazz) {
113    return new MetricRegistryInfo(
114        suffix(WAL_COPROC_METRICS_NAME, clazz),
115        WAL_COPROC_METRICS_DESCRIPTION,
116        suffix(WAL_COPROC_METRICS_JMX_CONTEXT, clazz),
117        WAL_COPROC_METRICS_CONTEXT, false);
118  }
119
120  public static MetricRegistry createRegistryForWALCoprocessor(String clazz) {
121    return MetricRegistries.global().create(createRegistryInfoForWALCoprocessor(clazz));
122  }
123
124  public static void removeRegistry(MetricRegistry registry) {
125    if (registry == null) {
126      return;
127    }
128    MetricRegistries.global().remove(registry.getMetricRegistryInfo());
129  }
130}