use of com.wavefront.agent.preprocessor.ReportableEntityPreprocessor in project java by wavefrontHQ.
the class ChannelByteArrayHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, byte[] msg) {
// ignore empty lines.
if (msg == null || msg.length == 0) {
return;
}
ReportableEntityPreprocessor preprocessor = preprocessorSupplier == null ? null : preprocessorSupplier.get();
List<ReportPoint> points = new ArrayList<>(1);
try {
decoder.decode(msg, points, "dummy");
for (ReportPoint point : points) {
if (preprocessor != null && !preprocessor.forPointLine().getTransformers().isEmpty()) {
String pointLine = ReportPointSerializer.pointToString(point);
pointLine = preprocessor.forPointLine().transform(pointLine);
List<ReportPoint> parsedPoints = new ArrayList<>(1);
recoder.decodeReportPoints(pointLine, parsedPoints, "dummy");
parsedPoints.forEach(x -> preprocessAndReportPoint(x, preprocessor));
} else {
preprocessAndReportPoint(point, preprocessor);
}
}
} 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.block(null, errMsg);
}
}
use of com.wavefront.agent.preprocessor.ReportableEntityPreprocessor in project java by wavefrontHQ.
the class WavefrontPortUnificationHandler method preprocessAndHandlePoint.
public static void preprocessAndHandlePoint(String message, ReportableEntityDecoder<String, ReportPoint> decoder, ReportableEntityHandler<ReportPoint, String> handler, @Nullable Supplier<ReportableEntityPreprocessor> preprocessorSupplier, @Nullable ChannelHandlerContext ctx, String type) {
ReportableEntityPreprocessor preprocessor = preprocessorSupplier == null ? null : preprocessorSupplier.get();
String[] messageHolder = new String[1];
// transform the line if needed
if (preprocessor != null) {
message = preprocessor.forPointLine().transform(message);
// apply white/black lists after formatting
if (!preprocessor.forPointLine().filter(message, messageHolder)) {
if (messageHolder[0] != null) {
handler.reject((ReportPoint) null, message);
} else {
handler.block(null, message);
}
return;
}
}
List<ReportPoint> output = new ArrayList<>(1);
try {
decoder.decode(message, output, "dummy");
} catch (Exception e) {
handler.reject(message, formatErrorMessage("WF-300 Cannot parse " + type + ": \"" + message + "\"", e, ctx));
return;
}
for (ReportPoint object : output) {
if (preprocessor != null) {
preprocessor.forReportPoint().transform(object);
if (!preprocessor.forReportPoint().filter(object, messageHolder)) {
if (messageHolder[0] != null) {
handler.reject(object, messageHolder[0]);
} else {
handler.block(object);
}
return;
}
}
handler.report(object);
}
}
use of com.wavefront.agent.preprocessor.ReportableEntityPreprocessor in project java by wavefrontHQ.
the class WriteHttpJsonPortUnificationHandler method reportMetrics.
private void reportMetrics(JsonNode metrics) {
ReportableEntityPreprocessor preprocessor = preprocessorSupplier == null ? null : preprocessorSupplier.get();
String[] messageHolder = new String[1];
for (final JsonNode metric : metrics) {
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) {
pointHandler.reject((ReportPoint) null, "[values] missing in JSON object");
logger.warning("Skipping - [values] missing in JSON object.");
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 = new ArrayList<>(1);
ReportPoint point = builder.build();
if (preprocessor != null && preprocessor.forPointLine().getTransformers().size() > 0) {
//
String pointLine = ReportPointSerializer.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(point);
if (!preprocessor.forReportPoint().filter(point, messageHolder)) {
if (messageHolder[0] != null) {
pointHandler.reject(point, messageHolder[0]);
} else {
pointHandler.block(point);
}
continue;
}
}
pointHandler.report(parsedPoint);
}
index++;
}
}
}
use of com.wavefront.agent.preprocessor.ReportableEntityPreprocessor 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
* @param ctx channel handler context (to retrieve remote address)
* @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, ChannelHandlerContext ctx) {
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 = resolver == null ? "unknown" : resolver.apply(getRemoteAddress(ctx));
}
// 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.reject((ReportPoint) null, "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();
ReportableEntityPreprocessor preprocessor = preprocessorSupplier == null ? null : preprocessorSupplier.get();
String[] messageHolder = new String[1];
if (preprocessor != null) {
preprocessor.forReportPoint().transform(point);
if (!preprocessor.forReportPoint().filter(point, messageHolder)) {
if (messageHolder[0] != null) {
pointHandler.reject(point, messageHolder[0]);
return false;
} else {
pointHandler.block(point);
return true;
}
}
}
pointHandler.report(point);
return true;
} catch (final Exception e) {
logWarning("WF-300: Failed to add metric", e, null);
return false;
}
}
use of com.wavefront.agent.preprocessor.ReportableEntityPreprocessor in project java by wavefrontHQ.
the class JaegerGrpcCollectorHandlerTest method testJaegerPreprocessedDerivedMetrics.
/**
* Test for derived metrics emitted from Jaeger trace listeners. Derived metrics should report
* tag values post applying preprocessing rules to the span.
*/
@Test
public void testJaegerPreprocessedDerivedMetrics() throws Exception {
reset(mockTraceHandler, mockWavefrontSender);
mockTraceHandler.report(Span.newBuilder().setCustomer("dummy").setStartMillis(startTime).setDuration(4000).setName("HTTP GET").setSource(PREPROCESSED_SOURCE_VALUE).setSpanId("00000000-0000-0000-0000-00000012d687").setTraceId("00000000-4996-02d2-0000-011f71fb04cb").setAnnotations(ImmutableList.of(new Annotation("ip", "10.0.0.1"), new Annotation("service", PREPROCESSED_SERVICE_TAG_VALUE), new Annotation("application", PREPROCESSED_APPLICATION_TAG_VALUE), new Annotation("cluster", PREPROCESSED_CLUSTER_TAG_VALUE), new Annotation("shard", PREPROCESSED_SHARD_TAG_VALUE))).build());
expectLastCall();
Capture<HashMap<String, String>> tagsCapture = EasyMock.newCapture();
mockWavefrontSender.sendMetric(eq(HEART_BEAT_METRIC), eq(1.0), anyLong(), eq(PREPROCESSED_SOURCE_VALUE), EasyMock.capture(tagsCapture));
expectLastCall().anyTimes();
replay(mockTraceHandler, mockWavefrontSender);
Supplier<ReportableEntityPreprocessor> preprocessorSupplier = () -> {
ReportableEntityPreprocessor preprocessor = new ReportableEntityPreprocessor();
PreprocessorRuleMetrics preprocessorRuleMetrics = new PreprocessorRuleMetrics(null, null, null);
preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(APPLICATION_TAG_KEY, "^Jaeger.*", PREPROCESSED_APPLICATION_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(SERVICE_TAG_KEY, "^test.*", PREPROCESSED_SERVICE_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer("sourceName", "^jaeger.*", PREPROCESSED_SOURCE_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(CLUSTER_TAG_KEY, "^none.*", PREPROCESSED_CLUSTER_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
preprocessor.forSpan().addTransformer(new SpanReplaceRegexTransformer(SHARD_TAG_KEY, "^none.*", PREPROCESSED_SHARD_TAG_VALUE, null, null, false, x -> true, preprocessorRuleMetrics));
return preprocessor;
};
JaegerGrpcCollectorHandler handler = new JaegerGrpcCollectorHandler("9876", mockTraceHandler, mockTraceLogsHandler, mockWavefrontSender, () -> false, () -> false, preprocessorSupplier, new SpanSampler(new RateSampler(1.0D), () -> null), null, null);
Model.KeyValue ipTag = Model.KeyValue.newBuilder().setKey("ip").setVStr("10.0.0.1").setVType(Model.ValueType.STRING).build();
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES * 2);
buffer.putLong(1234567890L);
buffer.putLong(1234567890123L);
ByteString traceId = ByteString.copyFrom(buffer.array());
buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(1234567L);
ByteString span2Id = ByteString.copyFrom(buffer.array());
Model.Span span2 = Model.Span.newBuilder().setTraceId(traceId).setSpanId(span2Id).setDuration(Duration.newBuilder().setSeconds(4L).build()).setOperationName("HTTP GET").setStartTime(fromMillis(startTime)).build();
Model.Batch testBatch = Model.Batch.newBuilder().setProcess(Model.Process.newBuilder().setServiceName("testService").addTags(ipTag).build()).addAllSpans(ImmutableList.of(span2)).build();
Collector.PostSpansRequest batches = Collector.PostSpansRequest.newBuilder().setBatch(testBatch).build();
handler.postSpans(batches, emptyStreamObserver);
handler.run();
verifyWithTimeout(500, mockTraceHandler, mockWavefrontSender);
HashMap<String, String> tagsReturned = tagsCapture.getValue();
assertEquals(PREPROCESSED_APPLICATION_TAG_VALUE, tagsReturned.get(APPLICATION_TAG_KEY));
assertEquals(PREPROCESSED_SERVICE_TAG_VALUE, tagsReturned.get(SERVICE_TAG_KEY));
assertEquals(PREPROCESSED_CLUSTER_TAG_VALUE, tagsReturned.get(CLUSTER_TAG_KEY));
assertEquals(PREPROCESSED_SHARD_TAG_VALUE, tagsReturned.get(SHARD_TAG_KEY));
}
Aggregations