org.apache.hadoop.metrics2.MetricsException Java Examples

The following examples show how to use org.apache.hadoop.metrics2.MetricsException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QueryMetricsFacade.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private static QueryMetrics getQueryMetrics(String name) {
    KylinConfig config = KylinConfig.getInstanceFromEnv();
    int[] intervals = config.getQueryMetricsPercentilesIntervals();

    QueryMetrics queryMetrics = metricsMap.get(name);
    if (queryMetrics != null) {
        return queryMetrics;
    }

    synchronized (QueryMetricsFacade.class) {
        queryMetrics = metricsMap.get(name);
        if (queryMetrics != null) {
            return queryMetrics;
        }

        try {
            queryMetrics = new QueryMetrics(intervals).registerWith(name);
            metricsMap.put(name, queryMetrics);
            return queryMetrics;
        } catch (MetricsException e) {
            logger.warn(name + " register error: ", e);
        }
    }
    return queryMetrics;
}
 
Example #2
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 6 votes vote down vote up
public MutableHistogram getHistogram(String histoName) {
  //See getGauge for description on how this works.
  MutableMetric histo = metricsMap.get(histoName);
  if (histo == null) {
    MutableHistogram newCounter =
        new MutableHistogram(new MetricsInfoImpl(histoName, ""));
    histo = metricsMap.putIfAbsent(histoName, newCounter);
    if (histo == null) {
      return newCounter;
    }
  }


  if (!(histo instanceof MutableHistogram)) {
    throw new MetricsException("Metric already exists in registry for metric name: " +
        histoName + " and not of type MutableHistogram");
  }

  return (MutableHistogram) histo;
}
 
Example #3
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Get a MetricMutableCounterLong from the storage.  If it is not there atomically put it.
 *
 * @param counterName            Name of the counter to get
 * @param potentialStartingValue starting value if we have to create a new counter
 */
public MutableFastCounter getCounter(String counterName, long potentialStartingValue) {
  //See getGauge for description on how this works.
  MutableMetric counter = metricsMap.get(counterName);
  if (counter == null) {
    MutableFastCounter newCounter =
            new MutableFastCounter(new MetricsInfoImpl(counterName, ""), potentialStartingValue);
    counter = metricsMap.putIfAbsent(counterName, newCounter);
    if (counter == null) {
      return newCounter;
    }
  }


  if (!(counter instanceof MutableCounter)) {
    throw new MetricsException("Metric already exists in registry for metric name: " +
            counterName + " and not of type MutableCounter");
  }

  return (MutableFastCounter) counter;
}
 
Example #4
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Add sample to a stat metric by name.
 * @param name  of the metric
 * @param value of the snapshot to add
 */
public void add(String name, long value) {
  MutableMetric m = metricsMap.get(name);

  if (m != null) {
    if (m instanceof MutableStat) {
      ((MutableStat) m).add(value);
    }
    else {
      throw new MetricsException("Unsupported add(value) for metric "+ name);
    }
  }
  else {
    metricsMap.put(name, newRate(name)); // default is a rate metric
    add(name, value);
  }
}
 
Example #5
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 6 votes vote down vote up
@InterfaceAudience.Private
public MutableRate newRate(String name, String desc,
    boolean extended, boolean returnExisting) {
  if (returnExisting) {
    MutableMetric rate = metricsMap.get(name);
    if (rate != null) {
      if (rate instanceof MutableRate) {
        return (MutableRate) rate;
      }

      throw new MetricsException("Unexpected metrics type "+ rate.getClass()
                                 +" for "+ name);
    }
  }
  MutableRate ret = new MutableRate(name, desc, extended);
  return addNewMetricIfAbsent(name, ret, MutableRate.class);
}
 
Example #6
Source File: QueryMetricsFacade.java    From kylin with Apache License 2.0 6 votes vote down vote up
private static QueryMetrics getQueryMetrics(String name) {
    KylinConfig config = KylinConfig.getInstanceFromEnv();
    int[] intervals = config.getQueryMetricsPercentilesIntervals();

    QueryMetrics queryMetrics = metricsMap.get(name);
    if (queryMetrics != null) {
        return queryMetrics;
    }

    synchronized (QueryMetricsFacade.class) {
        queryMetrics = metricsMap.get(name);
        if (queryMetrics != null) {
            return queryMetrics;
        }

        try {
            queryMetrics = new QueryMetrics(intervals).registerWith(name);
            metricsMap.put(name, queryMetrics);
            return queryMetrics;
        } catch (MetricsException e) {
            logger.warn(name + " register error: ", e);
        }
    }
    return queryMetrics;
}
 
Example #7
Source File: MetricsRegistry.java    From big-c with Apache License 2.0 6 votes vote down vote up
private void checkMetricName(String name) {
  // Check for invalid characters in metric name
  boolean foundWhitespace = false;
  for (int i = 0; i < name.length(); i++) {
    char c = name.charAt(i);
    if (Character.isWhitespace(c)) {
      foundWhitespace = true;
      break;
    }
  }
  if (foundWhitespace) {
    throw new MetricsException("Metric name '"+ name +
        "' contains illegal whitespace character");
  }
  // Check if name has already been registered
  if (metricsMap.containsKey(name)) {
    throw new MetricsException("Metric name "+ name +" already exists!");
  }
}
 
Example #8
Source File: MetricsRegistry.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Add sample to a stat metric by name.
 * @param name  of the metric
 * @param value of the snapshot to add
 */
public synchronized void add(String name, long value) {
  MutableMetric m = metricsMap.get(name);

  if (m != null) {
    if (m instanceof MutableStat) {
      ((MutableStat) m).add(value);
    }
    else {
      throw new MetricsException("Unsupported add(value) for metric "+ name);
    }
  }
  else {
    metricsMap.put(name, newRate(name)); // default is a rate metric
    add(name, value);
  }
}
 
Example #9
Source File: MetricsRegistry.java    From big-c with Apache License 2.0 6 votes vote down vote up
@InterfaceAudience.Private
public synchronized MutableRate newRate(String name, String desc,
    boolean extended, boolean returnExisting) {
  if (returnExisting) {
    MutableMetric rate = metricsMap.get(name);
    if (rate != null) {
      if (rate instanceof MutableRate) return (MutableRate) rate;
      throw new MetricsException("Unexpected metrics type "+ rate.getClass()
                                 +" for "+ name);
    }
  }
  checkMetricName(name);
  MutableRate ret = new MutableRate(name, desc, extended);
  metricsMap.put(name, ret);
  return ret;
}
 
Example #10
Source File: MethodMetric.java    From big-c with Apache License 2.0 6 votes vote down vote up
MutableMetric newTag(Class<?> resType) {
  if (resType == String.class) {
    return new MutableMetric() {
      @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {
        try {
          Object ret = method.invoke(obj, (Object[]) null);
          rb.tag(info, (String) ret);
        }
        catch (Exception ex) {
          LOG.error("Error invoking method "+ method.getName(), ex);
        }
      }
    };
  }
  throw new MetricsException("Unsupported tag type: "+ resType.getName());
}
 
Example #11
Source File: MethodMetric.java    From big-c with Apache License 2.0 6 votes vote down vote up
MutableMetric newGauge(final Class<?> t) {
  if (isInt(t) || isLong(t) || isFloat(t) || isDouble(t)) {
    return new MutableMetric() {
      @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {
        try {
          Object ret = method.invoke(obj, (Object[]) null);
          if (isInt(t)) rb.addGauge(info, ((Integer) ret).intValue());
          else if (isLong(t)) rb.addGauge(info, ((Long) ret).longValue());
          else if (isFloat(t)) rb.addGauge(info, ((Float) ret).floatValue());
          else rb.addGauge(info, ((Double) ret).doubleValue());
        }
        catch (Exception ex) {
          LOG.error("Error invoking method "+ method.getName(), ex);
        }
      }
    };
  }
  throw new MetricsException("Unsupported gauge type: "+ t.getName());
}
 
Example #12
Source File: MethodMetric.java    From big-c with Apache License 2.0 6 votes vote down vote up
MutableMetric newCounter(final Class<?> type) {
  if (isInt(type) || isLong(type)) {
    return new MutableMetric() {
      @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {
        try {
          Object ret = method.invoke(obj, (Object[])null);
          if (isInt(type)) rb.addCounter(info, ((Integer) ret).intValue());
          else rb.addCounter(info, ((Long) ret).longValue());
        }
        catch (Exception ex) {
          LOG.error("Error invoking method "+ method.getName(), ex);
        }
      }
    };
  }
  throw new MetricsException("Unsupported counter type: "+ type.getName());
}
 
Example #13
Source File: MetricsSourceBuilder.java    From big-c with Apache License 2.0 6 votes vote down vote up
public MetricsSource build() {
  if (source instanceof MetricsSource) {
    if (hasAtMetric && !hasRegistry) {
      throw new MetricsException("Hybrid metrics: registry required.");
    }
    return (MetricsSource) source;
  }
  else if (!hasAtMetric) {
    throw new MetricsException("No valid @Metric annotation found.");
  }
  return new MetricsSource() {
    @Override
    public void getMetrics(MetricsCollector builder, boolean all) {
      registry.snapshot(builder.addRecord(registry.info()), all);
    }
  };
}
 
Example #14
Source File: GraphiteSink.java    From big-c with Apache License 2.0 6 votes vote down vote up
public void connect() {
  if (isConnected()) {
    throw new MetricsException("Already connected to Graphite");
  }
  if (tooManyConnectionFailures()) {
    // return silently (there was ERROR in logs when we reached limit for the first time)
    return;
  }
  try {
    // Open a connection to Graphite server.
    socket = new Socket(serverHost, serverPort);
    writer = new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8);
  } catch (Exception e) {
    connectionFailures++;
    if (tooManyConnectionFailures()) {
      // first time when connection limit reached, report to logs
      LOG.error("Too many connection failures, would not try to connect again.");
    }
    throw new MetricsException("Error creating connection, "
        + serverHost + ":" + serverPort, e);
  }
}
 
Example #15
Source File: GraphiteSink.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public void connect() {
  if (isConnected()) {
    throw new MetricsException("Already connected to Graphite");
  }
  if (tooManyConnectionFailures()) {
    // return silently (there was ERROR in logs when we reached limit for the first time)
    return;
  }
  try {
    // Open a connection to Graphite server.
    socket = new Socket(serverHost, serverPort);
    writer = new OutputStreamWriter(socket.getOutputStream(), Charsets.UTF_8);
  } catch (Exception e) {
    connectionFailures++;
    if (tooManyConnectionFailures()) {
      // first time when connection limit reached, report to logs
      LOG.error("Too many connection failures, would not try to connect again.");
    }
    throw new MetricsException("Error creating connection, "
        + serverHost + ":" + serverPort, e);
  }
}
 
Example #16
Source File: MetricsSourceBuilder.java    From hadoop with Apache License 2.0 6 votes vote down vote up
public MetricsSource build() {
  if (source instanceof MetricsSource) {
    if (hasAtMetric && !hasRegistry) {
      throw new MetricsException("Hybrid metrics: registry required.");
    }
    return (MetricsSource) source;
  }
  else if (!hasAtMetric) {
    throw new MetricsException("No valid @Metric annotation found.");
  }
  return new MetricsSource() {
    @Override
    public void getMetrics(MetricsCollector builder, boolean all) {
      registry.snapshot(builder.addRecord(registry.info()), all);
    }
  };
}
 
Example #17
Source File: MetricsRegistry.java    From hadoop with Apache License 2.0 6 votes vote down vote up
private void checkMetricName(String name) {
  // Check for invalid characters in metric name
  boolean foundWhitespace = false;
  for (int i = 0; i < name.length(); i++) {
    char c = name.charAt(i);
    if (Character.isWhitespace(c)) {
      foundWhitespace = true;
      break;
    }
  }
  if (foundWhitespace) {
    throw new MetricsException("Metric name '"+ name +
        "' contains illegal whitespace character");
  }
  // Check if name has already been registered
  if (metricsMap.containsKey(name)) {
    throw new MetricsException("Metric name "+ name +" already exists!");
  }
}
 
Example #18
Source File: MethodMetric.java    From hadoop with Apache License 2.0 6 votes vote down vote up
MutableMetric newCounter(final Class<?> type) {
  if (isInt(type) || isLong(type)) {
    return new MutableMetric() {
      @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {
        try {
          Object ret = method.invoke(obj, (Object[])null);
          if (isInt(type)) rb.addCounter(info, ((Integer) ret).intValue());
          else rb.addCounter(info, ((Long) ret).longValue());
        }
        catch (Exception ex) {
          LOG.error("Error invoking method "+ method.getName(), ex);
        }
      }
    };
  }
  throw new MetricsException("Unsupported counter type: "+ type.getName());
}
 
Example #19
Source File: MetricsRegistry.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Add sample to a stat metric by name.
 * @param name  of the metric
 * @param value of the snapshot to add
 */
public synchronized void add(String name, long value) {
  MutableMetric m = metricsMap.get(name);

  if (m != null) {
    if (m instanceof MutableStat) {
      ((MutableStat) m).add(value);
    }
    else {
      throw new MetricsException("Unsupported add(value) for metric "+ name);
    }
  }
  else {
    metricsMap.put(name, newRate(name)); // default is a rate metric
    add(name, value);
  }
}
 
Example #20
Source File: MethodMetric.java    From hadoop with Apache License 2.0 6 votes vote down vote up
MutableMetric newGauge(final Class<?> t) {
  if (isInt(t) || isLong(t) || isFloat(t) || isDouble(t)) {
    return new MutableMetric() {
      @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {
        try {
          Object ret = method.invoke(obj, (Object[]) null);
          if (isInt(t)) rb.addGauge(info, ((Integer) ret).intValue());
          else if (isLong(t)) rb.addGauge(info, ((Long) ret).longValue());
          else if (isFloat(t)) rb.addGauge(info, ((Float) ret).floatValue());
          else rb.addGauge(info, ((Double) ret).doubleValue());
        }
        catch (Exception ex) {
          LOG.error("Error invoking method "+ method.getName(), ex);
        }
      }
    };
  }
  throw new MetricsException("Unsupported gauge type: "+ t.getName());
}
 
Example #21
Source File: MetricsRegistry.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@InterfaceAudience.Private
public synchronized MutableRate newRate(String name, String desc,
    boolean extended, boolean returnExisting) {
  if (returnExisting) {
    MutableMetric rate = metricsMap.get(name);
    if (rate != null) {
      if (rate instanceof MutableRate) return (MutableRate) rate;
      throw new MetricsException("Unexpected metrics type "+ rate.getClass()
                                 +" for "+ name);
    }
  }
  checkMetricName(name);
  MutableRate ret = new MutableRate(name, desc, extended);
  metricsMap.put(name, ret);
  return ret;
}
 
Example #22
Source File: MethodMetric.java    From hadoop with Apache License 2.0 6 votes vote down vote up
MutableMetric newTag(Class<?> resType) {
  if (resType == String.class) {
    return new MutableMetric() {
      @Override public void snapshot(MetricsRecordBuilder rb, boolean all) {
        try {
          Object ret = method.invoke(obj, (Object[]) null);
          rb.tag(info, (String) ret);
        }
        catch (Exception ex) {
          LOG.error("Error invoking method "+ method.getName(), ex);
        }
      }
    };
  }
  throw new MetricsException("Unsupported tag type: "+ resType.getName());
}
 
Example #23
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private<T> T returnExistingWithCast(MutableMetric metric,
                                    Class<T> metricClass, String name) {
  if (!metricClass.isAssignableFrom(metric.getClass())) {
    throw new MetricsException("Metric already exists in registry for metric name: " +
            name + " and not of type " + metricClass +
            " but instead of type " + metric.getClass());
  }

  return (T) metric;
}
 
Example #24
Source File: HddsServerUtil.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize hadoop metrics system for Ozone servers.
 * @param configuration OzoneConfiguration to use.
 * @param serverName    The logical name of the server components.
 */
public static MetricsSystem initializeMetrics(
    OzoneConfiguration configuration, String serverName) {
  MetricsSystem metricsSystem = DefaultMetricsSystem.initialize(serverName);
  try {
    JvmMetrics.create(serverName,
        configuration.get(DFSConfigKeysLegacy.DFS_METRICS_SESSION_ID_KEY),
        DefaultMetricsSystem.instance());
  } catch (MetricsException e) {
    LOG.info("Metrics source JvmMetrics already added to DataNode.");
  }
  return metricsSystem;
}
 
Example #25
Source File: DynamicMetricsRegistry.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Get a MetricMutableGaugeLong from the storage.  If it is not there atomically put it.
 *
 * @param gaugeName              name of the gauge to create or get.
 * @param potentialStartingValue value of the new gauge if we have to create it.
 */
public MutableGaugeLong getGauge(String gaugeName, long potentialStartingValue) {
  //Try and get the guage.
  MutableMetric metric = metricsMap.get(gaugeName);

  //If it's not there then try and put a new one in the storage.
  if (metric == null) {

    //Create the potential new gauge.
    MutableGaugeLong newGauge = new MutableGaugeLong(new MetricsInfoImpl(gaugeName, ""),
            potentialStartingValue);

    // Try and put the gauge in.  This is atomic.
    metric = metricsMap.putIfAbsent(gaugeName, newGauge);

    //If the value we get back is null then the put was successful and we will return that.
    //otherwise gaugeLong should contain the thing that was in before the put could be completed.
    if (metric == null) {
      return newGauge;
    }
  }

  if (!(metric instanceof MutableGaugeLong)) {
    throw new MetricsException("Metric already exists in registry for metric name: " + gaugeName +
            " and not of type MetricMutableGaugeLong");
  }

  return (MutableGaugeLong) metric;
}
 
Example #26
Source File: GraphiteSink.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void flush() {
  try {
    graphite.flush();
  } catch (Exception e) {
    LOG.warn("Error flushing metrics to Graphite", e);
    try {
      graphite.close();
    } catch (Exception e1) {
      throw new MetricsException("Error closing connection to Graphite", e1);
    }
  }
}
 
Example #27
Source File: FileSink.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
public void init(SubsetConfiguration conf) {
  String filename = conf.getString(FILENAME_KEY);
  try {
    writer = filename == null ? System.out
        : new PrintStream(new FileOutputStream(new File(filename)),
                          true, "UTF-8");
  } catch (Exception e) {
    throw new MetricsException("Error creating "+ filename, e);
  }
}
 
Example #28
Source File: TestMetricsRegistry.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Ignore
private void expectMetricsException(String prefix, Runnable fun) {
  try {
    fun.run();
  }
  catch (MetricsException e) {
    assertTrue("expected exception", e.getMessage().startsWith(prefix));
    return;
  }
  fail("should've thrown '"+ prefix +"...'");
}
 
Example #29
Source File: MutableMetricsFactory.java    From hadoop with Apache License 2.0 5 votes vote down vote up
MutableMetric newForField(Field field, Metric annotation,
                          MetricsRegistry registry) {
  if (LOG.isDebugEnabled()) {
    LOG.debug("field "+ field +" with annotation "+ annotation);
  }
  MetricsInfo info = getInfo(annotation, field);
  MutableMetric metric = newForField(field, annotation);
  if (metric != null) {
    registry.add(info.name(), metric);
    return metric;
  }
  final Class<?> cls = field.getType();
  if (cls == MutableCounterInt.class) {
    return registry.newCounter(info, 0);
  }
  if (cls == MutableCounterLong.class) {
    return registry.newCounter(info, 0L);
  }
  if (cls == MutableGaugeInt.class) {
    return registry.newGauge(info, 0);
  }
  if (cls == MutableGaugeLong.class) {
    return registry.newGauge(info, 0L);
  }
  if (cls == MutableRate.class) {
    return registry.newRate(info.name(), info.description(),
                            annotation.always());
  }
  if (cls == MutableRates.class) {
    return new MutableRates(registry);
  }
  if (cls == MutableStat.class) {
    return registry.newStat(info.name(), info.description(),
                            annotation.sampleName(), annotation.valueName(),
                            annotation.always());
  }
  throw new MetricsException("Unsupported metric field "+ field.getName() +
                             " of type "+ field.getType().getName());
}
 
Example #30
Source File: FileSink.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
public void init(SubsetConfiguration conf) {
  String filename = conf.getString(FILENAME_KEY);
  try {
    writer = filename == null ? System.out
        : new PrintStream(new FileOutputStream(new File(filename)),
                          true, "UTF-8");
  } catch (Exception e) {
    throw new MetricsException("Error creating "+ filename, e);
  }
}