コード例 #1
0
 @Override
 protected void publish(Iterator<MetricValues> metrics) throws Exception {
   while (metrics.hasNext()) {
     MetricValues metric = metrics.next();
     metricStore.add(metric);
   }
 }
コード例 #2
0
  @Override
  public void onReceived(Iterator<FetchedMessage> messages) {
    // Decode the metrics records.
    final ByteBufferInputStream is = new ByteBufferInputStream(null);
    List<MetricValues> records = Lists.newArrayList();

    while (messages.hasNext()) {
      FetchedMessage input = messages.next();
      try {
        MetricValues metricValues =
            recordReader.read(new BinaryDecoder(is.reset(input.getPayload())), recordSchema);
        records.add(metricValues);
      } catch (IOException e) {
        LOG.info("Failed to decode message to MetricValue. Skipped. {}", e.getMessage());
      }
    }

    if (records.isEmpty()) {
      LOG.info("No records to process.");
      return;
    }

    try {
      addProcessingStats(records);
      metricStore.add(records);
    } catch (Exception e) {
      String msg = "Failed to add metrics data to a store";
      LOG.error(msg);
      // todo: will it shut down the whole the metrics processor service??
      throw new RuntimeException(msg, e);
    }

    recordProcessed += records.size();
    if (recordProcessed % 1000 == 0) {
      LOG.info("{} metrics records processed", recordProcessed);
      LOG.info("Last record time: {}", records.get(records.size() - 1).getTimestamp());
    }
  }