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");
}
}
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);
}
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);
}
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);
}
Aggregations