Search in sources :

Example 1 with ReportPoint

use of wavefront.report.ReportPoint in project java by wavefrontHQ.

the class ChannelByteArrayHandler method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, byte[] msg) throws Exception {
    // ignore empty lines.
    if (msg == null || msg.length == 0) {
        return;
    }
    List<ReportPoint> points = Lists.newArrayListWithExpectedSize(1);
    try {
        decoder.decodeReportPoints(msg, points, "dummy");
        for (ReportPoint point : points) {
            if (preprocessor != null && preprocessor.forPointLine().hasTransformers()) {
                String pointLine = PointHandlerImpl.pointToString(point);
                pointLine = preprocessor.forPointLine().transform(pointLine);
                List<ReportPoint> parsedPoints = Lists.newArrayListWithExpectedSize(1);
                recoder.decodeReportPoints(pointLine, parsedPoints, "dummy");
                parsedPoints.forEach(this::preprocessAndReportPoint);
            } else {
                preprocessAndReportPoint(point);
            }
        }
    } catch (final Exception e) {
        final Throwable rootCause = Throwables.getRootCause(e);
        String errMsg = "WF-300 Cannot parse: \"" + "\", reason: \"" + e.getMessage() + "\"";
        if (rootCause != null && rootCause.getMessage() != null) {
            errMsg = errMsg + ", root cause: \"" + rootCause.getMessage() + "\"";
        }
        InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
        if (remoteAddress != null) {
            errMsg += "; remote: " + remoteAddress.getHostString();
        }
        logger.log(Level.WARNING, errMsg, e);
        pointHandler.handleBlockedPoint(errMsg);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ReportPoint(wavefront.report.ReportPoint)

Example 2 with ReportPoint

use of wavefront.report.ReportPoint in project java by wavefrontHQ.

the class JsonMetricsEndpoint method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    Map<String, String> tags = Maps.newHashMap();
    for (Enumeration<String> parameters = request.getParameterNames(); parameters.hasMoreElements(); ) {
        String tagk = parameters.nextElement().trim().toLowerCase();
        if (tagk.equals("h") || tagk.equals("p") || tagk.equals("d") || tagk.equals("t")) {
            continue;
        }
        if (request.getParameter(tagk) != null && request.getParameter(tagk).length() > 0) {
            tags.put(tagk, request.getParameter(tagk));
        }
    }
    List<ReportPoint> points = new ArrayList<>();
    Long timestamp;
    if (request.getParameter("d") == null) {
        timestamp = Clock.now();
    } else {
        try {
            timestamp = Long.parseLong(request.getParameter("d"));
        } catch (NumberFormatException e) {
            timestamp = Clock.now();
        }
    }
    String prefix;
    if (this.prefix != null) {
        prefix = request.getParameter("p") == null ? this.prefix : this.prefix + "." + request.getParameter("p");
    } else {
        prefix = request.getParameter("p");
    }
    String host = request.getParameter("h") == null ? defaultHost : request.getParameter("h");
    JsonNode metrics = new ObjectMapper().readTree(request.getReader());
    JsonMetricsParser.report("dummy", prefix, metrics, points, host, timestamp);
    for (ReportPoint point : points) {
        if (point.getAnnotations() == null) {
            point.setAnnotations(tags);
        } else {
            Map<String, String> newAnnotations = Maps.newHashMap(tags);
            newAnnotations.putAll(point.getAnnotations());
            point.setAnnotations(newAnnotations);
        }
        if (preprocessor != null) {
            if (!preprocessor.forReportPoint().filter(point)) {
                if (preprocessor.forReportPoint().getLastFilterResult() != null) {
                    blockedPointsLogger.warning(PointHandlerImpl.pointToString(point));
                } else {
                    blockedPointsLogger.info(PointHandlerImpl.pointToString(point));
                }
                handler.handleBlockedPoint(preprocessor.forReportPoint().getLastFilterResult());
                continue;
            }
            preprocessor.forReportPoint().transform(point);
        }
        handler.reportPoint(point, "json: " + PointHandlerImpl.pointToString(point));
    }
    response.setContentType("text/html;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    baseRequest.setHandled(true);
}
Also used : ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ReportPoint(wavefront.report.ReportPoint) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ReportPoint

use of wavefront.report.ReportPoint in project java by wavefrontHQ.

the class OpenTSDBPortUnificationHandler method reportMetric.

/**
 * Parse the individual metric object and send the metric to on to the point handler.
 *
 * @param metric the JSON object representing a single metric
 * @return True if the metric was reported successfully; False o/w
 * @see <a href="http://opentsdb.net/docs/build/html/api_http/put.html">OpenTSDB /api/put documentation</a>
 */
private boolean reportMetric(final JsonNode metric) {
    try {
        String metricName = metric.get("metric").textValue();
        JsonNode tags = metric.get("tags");
        Map<String, String> wftags = JsonMetricsParser.makeTags(tags);
        String hostName;
        if (wftags.containsKey("host")) {
            hostName = wftags.get("host");
        } else if (wftags.containsKey("source")) {
            hostName = wftags.get("source");
        } else {
            hostName = decoder.getDefaultHostName();
        }
        // remove source/host from the tags list
        Map<String, String> wftags2 = new HashMap<>();
        for (Map.Entry<String, String> wftag : wftags.entrySet()) {
            if (wftag.getKey().equalsIgnoreCase("host") || wftag.getKey().equalsIgnoreCase("source")) {
                continue;
            }
            wftags2.put(wftag.getKey(), wftag.getValue());
        }
        ReportPoint.Builder builder = ReportPoint.newBuilder();
        builder.setMetric(metricName);
        JsonNode time = metric.get("timestamp");
        // if timestamp is not available, fall back to Clock.now()
        long ts = Clock.now();
        if (time != null) {
            int timestampSize = Long.toString(time.asLong()).length();
            if (timestampSize == 19) {
                // nanoseconds
                ts = time.asLong() / 1000000;
            } else if (timestampSize == 16) {
                // microseconds
                ts = time.asLong() / 1000;
            } else if (timestampSize == 13) {
                // milliseconds
                ts = time.asLong();
            } else {
                // seconds
                ts = time.asLong() * 1000;
            }
        }
        builder.setTimestamp(ts);
        JsonNode value = metric.get("value");
        if (value == null) {
            pointHandler.handleBlockedPoint("Skipping.  Missing 'value' in JSON node.");
            return false;
        }
        if (value.isDouble()) {
            builder.setValue(value.asDouble());
        } else {
            builder.setValue(value.asLong());
        }
        builder.setAnnotations(wftags2);
        builder.setTable("dummy");
        builder.setHost(hostName);
        ReportPoint point = builder.build();
        if (preprocessor != null) {
            preprocessor.forReportPoint().transform(point);
            if (!preprocessor.forReportPoint().filter(point)) {
                if (preprocessor.forReportPoint().getLastFilterResult() != null) {
                    blockedPointsLogger.warning(PointHandlerImpl.pointToString(point));
                } else {
                    blockedPointsLogger.info(PointHandlerImpl.pointToString(point));
                }
                pointHandler.handleBlockedPoint(preprocessor.forReportPoint().getLastFilterResult());
                return false;
            }
        }
        pointHandler.reportPoint(point, null);
        return true;
    } catch (final Exception e) {
        blockMessage("WF-300", "Failed to add metric", e, null);
        return false;
    }
}
Also used : HashMap(java.util.HashMap) JsonNode(com.fasterxml.jackson.databind.JsonNode) HashMap(java.util.HashMap) Map(java.util.Map) ReportPoint(wavefront.report.ReportPoint) ReportPoint(wavefront.report.ReportPoint) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 4 with ReportPoint

use of wavefront.report.ReportPoint in project java by wavefrontHQ.

the class AccumulationTask method run.

@Override
public void run() {
    while (input.size() > 0 && !Thread.currentThread().isInterrupted()) {
        List<String> lines = input.peek();
        if (lines == null) {
            // remove corrupt data
            input.remove();
            continue;
        }
        long startNanos = nanoTime();
        for (String line : lines) {
            try {
                // Ignore empty lines
                if ((line = line.trim()).isEmpty()) {
                    continue;
                }
                // Parse line
                points.clear();
                try {
                    decoder.decodeReportPoints(line, points, "c");
                } catch (Exception e) {
                    final Throwable cause = Throwables.getRootCause(e);
                    String errMsg = "WF-300 Cannot parse: \"" + line + "\", reason: \"" + e.getMessage() + "\"";
                    if (cause != null && cause.getMessage() != null) {
                        errMsg = errMsg + ", root cause: \"" + cause.getMessage() + "\"";
                    }
                    throw new IllegalArgumentException(errMsg);
                }
                // now have the point, continue like in PointHandlerImpl
                ReportPoint event = points.get(0);
                // need the granularity here
                Validation.validatePoint(event, granularity.name(), line, validationLevel);
                if (event.getValue() instanceof Double) {
                    // Get key
                    Utils.HistogramKey histogramKey = Utils.makeKey(event, granularity);
                    double value = (Double) event.getValue();
                    eventCounter.inc();
                    // atomic update
                    digests.put(histogramKey, value, compression, ttlMillis);
                } else if (event.getValue() instanceof Histogram) {
                    Histogram value = (Histogram) event.getValue();
                    Utils.Granularity granularity = fromMillis(value.getDuration());
                    histogramBinCount.update(value.getCounts().size());
                    histogramSampleCount.update(value.getCounts().stream().mapToLong(x -> x).sum());
                    // Key
                    Utils.HistogramKey histogramKey = Utils.makeKey(event, granularity);
                    histogramCounter.inc();
                    // atomic update
                    digests.put(histogramKey, value, compression, ttlMillis);
                }
            } catch (Exception e) {
                if (!(e instanceof IllegalArgumentException)) {
                    logger.log(Level.SEVERE, "Unexpected error while parsing/accumulating sample: " + e.getMessage(), e);
                }
                ignoredCounter.inc();
                if (StringUtils.isNotEmpty(e.getMessage())) {
                    blockedPointsHandler.handleBlockedPoint(e.getMessage());
                }
            }
        }
        // end point processing
        input.remove();
        batchProcessTime.update(nanoTime() - startNanos);
    }
// end batch processing
}
Also used : PointHandler(com.wavefront.agent.PointHandler) StringUtils(org.apache.commons.lang.StringUtils) Counter(com.yammer.metrics.core.Counter) Validation(com.wavefront.agent.Validation) Throwables(com.google.common.base.Throwables) ObjectQueue(com.squareup.tape.ObjectQueue) AgentDigest(com.tdunning.math.stats.AgentDigest) Logger(java.util.logging.Logger) Level(java.util.logging.Level) System.nanoTime(java.lang.System.nanoTime) List(java.util.List) Lists(com.google.common.collect.Lists) Utils(com.wavefront.agent.histogram.Utils) MetricName(com.yammer.metrics.core.MetricName) ReportPoint(wavefront.report.ReportPoint) Decoder(com.wavefront.ingester.Decoder) Metrics(com.yammer.metrics.Metrics) Histogram(wavefront.report.Histogram) Granularity.fromMillis(com.wavefront.agent.histogram.Utils.Granularity.fromMillis) Histogram(wavefront.report.Histogram) StringUtils(org.apache.commons.lang.StringUtils) Utils(com.wavefront.agent.histogram.Utils) ReportPoint(wavefront.report.ReportPoint)

Example 5 with ReportPoint

use of wavefront.report.ReportPoint in project java by wavefrontHQ.

the class TapeReportPointConverter method toStream.

@Override
public void toStream(ReportPoint point, OutputStream outputStream) throws IOException {
    BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
    DatumWriter<ReportPoint> writer = new SpecificDatumWriter<>(ReportPoint.SCHEMA$);
    writer.write(point, encoder);
    encoder.flush();
}
Also used : BinaryEncoder(org.apache.avro.io.BinaryEncoder) ReportPoint(wavefront.report.ReportPoint) SpecificDatumWriter(org.apache.avro.specific.SpecificDatumWriter)

Aggregations

ReportPoint (wavefront.report.ReportPoint)71 Test (org.junit.Test)50 ArrayList (java.util.ArrayList)24 Histogram (wavefront.report.Histogram)10 PointHandler (com.wavefront.agent.PointHandler)4 List (java.util.List)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AgentDigest (com.tdunning.math.stats.AgentDigest)2 InetSocketAddress (java.net.InetSocketAddress)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Throwables (com.google.common.base.Throwables)1 ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 ObjectQueue (com.squareup.tape.ObjectQueue)1 Validation (com.wavefront.agent.Validation)1 Utils (com.wavefront.agent.histogram.Utils)1 Granularity.fromMillis (com.wavefront.agent.histogram.Utils.Granularity.fromMillis)1