Search in sources :

Example 1 with DataFormat

use of com.wavefront.agent.formatter.DataFormat in project java by wavefrontHQ.

the class WavefrontPortUnificationHandler method processLine.

/**
 * @param ctx      ChannelHandler context (to retrieve remote client's IP in case of errors)
 * @param message  line being processed
 */
@Override
protected void processLine(final ChannelHandlerContext ctx, @Nonnull String message, @Nullable DataFormat format) {
    DataFormat dataFormat = format == null ? DataFormat.autodetect(message) : format;
    switch(dataFormat) {
        case SOURCE_TAG:
            ReportableEntityHandler<ReportSourceTag, SourceTag> sourceTagHandler = sourceTagHandlerSupplier.get();
            if (sourceTagHandler == null || sourceTagDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "sourceTag-formatted data!");
                return;
            }
            List<ReportSourceTag> output = new ArrayList<>(1);
            try {
                sourceTagDecoder.decode(message, output, "dummy");
                for (ReportSourceTag tag : output) {
                    sourceTagHandler.report(tag);
                }
            } catch (Exception e) {
                sourceTagHandler.reject(message, formatErrorMessage("WF-300 Cannot parse sourceTag: \"" + message + "\"", e, ctx));
            }
            return;
        case EVENT:
            ReportableEntityHandler<ReportEvent, ReportEvent> eventHandler = eventHandlerSupplier.get();
            if (eventHandler == null || eventDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept event data!");
                return;
            }
            List<ReportEvent> events = new ArrayList<>(1);
            try {
                eventDecoder.decode(message, events, "dummy");
                for (ReportEvent event : events) {
                    eventHandler.report(event);
                }
            } catch (Exception e) {
                eventHandler.reject(message, formatErrorMessage("WF-300 Cannot parse event: \"" + message + "\"", e, ctx));
            }
            return;
        case SPAN:
            ReportableEntityHandler<Span, String> spanHandler = spanHandlerSupplier.get();
            if (spanHandler == null || spanDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "tracing data (spans)!");
                return;
            }
            message = annotator == null ? message : annotator.apply(ctx, message);
            receivedSpansTotal.get().inc();
            preprocessAndHandleSpan(message, spanDecoder, spanHandler, spanHandler::report, preprocessorSupplier, ctx, span -> sampler.sample(span, discardedSpansBySampler.get()));
            return;
        case SPAN_LOG:
            if (isFeatureDisabled(spanLogsDisabled, SPANLOGS_DISABLED, discardedSpanLogs.get()))
                return;
            ReportableEntityHandler<SpanLogs, String> spanLogsHandler = spanLogsHandlerSupplier.get();
            if (spanLogsHandler == null || spanLogsDecoder == null || spanDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "tracing data (span logs)!");
                return;
            }
            handleSpanLogs(message, spanLogsDecoder, spanDecoder, spanLogsHandler, preprocessorSupplier, ctx, span -> sampler.sample(span, discardedSpanLogsBySampler.get()));
            return;
        case HISTOGRAM:
            if (isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get()))
                return;
            ReportableEntityHandler<ReportPoint, String> histogramHandler = histogramHandlerSupplier.get();
            if (histogramHandler == null || histogramDecoder == null) {
                wavefrontHandler.reject(message, "Port is not configured to accept " + "histogram-formatted data!");
                return;
            }
            message = annotator == null ? message : annotator.apply(ctx, message);
            preprocessAndHandlePoint(message, histogramDecoder, histogramHandler, preprocessorSupplier, ctx, "histogram");
            return;
        default:
            message = annotator == null ? message : annotator.apply(ctx, message);
            preprocessAndHandlePoint(message, wavefrontDecoder, wavefrontHandler, preprocessorSupplier, ctx, "metric");
    }
}
Also used : ArrayList(java.util.ArrayList) ReportSourceTag(wavefront.report.ReportSourceTag) SourceTag(com.wavefront.dto.SourceTag) SpanLogs(wavefront.report.SpanLogs) SpanUtils.handleSpanLogs(com.wavefront.agent.listeners.tracing.SpanUtils.handleSpanLogs) SpanUtils.preprocessAndHandleSpan(com.wavefront.agent.listeners.tracing.SpanUtils.preprocessAndHandleSpan) Span(wavefront.report.Span) ReportEvent(wavefront.report.ReportEvent) DataFormat(com.wavefront.agent.formatter.DataFormat) ReportSourceTag(wavefront.report.ReportSourceTag) ReportPoint(wavefront.report.ReportPoint)

Example 2 with DataFormat

use of com.wavefront.agent.formatter.DataFormat in project java by wavefrontHQ.

the class WavefrontPortUnificationHandler method handleHttpMessage.

@Override
protected void handleHttpMessage(ChannelHandlerContext ctx, FullHttpRequest request) {
    StringBuilder out = new StringBuilder();
    DataFormat format = getFormat(request);
    if ((format == HISTOGRAM && isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get(), out, request)) || (format == SPAN_LOG && isFeatureDisabled(spanLogsDisabled, SPANLOGS_DISABLED, discardedSpanLogs.get(), out, request))) {
        writeHttpResponse(ctx, HttpResponseStatus.FORBIDDEN, out, request);
        return;
    } else if (format == SPAN && isFeatureDisabled(traceDisabled, SPAN_DISABLED, discardedSpans.get(), out, request)) {
        receivedSpansTotal.get().inc(discardedSpans.get().count());
        writeHttpResponse(ctx, HttpResponseStatus.FORBIDDEN, out, request);
        return;
    }
    super.handleHttpMessage(ctx, request);
}
Also used : DataFormat(com.wavefront.agent.formatter.DataFormat)

Example 3 with DataFormat

use of com.wavefront.agent.formatter.DataFormat in project java by wavefrontHQ.

the class RelayPortUnificationHandler method handleHttpMessage.

@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) {
    URI uri = URI.create(request.uri());
    StringBuilder output = new StringBuilder();
    String path = uri.getPath();
    final boolean isDirectIngestion = path.startsWith("/report");
    if (path.endsWith("/checkin") && (path.startsWith("/api/daemon") || path.contains("wfproxy"))) {
        // simulate checkin response for proxy chaining
        ObjectNode jsonResponse = JsonNodeFactory.instance.objectNode();
        jsonResponse.put("currentTime", Clock.now());
        jsonResponse.put("allowAnyHostKeys", true);
        writeHttpResponse(ctx, HttpResponseStatus.OK, jsonResponse, request);
        return;
    }
    String format = URLEncodedUtils.parse(uri, CharsetUtil.UTF_8).stream().filter(x -> x.getName().equals("format") || x.getName().equals("f")).map(NameValuePair::getValue).findFirst().orElse(Constants.PUSH_FORMAT_WAVEFRONT);
    // Return HTTP 200 (OK) for payloads received on the proxy endpoint
    // Return HTTP 202 (ACCEPTED) for payloads received on the DDI endpoint
    // Return HTTP 204 (NO_CONTENT) for payloads received on all other endpoints
    HttpResponseStatus okStatus;
    if (isDirectIngestion) {
        okStatus = HttpResponseStatus.ACCEPTED;
    } else if (path.contains("/pushdata/") || path.contains("wfproxy/report")) {
        okStatus = HttpResponseStatus.OK;
    } else {
        okStatus = HttpResponseStatus.NO_CONTENT;
    }
    HttpResponseStatus status;
    switch(format) {
        case Constants.PUSH_FORMAT_HISTOGRAM:
            if (isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get(), output, request)) {
                status = HttpResponseStatus.FORBIDDEN;
                break;
            }
        case Constants.PUSH_FORMAT_WAVEFRONT:
        case Constants.PUSH_FORMAT_GRAPHITE_V2:
            AtomicBoolean hasSuccessfulPoints = new AtomicBoolean(false);
            try {
                // noinspection unchecked
                ReportableEntityDecoder<String, ReportPoint> histogramDecoder = (ReportableEntityDecoder<String, ReportPoint>) decoders.get(ReportableEntityType.HISTOGRAM);
                Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(message -> {
                    DataFormat dataFormat = DataFormat.autodetect(message);
                    switch(dataFormat) {
                        case EVENT:
                            wavefrontHandler.reject(message, "Relay port does not support " + "event-formatted data!");
                            break;
                        case SOURCE_TAG:
                            wavefrontHandler.reject(message, "Relay port does not support " + "sourceTag-formatted data!");
                            break;
                        case HISTOGRAM:
                            if (isFeatureDisabled(histogramDisabled, HISTO_DISABLED, discardedHistograms.get(), output)) {
                                break;
                            }
                            preprocessAndHandlePoint(message, histogramDecoder, histogramHandlerSupplier.get(), preprocessorSupplier, ctx, "histogram");
                            hasSuccessfulPoints.set(true);
                            break;
                        default:
                            // only apply annotator if point received on the DDI endpoint
                            message = annotator != null && isDirectIngestion ? annotator.apply(ctx, message) : message;
                            preprocessAndHandlePoint(message, wavefrontDecoder, wavefrontHandler, preprocessorSupplier, ctx, "metric");
                            hasSuccessfulPoints.set(true);
                            break;
                    }
                });
                status = hasSuccessfulPoints.get() ? okStatus : HttpResponseStatus.BAD_REQUEST;
            } catch (Exception e) {
                status = HttpResponseStatus.BAD_REQUEST;
                output.append(errorMessageWithRootCause(e));
                logWarning("WF-300: Failed to handle HTTP POST", e, ctx);
            }
            break;
        case Constants.PUSH_FORMAT_TRACING:
            if (isFeatureDisabled(traceDisabled, SPAN_DISABLED, discardedSpans.get(), output, request)) {
                receivedSpansTotal.get().inc(discardedSpans.get().count());
                status = HttpResponseStatus.FORBIDDEN;
                break;
            }
            List<Span> spans = new ArrayList<>();
            // noinspection unchecked
            ReportableEntityDecoder<String, Span> spanDecoder = (ReportableEntityDecoder<String, Span>) decoders.get(ReportableEntityType.TRACE);
            ReportableEntityHandler<Span, String> spanHandler = spanHandlerSupplier.get();
            Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(line -> {
                try {
                    receivedSpansTotal.get().inc();
                    spanDecoder.decode(line, spans, "dummy");
                } catch (Exception e) {
                    spanHandler.reject(line, formatErrorMessage(line, e, ctx));
                }
            });
            spans.forEach(spanHandler::report);
            status = okStatus;
            break;
        case Constants.PUSH_FORMAT_TRACING_SPAN_LOGS:
            if (isFeatureDisabled(spanLogsDisabled, SPANLOGS_DISABLED, discardedSpanLogs.get(), output, request)) {
                status = HttpResponseStatus.FORBIDDEN;
                break;
            }
            List<SpanLogs> spanLogs = new ArrayList<>();
            // noinspection unchecked
            ReportableEntityDecoder<JsonNode, SpanLogs> spanLogDecoder = (ReportableEntityDecoder<JsonNode, SpanLogs>) decoders.get(ReportableEntityType.TRACE_SPAN_LOGS);
            ReportableEntityHandler<SpanLogs, String> spanLogsHandler = spanLogsHandlerSupplier.get();
            Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(line -> {
                try {
                    spanLogDecoder.decode(JSON_PARSER.readTree(line), spanLogs, "dummy");
                } catch (Exception e) {
                    spanLogsHandler.reject(line, formatErrorMessage(line, e, ctx));
                }
            });
            spanLogs.forEach(spanLogsHandler::report);
            status = okStatus;
            break;
        default:
            status = HttpResponseStatus.BAD_REQUEST;
            logger.warning("Unexpected format for incoming HTTP request: " + format);
    }
    writeHttpResponse(ctx, status, output, request);
}
Also used : HealthCheckManager(com.wavefront.agent.channel.HealthCheckManager) FeatureCheckUtils.isFeatureDisabled(com.wavefront.agent.listeners.FeatureCheckUtils.isFeatureDisabled) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SPANLOGS_DISABLED(com.wavefront.agent.listeners.FeatureCheckUtils.SPANLOGS_DISABLED) SpanLogs(wavefront.report.SpanLogs) Supplier(java.util.function.Supplier) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DataFormat(com.wavefront.agent.formatter.DataFormat) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) TokenAuthenticator(com.wavefront.agent.auth.TokenAuthenticator) ReportableEntityHandler(com.wavefront.agent.handlers.ReportableEntityHandler) ChannelUtils.errorMessageWithRootCause(com.wavefront.agent.channel.ChannelUtils.errorMessageWithRootCause) ChannelUtils.writeHttpResponse(com.wavefront.agent.channel.ChannelUtils.writeHttpResponse) Map(java.util.Map) SharedGraphiteHostAnnotator(com.wavefront.agent.channel.SharedGraphiteHostAnnotator) HandlerKey(com.wavefront.agent.handlers.HandlerKey) Constants(com.wavefront.api.agent.Constants) CharsetUtil(io.netty.util.CharsetUtil) MetricName(com.yammer.metrics.core.MetricName) ReportPoint(wavefront.report.ReportPoint) JsonNode(com.fasterxml.jackson.databind.JsonNode) Clock(com.wavefront.common.Clock) URI(java.net.URI) Splitter(com.google.common.base.Splitter) Nullable(javax.annotation.Nullable) Counter(com.yammer.metrics.core.Counter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ReportableEntityPreprocessor(com.wavefront.agent.preprocessor.ReportableEntityPreprocessor) SPAN_DISABLED(com.wavefront.agent.listeners.FeatureCheckUtils.SPAN_DISABLED) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Span(wavefront.report.Span) Logger(java.util.logging.Logger) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ReportableEntityType(com.wavefront.data.ReportableEntityType) List(java.util.List) ChannelUtils.formatErrorMessage(com.wavefront.agent.channel.ChannelUtils.formatErrorMessage) ReportableEntityHandlerFactory(com.wavefront.agent.handlers.ReportableEntityHandlerFactory) ReportableEntityDecoder(com.wavefront.ingester.ReportableEntityDecoder) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Utils(com.wavefront.common.Utils) URLEncodedUtils(org.apache.http.client.utils.URLEncodedUtils) ChannelHandler(io.netty.channel.ChannelHandler) Metrics(com.yammer.metrics.Metrics) HISTO_DISABLED(com.wavefront.agent.listeners.FeatureCheckUtils.HISTO_DISABLED) NameValuePair(org.apache.http.NameValuePair) WavefrontPortUnificationHandler.preprocessAndHandlePoint(com.wavefront.agent.listeners.WavefrontPortUnificationHandler.preprocessAndHandlePoint) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) ArrayList(java.util.ArrayList) SpanLogs(wavefront.report.SpanLogs) JsonNode(com.fasterxml.jackson.databind.JsonNode) URI(java.net.URI) Span(wavefront.report.Span) DataFormat(com.wavefront.agent.formatter.DataFormat) NameValuePair(org.apache.http.NameValuePair) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ReportableEntityDecoder(com.wavefront.ingester.ReportableEntityDecoder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReportPoint(wavefront.report.ReportPoint)

Example 4 with DataFormat

use of com.wavefront.agent.formatter.DataFormat in project java by wavefrontHQ.

the class AbstractLineDelimitedHandler method handleHttpMessage.

/**
 * Handles an incoming HTTP message. Accepts HTTP POST on all paths
 */
@Override
protected void handleHttpMessage(final ChannelHandlerContext ctx, final FullHttpRequest request) {
    StringBuilder output = new StringBuilder();
    HttpResponseStatus status;
    try {
        DataFormat format = getFormat(request);
        Splitter.on('\n').trimResults().omitEmptyStrings().split(request.content().toString(CharsetUtil.UTF_8)).forEach(line -> processLine(ctx, line, format));
        status = HttpResponseStatus.ACCEPTED;
    } catch (Exception e) {
        status = HttpResponseStatus.BAD_REQUEST;
        output.append(errorMessageWithRootCause(e));
        logWarning("WF-300: Failed to handle HTTP POST", e, ctx);
    }
    writeHttpResponse(ctx, status, output, request);
}
Also used : HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DataFormat(com.wavefront.agent.formatter.DataFormat)

Aggregations

DataFormat (com.wavefront.agent.formatter.DataFormat)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 ArrayList (java.util.ArrayList)2 ReportPoint (wavefront.report.ReportPoint)2 Span (wavefront.report.Span)2 SpanLogs (wavefront.report.SpanLogs)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Splitter (com.google.common.base.Splitter)1 TokenAuthenticator (com.wavefront.agent.auth.TokenAuthenticator)1 ChannelUtils.errorMessageWithRootCause (com.wavefront.agent.channel.ChannelUtils.errorMessageWithRootCause)1 ChannelUtils.formatErrorMessage (com.wavefront.agent.channel.ChannelUtils.formatErrorMessage)1 ChannelUtils.writeHttpResponse (com.wavefront.agent.channel.ChannelUtils.writeHttpResponse)1 HealthCheckManager (com.wavefront.agent.channel.HealthCheckManager)1 SharedGraphiteHostAnnotator (com.wavefront.agent.channel.SharedGraphiteHostAnnotator)1 HandlerKey (com.wavefront.agent.handlers.HandlerKey)1 ReportableEntityHandler (com.wavefront.agent.handlers.ReportableEntityHandler)1 ReportableEntityHandlerFactory (com.wavefront.agent.handlers.ReportableEntityHandlerFactory)1