All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wavefront.agent.preprocessor.SpanAddAnnotationIfNotExistsTransformer Maven / Gradle / Ivy

There is a newer version: 4.36
Show newest version
package com.wavefront.agent.preprocessor;

import static com.wavefront.predicates.Util.expandPlaceholders;

import java.util.function.Predicate;
import javax.annotation.Nullable;
import wavefront.report.Annotation;
import wavefront.report.Span;

/**
 * Creates a new annotation with a specified key/value pair. If such point tag already exists, the
 * value won't be overwritten.
 *
 * @author [email protected]
 */
public class SpanAddAnnotationIfNotExistsTransformer extends SpanAddAnnotationTransformer {

  public SpanAddAnnotationIfNotExistsTransformer(
      final String key,
      final String value,
      @Nullable final Predicate v2Predicate,
      final PreprocessorRuleMetrics ruleMetrics) {
    super(key, value, v2Predicate, ruleMetrics);
  }

  @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);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy