Search in sources :

Example 61 with ReportPoint

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

the class ChannelStringHandler method processPointLine.

/**
 * This probably belongs in a base class.  It's only done like this so it can be easily re-used. This should be
 * refactored when it's clear where it belongs.
 */
public static void processPointLine(final String message, Decoder<String> decoder, final PointHandler pointHandler, @Nullable final PointPreprocessor preprocessor, @Nullable final ChannelHandlerContext ctx) {
    // ignore empty lines.
    if (message == null)
        return;
    String pointLine = message.trim();
    if (pointLine.isEmpty())
        return;
    // transform the line if needed
    if (preprocessor != null) {
        pointLine = preprocessor.forPointLine().transform(pointLine);
        // apply white/black lists after formatting
        if (!preprocessor.forPointLine().filter(pointLine)) {
            if (preprocessor.forPointLine().getLastFilterResult() != null) {
                blockedPointsLogger.warning(pointLine);
            } else {
                blockedPointsLogger.info(pointLine);
            }
            pointHandler.handleBlockedPoint(preprocessor.forPointLine().getLastFilterResult());
            return;
        }
    }
    // decode the line into report points
    List<ReportPoint> points = Lists.newArrayListWithExpectedSize(1);
    try {
        decoder.decodeReportPoints(pointLine, points, "dummy");
    } catch (Exception e) {
        final Throwable rootCause = Throwables.getRootCause(e);
        String errMsg = "WF-300 Cannot parse: \"" + pointLine + "\", reason: \"" + e.getMessage() + "\"";
        if (rootCause != null && rootCause.getMessage() != null) {
            errMsg = errMsg + ", root cause: \"" + rootCause.getMessage() + "\"";
        }
        if (ctx != null) {
            InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
            if (remoteAddress != null) {
                errMsg += "; remote: " + remoteAddress.getHostString();
            }
        }
        blockedPointsLogger.warning(pointLine);
        pointHandler.handleBlockedPoint(errMsg);
        return;
    }
    // transform the point after parsing, and apply additional white/black lists if any
    if (preprocessor != null) {
        for (ReportPoint point : points) {
            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;
            }
        }
    }
    pointHandler.reportPoints(points);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ReportPoint(wavefront.report.ReportPoint)

Example 62 with ReportPoint

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

the class WriteHttpJsonMetricsEndpoint method handle.

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    response.setContentType("text/html;charset=utf-8");
    JsonNode metrics = new ObjectMapper().readTree(request.getReader());
    if (!metrics.isArray()) {
        logger.warning("metrics is not an array!");
        handler.handleBlockedPoint("[metrics] is not an array!");
        // return HTTP 400
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        baseRequest.setHandled(true);
        return;
    }
    for (final JsonNode metric : metrics) {
        try {
            JsonNode host = metric.get("host");
            String hostName;
            if (host != null) {
                hostName = host.textValue();
                if (hostName == null || hostName.isEmpty()) {
                    hostName = defaultHost;
                }
            } else {
                hostName = defaultHost;
            }
            JsonNode time = metric.get("time");
            long ts = 0;
            if (time != null) {
                ts = time.asLong() * 1000;
            }
            JsonNode values = metric.get("values");
            if (values == null) {
                handler.handleBlockedPoint("[values] missing in JSON object");
                logger.warning("Skipping.  Missing values.");
                continue;
            }
            int index = 0;
            for (final JsonNode value : values) {
                String metricName = getMetricName(metric, index);
                ReportPoint.Builder builder = ReportPoint.newBuilder().setMetric(metricName).setTable("dummy").setTimestamp(ts).setHost(hostName);
                if (value.isDouble()) {
                    builder.setValue(value.asDouble());
                } else {
                    builder.setValue(value.asLong());
                }
                List<ReportPoint> parsedPoints = Lists.newArrayListWithExpectedSize(1);
                ReportPoint point = builder.build();
                if (preprocessor != null && preprocessor.forPointLine().hasTransformers()) {
                    // 
                    String pointLine = PointHandlerImpl.pointToString(point);
                    pointLine = preprocessor.forPointLine().transform(pointLine);
                    recoder.decodeReportPoints(pointLine, parsedPoints, "dummy");
                } else {
                    parsedPoints.add(point);
                }
                for (ReportPoint parsedPoint : parsedPoints) {
                    if (preprocessor != null) {
                        preprocessor.forReportPoint().transform(parsedPoint);
                        if (!preprocessor.forReportPoint().filter(parsedPoint)) {
                            if (preprocessor.forReportPoint().getLastFilterResult() != null) {
                                blockedPointsLogger.warning(PointHandlerImpl.pointToString(parsedPoint));
                            } else {
                                blockedPointsLogger.info(PointHandlerImpl.pointToString(parsedPoint));
                            }
                            handler.handleBlockedPoint(preprocessor.forReportPoint().getLastFilterResult());
                            continue;
                        }
                    }
                    handler.reportPoint(parsedPoint, "write_http json: " + PointHandlerImpl.pointToString(parsedPoint));
                }
                index++;
            }
        } catch (final Exception e) {
            handler.handleBlockedPoint("Failed adding metric: " + e);
            logger.log(Level.WARNING, "Failed adding metric", e);
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            baseRequest.setHandled(true);
            return;
        }
    }
    response.setStatus(HttpServletResponse.SC_OK);
    baseRequest.setHandled(true);
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) ReportPoint(wavefront.report.ReportPoint) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReportPoint(wavefront.report.ReportPoint) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 63 with ReportPoint

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

the class InteractiveLogsTester method interactiveTest.

/**
 * Read one line of stdin and print a message to stdout.
 */
public boolean interactiveTest() throws ConfigurationException {
    final AtomicBoolean reported = new AtomicBoolean(false);
    LogsIngester logsIngester = new LogsIngester(new PointHandler() {

        @Override
        public void reportPoint(ReportPoint point, @Nullable String debugLine) {
            reported.set(true);
            System.out.println(PointHandlerImpl.pointToString(point));
        }

        @Override
        public void reportPoints(List<ReportPoint> points) {
            for (ReportPoint point : points) reportPoint(point, "");
        }

        @Override
        public void handleBlockedPoint(@Nullable String pointLine) {
            System.out.println("Blocked point: " + pointLine);
        }
    }, logsIngestionConfigSupplier, prefix, System::currentTimeMillis);
    String line = stdin.nextLine();
    logsIngester.ingestLog(new LogsMessage() {

        @Override
        public String getLogLine() {
            return line;
        }

        @Override
        public String hostOrDefault(String fallbackHost) {
            try {
                return InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException e) {
                return "localhost";
            }
        }
    });
    logsIngester.flush();
    if (!reported.get()) {
        System.out.println("Input matched no groks.");
    }
    return stdin.hasNext();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnknownHostException(java.net.UnknownHostException) PointHandler(com.wavefront.agent.PointHandler) ReportPoint(wavefront.report.ReportPoint)

Example 64 with ReportPoint

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

the class PointHandlerTest method testHistogramReportPointToString_BadValue.

@Test(expected = RuntimeException.class)
public void testHistogramReportPointToString_BadValue() {
    ReportPoint p = new ReportPoint("m", 1469751813L, new ArrayUtils(), "h", "c", ImmutableMap.of());
    PointHandlerImpl.pointToString(p);
}
Also used : ArrayUtils(org.apache.commons.lang.ArrayUtils) ReportPoint(wavefront.report.ReportPoint) Test(org.junit.Test)

Example 65 with ReportPoint

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

the class PointHandlerTest method testPointAnnotationKeyValidation.

@Test
public void testPointAnnotationKeyValidation() {
    Map<String, String> goodMap = new HashMap<String, String>();
    goodMap.put("key", "value");
    Map<String, String> badMap = new HashMap<String, String>();
    badMap.put("k:ey", "value");
    ReportPoint rp = new ReportPoint("some metric", System.currentTimeMillis(), 10L, "host", "table", goodMap);
    Assert.assertTrue(Validation.annotationKeysAreValid(rp));
    rp.setAnnotations(badMap);
    Assert.assertFalse(Validation.annotationKeysAreValid(rp));
}
Also used : HashMap(java.util.HashMap) ReportPoint(wavefront.report.ReportPoint) Test(org.junit.Test)

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