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: 9999.0
Show newest version
package com.wavefront.agent.preprocessor;

import javax.annotation.Nullable;
import java.util.function.Predicate;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;

/**
 * A no-op rule that simply counts points or spans. Optionally, can count only
 * points/spans 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