use of com.wavefront.agent.listeners.FeatureCheckUtils.SPAN_DISABLED 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);
}
Aggregations