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

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

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

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import java.util.function.Predicate;
import javax.annotation.Nullable;

/**
 * A no-op rule that simply counts points or spans or logs. Optionally, can count only
 * points/spans/logs matching the {@code if} predicate.
 *
 * @author [email protected]
 */
public class CountTransformer implements Function {

  private final PreprocessorRuleMetrics ruleMetrics;
  private final Predicate v2Predicate;

  public CountTransformer(
      @Nullable final Predicate v2Predicate, final PreprocessorRuleMetrics ruleMetrics) {
    Preconditions.checkNotNull(ruleMetrics, "PreprocessorRuleMetrics can't be null");
    this.ruleMetrics = ruleMetrics;
    this.v2Predicate = v2Predicate != null ? v2Predicate : x -> true;
  }

  @Nullable
  @Override
  public T apply(@Nullable T input) {
    if (input == null) return null;
    long startNanos = ruleMetrics.ruleStart();
    try {
      if (v2Predicate.test(input)) {
        ruleMetrics.incrementRuleAppliedCounter();
      }
      return input;
    } finally {
      ruleMetrics.ruleEnd(startNanos);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy