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.regionserver.metrics;
019
020import org.apache.hadoop.hbase.metrics.MetricRegistry;
021import org.apache.hadoop.hbase.quotas.RpcThrottlingException;
022import org.apache.yetus.audience.InterfaceAudience;
023
024@InterfaceAudience.Private
025public class MetricsThrottleExceptions {
026
027  /**
028   * The name of the metrics
029   */
030  private static final String METRICS_NAME = "ThrottleExceptions";
031
032  /**
033   * The name of the metrics context that metrics will be under.
034   */
035  private static final String METRICS_CONTEXT = "regionserver";
036
037  /**
038   * Description
039   */
040  private static final String METRICS_DESCRIPTION = "Metrics about RPC throttling exceptions";
041
042  /**
043   * The name of the metrics context that metrics will be under in jmx
044   */
045  private static final String METRICS_JMX_CONTEXT = "RegionServer,sub=" + METRICS_NAME;
046
047  private final MetricRegistry registry;
048
049  public MetricsThrottleExceptions(MetricRegistry sharedRegistry) {
050    registry = sharedRegistry;
051  }
052
053  /**
054   * Record a throttle exception with contextual information.
055   * @param throttleType the type of throttle exception
056   * @param user         the user who triggered the throttle
057   * @param table        the table that was being accessed
058   */
059  public void recordThrottleException(RpcThrottlingException.Type throttleType, String user,
060    String table) {
061    String metricName = qualifyThrottleMetric(throttleType, user, table);
062    registry.counter(metricName).increment();
063  }
064
065  private static String qualifyThrottleMetric(RpcThrottlingException.Type throttleType, String user,
066    String table) {
067    return String.format("RpcThrottlingException_Type_%s_User_%s_Table_%s", throttleType.name(),
068      sanitizeMetricName(user), sanitizeMetricName(table));
069  }
070
071  private static String sanitizeMetricName(String name) {
072    if (name == null) {
073      return "unknown";
074    }
075    // Only replace characters that are problematic for JMX ObjectNames
076    // Keep meaningful characters like hyphens, periods, etc.
077    return name.replaceAll("[,=:*?\"\\n]", "_");
078  }
079
080}