@Override
 protected void publish(Iterator<MetricValues> metrics) throws Exception {
   while (metrics.hasNext()) {
     MetricValues metric = metrics.next();
     metricStore.add(metric);
   }
 }
Example #2
0
  private MetricQueryResult executeQuery(MetricQueryRequest queryRequest) throws Exception {
    if (queryRequest.getMetrics().size() == 0) {
      throw new IllegalArgumentException("Missing metrics parameter in the query");
    }

    Map<String, String> tagsSliceBy = humanToTagNames(transformTagMap(queryRequest.getTags()));

    MetricQueryRequest.TimeRange timeRange = queryRequest.getTimeRange();

    MetricDataQuery query =
        new MetricDataQuery(
            timeRange.getStart(),
            timeRange.getEnd(),
            timeRange.getResolutionInSeconds(),
            timeRange.getCount(),
            toMetrics(queryRequest.getMetrics()),
            tagsSliceBy,
            transformGroupByTags(queryRequest.getGroupBy()),
            timeRange.getInterpolate());
    Collection<MetricTimeSeries> queryResult = metricStore.query(query);

    long endTime = timeRange.getEnd();
    if (timeRange.getResolutionInSeconds() == Integer.MAX_VALUE && endTime == 0) {
      // for aggregate query, we set the end time to be query time (current time)
      endTime = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
    }

    return decorate(queryResult, timeRange.getStart(), endTime, timeRange.getResolutionInSeconds());
  }
Example #3
0
 private Collection<String> getMetrics(List<MetricTagValue> tagValues) throws Exception {
   // we want to search the entire range, so startTimestamp is '0' and end Timestamp is
   // Integer.MAX_VALUE and
   // limit is -1 , to include the entire search result.
   MetricSearchQuery searchQuery =
       new MetricSearchQuery(0, Integer.MAX_VALUE, -1, toTagValues(tagValues));
   Collection<String> metricNames = metricStore.findMetricNames(searchQuery);
   return Lists.newArrayList(Iterables.filter(metricNames, Predicates.notNull()));
 }
Example #4
0
 private void searchTagAndRespond(HttpResponder responder, List<String> tags) {
   try {
     // we want to search the entire range, so startTimestamp is '0' and end Timestamp is
     // Integer.MAX_VALUE and
     // limit is -1 , to include the entire search result.
     MetricSearchQuery searchQuery =
         new MetricSearchQuery(
             0, Integer.MAX_VALUE, -1, toTagValues(humanToTagNames(parseTagValues(tags))));
     Collection<TagValue> nextTags = metricStore.findNextAvailableTags(searchQuery);
     responder.sendJson(HttpResponseStatus.OK, tagValuesToHuman(nextTags));
   } catch (IllegalArgumentException e) {
     LOG.warn("Invalid request", e);
     responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
   } catch (Exception e) {
     LOG.error("Exception querying metrics ", e);
     responder.sendString(
         HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
   }
 }
Example #5
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());
    }
  }
 @Inject
 public LocalMetricsCollectionService(CConfiguration cConf, MetricStore metricStore) {
   this.cConf = cConf;
   this.metricStore = metricStore;
   metricStore.setMetricsContext(this.getContext(METRICS_PROCESSOR_CONTEXT));
 }