All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wavefront.agent.JsonMetricsEndpoint Maven / Gradle / Ivy

There is a newer version: 4.36
Show newest version
package com.wavefront.agent;

import com.google.common.collect.Maps;

import com.fasterxml.jackson.databind.JsonNode;
import com.wavefront.common.Clock;
import com.wavefront.metrics.JsonMetricsParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import sunnylabs.report.ReportPoint;

/**
 * Agent-side JSON metrics endpoint.
 *
 * @author Clement Pang ([email protected]).
 */
@Path("/")
public class JsonMetricsEndpoint extends PointHandler {

  @Nullable
  private final String prefix;
  private final String defaultHost;

  public JsonMetricsEndpoint(final int port, final String host,
                             @Nullable
                             final String prefix, final String validationLevel, final int blockedPointsPerBatch,
                             PostPushDataTimedTask[] postPushDataTimedTasks) {
    super(port, validationLevel, blockedPointsPerBatch, postPushDataTimedTasks);
    this.prefix = prefix;
    this.defaultHost = host;
  }

  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response reportMetrics(@QueryParam("h") String host,
                                @QueryParam("p") String prefix,
                                @QueryParam("d") Long timestamp,
                                @Context UriInfo uriInfo,
                                JsonNode metrics) {
    MultivaluedMap queryParams = uriInfo.getQueryParameters(true);
    Map tags = Maps.newHashMap();
    for (Map.Entry> entry : queryParams.entrySet()) {
      String tagk = entry.getKey().trim().toLowerCase();
      if (tagk.equals("h") || tagk.equals("p") || tagk.equals("d") || tagk.equals("t")) {
        continue;
      }
      tags.put(tagk, entry.getValue().get(0));
    }
    List points = new ArrayList<>();
    long when = timestamp == null ? Clock.now() : timestamp;
    if (this.prefix != null) {
      if (prefix == null) prefix = this.prefix;
      else prefix = this.prefix + "." + prefix;
    }
    if (host == null) host = defaultHost;
    JsonMetricsParser.report("dummy", prefix, metrics, points, host, when);
    for (ReportPoint point : points) {
      if (point.getAnnotations() == null) {
        point.setAnnotations(tags);
      } else {
        Map newAnnotations = Maps.newHashMap(tags);
        newAnnotations.putAll(point.getAnnotations());
        point.setAnnotations(newAnnotations);
      }
      reportPoint(point, "json: " + pointToString(point));
    }
    return Response.accepted().build();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy