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