com.wavefront.agent.preprocessor.CountTransformer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of proxy-test Show documentation
Show all versions of proxy-test Show documentation
Service for batching and relaying metric traffic to Wavefront
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);
}
}
}