Java Code Examples for org.apache.hadoop.metrics2.MetricsException

The following examples show how to use org.apache.hadoop.metrics2.MetricsException. These examples are extracted from open source projects. 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 Project: kylin-on-parquet-v2   Source File: QueryMetricsFacade.java    License: 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 Project: hadoop   Source File: GraphiteSink.java    License: 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 3
Source Project: hadoop   Source File: MetricsSourceBuilder.java    License: 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 4
Source Project: hadoop   Source File: MethodMetric.java    License: 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 5
Source Project: hadoop   Source File: MethodMetric.java    License: 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 6
Source Project: hadoop   Source File: MethodMetric.java    License: 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 7
Source Project: hadoop   Source File: MetricsRegistry.java    License: 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 8
Source Project: hadoop   Source File: MetricsRegistry.java    License: 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 Project: hadoop   Source File: MetricsRegistry.java    License: 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 10
Source Project: big-c   Source File: GraphiteSink.java    License: 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 11
Source Project: big-c   Source File: MetricsSourceBuilder.java    License: 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 12
Source Project: big-c   Source File: MethodMetric.java    License: 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 Project: big-c   Source File: MethodMetric.java    License: 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 14
Source Project: big-c   Source File: MethodMetric.java    License: 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 15
Source Project: big-c   Source File: MetricsRegistry.java    License: 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 16
Source Project: big-c   Source File: MetricsRegistry.java    License: 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 17
Source Project: big-c   Source File: MetricsRegistry.java    License: 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 Project: kylin   Source File: QueryMetricsFacade.java    License: 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 19
Source Project: hbase   Source File: DynamicMetricsRegistry.java    License: 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 20
Source Project: hbase   Source File: DynamicMetricsRegistry.java    License: 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 21
Source Project: hbase   Source File: DynamicMetricsRegistry.java    License: 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 22
Source Project: hbase   Source File: DynamicMetricsRegistry.java    License: 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 23
Source Project: hadoop-ozone   Source File: HddsServerUtil.java    License: 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 24
Source Project: hadoop   Source File: GraphiteSink.java    License: 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 25
Source Project: hadoop   Source File: FileSink.java    License: 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 26
Source Project: hadoop   Source File: MutableMetricsFactory.java    License: 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 27
Source Project: hadoop   Source File: TestMetricsRegistry.java    License: 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 28
Source Project: big-c   Source File: GraphiteSink.java    License: 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 29
Source Project: big-c   Source File: FileSink.java    License: 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 30
Source Project: big-c   Source File: MutableMetricsFactory.java    License: 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());
}