use of wavefront.report.Annotation in project java by wavefrontHQ.
the class SpanAddAnnotationTransformer method apply.
@Nullable
@Override
public Span apply(@Nullable Span span) {
if (span == null)
return null;
long startNanos = ruleMetrics.ruleStart();
try {
if (!v2Predicate.test(span))
return span;
span.getAnnotations().add(new Annotation(key, expandPlaceholders(value, span)));
ruleMetrics.incrementRuleAppliedCounter();
return span;
} finally {
ruleMetrics.ruleEnd(startNanos);
}
}
use of wavefront.report.Annotation in project java by wavefrontHQ.
the class SpanExtractAnnotationTransformer method internalApply.
protected void internalApply(@Nonnull Span span) {
List<Annotation> buffer = new ArrayList<>();
switch(input) {
case "spanName":
if (extractAnnotation(span, span.getName(), buffer) && patternReplaceInput != null) {
span.setName(compiledSearchPattern.matcher(span.getName()).replaceAll(expandPlaceholders(patternReplaceInput, span)));
}
break;
case "sourceName":
if (extractAnnotation(span, span.getSource(), buffer) && patternReplaceInput != null) {
span.setSource(compiledSearchPattern.matcher(span.getSource()).replaceAll(expandPlaceholders(patternReplaceInput, span)));
}
break;
default:
for (Annotation a : span.getAnnotations()) {
if (a.getKey().equals(input)) {
if (extractAnnotation(span, a.getValue(), buffer)) {
if (patternReplaceInput != null) {
a.setValue(compiledSearchPattern.matcher(a.getValue()).replaceAll(expandPlaceholders(patternReplaceInput, span)));
}
if (firstMatchOnly) {
break;
}
}
}
}
}
span.getAnnotations().addAll(buffer);
}
use of wavefront.report.Annotation in project java by wavefrontHQ.
the class SpanAddAnnotationIfNotExistsTransformer method apply.
@Nullable
@Override
public Span apply(@Nullable Span span) {
if (span == null)
return null;
long startNanos = ruleMetrics.ruleStart();
try {
if (!v2Predicate.test(span))
return span;
if (span.getAnnotations().stream().noneMatch(a -> a.getKey().equals(key))) {
span.getAnnotations().add(new Annotation(key, expandPlaceholders(value, span)));
ruleMetrics.incrementRuleAppliedCounter();
}
return span;
} finally {
ruleMetrics.ruleEnd(startNanos);
}
}
use of wavefront.report.Annotation in project java by wavefrontHQ.
the class ZipkinPortUnificationHandler method processZipkinSpan.
private void processZipkinSpan(zipkin2.Span zipkinSpan) {
if (ZIPKIN_DATA_LOGGER.isLoggable(Level.FINEST)) {
ZIPKIN_DATA_LOGGER.info("Inbound Zipkin span: " + zipkinSpan.toString());
}
// Add application tags, span references, span kind and http uri, responses etc.
List<Annotation> annotations = new ArrayList<>();
// Add original Zipkin trace and span ids as tags to make finding them easier
annotations.add(new Annotation("zipkinSpanId", zipkinSpan.id()));
annotations.add(new Annotation("zipkinTraceId", zipkinSpan.traceId()));
// Set Span's References.
if (zipkinSpan.parentId() != null) {
annotations.add(new Annotation(TraceConstants.PARENT_KEY, Utils.convertToUuidString(zipkinSpan.parentId())));
}
// Set Span Kind.
if (zipkinSpan.kind() != null) {
String kind = zipkinSpan.kind().toString().toLowerCase();
annotations.add(new Annotation("span.kind", kind));
if (zipkinSpan.annotations() != null && !zipkinSpan.annotations().isEmpty()) {
annotations.add(new Annotation("_spanSecondaryId", kind));
}
}
// Set Span's service name.
String serviceName = zipkinSpan.localServiceName() == null ? DEFAULT_SERVICE : zipkinSpan.localServiceName();
annotations.add(new Annotation(SERVICE_TAG_KEY, serviceName));
String applicationName = this.proxyLevelApplicationName;
String cluster = NULL_TAG_VAL;
String shard = NULL_TAG_VAL;
String componentTagValue = NULL_TAG_VAL;
boolean isError = false;
boolean isDebugSpanTag = false;
// Set all other Span Tags.
Set<String> ignoreKeys = new HashSet<>(ImmutableSet.of(SOURCE_KEY));
if (zipkinSpan.tags() != null && zipkinSpan.tags().size() > 0) {
for (Map.Entry<String, String> tag : zipkinSpan.tags().entrySet()) {
if (!ignoreKeys.contains(tag.getKey().toLowerCase()) && !StringUtils.isBlank(tag.getValue())) {
Annotation annotation = new Annotation(tag.getKey(), tag.getValue());
switch(annotation.getKey()) {
case APPLICATION_TAG_KEY:
applicationName = annotation.getValue();
continue;
case CLUSTER_TAG_KEY:
cluster = annotation.getValue();
continue;
case SHARD_TAG_KEY:
shard = annotation.getValue();
continue;
case COMPONENT_TAG_KEY:
componentTagValue = annotation.getValue();
break;
case ERROR_SPAN_TAG_KEY:
isError = true;
// Ignore the original error value
annotation.setValue(ERROR_SPAN_TAG_VAL);
break;
case DEBUG_TAG_KEY:
isDebugSpanTag = annotation.getValue().equals(DEBUG_SPAN_TAG_VAL);
break;
}
annotations.add(annotation);
}
}
}
// Add all wavefront indexed tags. These are set based on below hierarchy.
// Span Level > Proxy Level > Default
annotations.add(new Annotation(APPLICATION_TAG_KEY, applicationName));
annotations.add(new Annotation(CLUSTER_TAG_KEY, cluster));
annotations.add(new Annotation(SHARD_TAG_KEY, shard));
// Add Sampling related annotations.
// Add a debug span tag as needed to enable sampling of this span with intelligent sampling.
boolean isDebug = zipkinSpan.debug() != null ? zipkinSpan.debug() : false;
if (!isDebugSpanTag && isDebug) {
annotations.add(new Annotation(DEBUG_SPAN_TAG_KEY, DEBUG_SPAN_TAG_VAL));
}
// Add additional annotations.
if (zipkinSpan.localEndpoint() != null && zipkinSpan.localEndpoint().ipv4() != null) {
annotations.add(new Annotation("ipv4", zipkinSpan.localEndpoint().ipv4()));
}
if (!spanLogsDisabled.get() && zipkinSpan.annotations() != null && !zipkinSpan.annotations().isEmpty()) {
annotations.add(new Annotation("_spanLogs", "true"));
}
/* Add source of the span following the below:
* 1. If "source" is provided by span tags , use it else
* 2. Default "source" to "zipkin".
*/
String sourceName = DEFAULT_SOURCE;
if (zipkinSpan.tags() != null && zipkinSpan.tags().size() > 0) {
if (zipkinSpan.tags().get(SOURCE_KEY) != null) {
sourceName = zipkinSpan.tags().get(SOURCE_KEY);
}
}
// Set spanName.
String spanName = zipkinSpan.name() == null ? DEFAULT_SPAN_NAME : zipkinSpan.name();
String spanId = Utils.convertToUuidString(zipkinSpan.id());
String traceId = Utils.convertToUuidString(zipkinSpan.traceId());
// Build wavefront span
Span wavefrontSpan = Span.newBuilder().setCustomer("dummy").setName(spanName).setSource(sourceName).setSpanId(spanId).setTraceId(traceId).setStartMillis(zipkinSpan.timestampAsLong() / 1000).setDuration(zipkinSpan.durationAsLong() / 1000).setAnnotations(annotations).build();
if (zipkinSpan.tags().containsKey(SPAN_TAG_ERROR)) {
if (ZIPKIN_DATA_LOGGER.isLoggable(Level.FINER)) {
ZIPKIN_DATA_LOGGER.info("Span id :: " + spanId + " with trace id :: " + traceId + " , includes error tag :: " + zipkinSpan.tags().get(SPAN_TAG_ERROR));
}
}
// Log Zipkin spans as well as Wavefront spans for debugging purposes.
if (ZIPKIN_DATA_LOGGER.isLoggable(Level.FINEST)) {
ZIPKIN_DATA_LOGGER.info("Converted Wavefront span: " + wavefrontSpan.toString());
}
if (preprocessorSupplier != null) {
ReportableEntityPreprocessor preprocessor = preprocessorSupplier.get();
String[] messageHolder = new String[1];
preprocessor.forSpan().transform(wavefrontSpan);
if (!preprocessor.forSpan().filter(wavefrontSpan, messageHolder)) {
if (messageHolder[0] != null) {
spanHandler.reject(wavefrontSpan, messageHolder[0]);
} else {
spanHandler.block(wavefrontSpan);
}
return;
}
}
if (sampler.sample(wavefrontSpan, discardedSpansBySampler)) {
spanHandler.report(wavefrontSpan);
if (zipkinSpan.annotations() != null && !zipkinSpan.annotations().isEmpty() && !isFeatureDisabled(spanLogsDisabled, SPANLOGS_DISABLED, null)) {
SpanLogs spanLogs = SpanLogs.newBuilder().setCustomer("default").setTraceId(wavefrontSpan.getTraceId()).setSpanId(wavefrontSpan.getSpanId()).setSpanSecondaryId(zipkinSpan.kind() != null ? zipkinSpan.kind().toString().toLowerCase() : null).setLogs(zipkinSpan.annotations().stream().map(x -> SpanLog.newBuilder().setTimestamp(x.timestamp()).setFields(ImmutableMap.of("annotation", x.value())).build()).collect(Collectors.toList())).build();
spanLogsHandler.report(spanLogs);
}
}
// report stats irrespective of span sampling.
if (wfInternalReporter != null) {
// Set post preprocessor rule values and report converted metrics/histograms from the span
List<Annotation> processedAnnotations = wavefrontSpan.getAnnotations();
for (Annotation processedAnnotation : processedAnnotations) {
switch(processedAnnotation.getKey()) {
case APPLICATION_TAG_KEY:
applicationName = processedAnnotation.getValue();
continue;
case SERVICE_TAG_KEY:
serviceName = processedAnnotation.getValue();
continue;
case CLUSTER_TAG_KEY:
cluster = processedAnnotation.getValue();
continue;
case SHARD_TAG_KEY:
shard = processedAnnotation.getValue();
continue;
case COMPONENT_TAG_KEY:
componentTagValue = processedAnnotation.getValue();
continue;
case ERROR_TAG_KEY:
isError = true;
continue;
}
}
List<Pair<String, String>> spanTags = processedAnnotations.stream().map(a -> new Pair<>(a.getKey(), a.getValue())).collect(Collectors.toList());
discoveredHeartbeatMetrics.add(reportWavefrontGeneratedData(wfInternalReporter, wavefrontSpan.getName(), applicationName, serviceName, cluster, shard, wavefrontSpan.getSource(), componentTagValue, isError, zipkinSpan.durationAsLong(), traceDerivedCustomTagKeys, spanTags, true));
}
}
use of wavefront.report.Annotation in project java by wavefrontHQ.
the class JaegerProtobufUtils method processBatch.
public static void processBatch(Model.Batch batch, @Nullable StringBuilder output, String sourceName, String applicationName, ReportableEntityHandler<Span, String> spanHandler, ReportableEntityHandler<SpanLogs, String> spanLogsHandler, @Nullable WavefrontInternalReporter wfInternalReporter, Supplier<Boolean> traceDisabled, Supplier<Boolean> spanLogsDisabled, Supplier<ReportableEntityPreprocessor> preprocessorSupplier, SpanSampler sampler, Set<String> traceDerivedCustomTagKeys, Counter discardedTraces, Counter discardedBatches, Counter discardedSpansBySampler, Set<Pair<Map<String, String>, String>> discoveredHeartbeatMetrics, Counter receivedSpansTotal) {
String serviceName = batch.getProcess().getServiceName();
List<Annotation> processAnnotations = new ArrayList<>();
boolean isSourceProcessTagPresent = false;
String cluster = NULL_TAG_VAL;
String shard = NULL_TAG_VAL;
if (batch.getProcess().getTagsList() != null) {
for (Model.KeyValue tag : batch.getProcess().getTagsList()) {
if (tag.getKey().equals(APPLICATION_TAG_KEY) && tag.getVType() == Model.ValueType.STRING) {
applicationName = tag.getVStr();
continue;
}
if (tag.getKey().equals(CLUSTER_TAG_KEY) && tag.getVType() == Model.ValueType.STRING) {
cluster = tag.getVStr();
continue;
}
if (tag.getKey().equals(SHARD_TAG_KEY) && tag.getVType() == Model.ValueType.STRING) {
shard = tag.getVStr();
continue;
}
// "source" in span tag > "source" in process tag > "hostname" in process tag > DEFAULT
if (tag.getKey().equals("hostname") && tag.getVType() == Model.ValueType.STRING) {
if (!isSourceProcessTagPresent) {
sourceName = tag.getVStr();
}
continue;
}
if (tag.getKey().equals(SOURCE_KEY) && tag.getVType() == Model.ValueType.STRING) {
sourceName = tag.getVStr();
isSourceProcessTagPresent = true;
continue;
}
if (tag.getKey().equals(SERVICE_TAG_KEY) && tag.getVType() == Model.ValueType.STRING) {
// ignore "service" tags, since service is a field on the span
continue;
}
Annotation annotation = tagToAnnotation(tag);
processAnnotations.add(annotation);
}
}
if (isFeatureDisabled(traceDisabled, SPAN_DISABLED, discardedBatches, output)) {
discardedTraces.inc(batch.getSpansCount());
receivedSpansTotal.inc(batch.getSpansCount());
return;
}
receivedSpansTotal.inc(batch.getSpansCount());
for (Model.Span span : batch.getSpansList()) {
processSpan(span, serviceName, sourceName, applicationName, cluster, shard, processAnnotations, spanHandler, spanLogsHandler, wfInternalReporter, spanLogsDisabled, preprocessorSupplier, sampler, traceDerivedCustomTagKeys, discardedSpansBySampler, discoveredHeartbeatMetrics);
}
}
Aggregations