Filters
Sometimes you don’t want to report all of the telemetry data. This is when filtering comes in handy. With filtering you set up rules for what format the data should fulfill to be reported. Each reporter can have its own filter.
Default filter
The default filter passes all metric keys through to the reporter. This default filter will be used if no configuration for filter is specified.
You can also explicitly declare that you want it to be used by adding the following to the configuration file:
<your_reporter>.filter = default-filter
Filter API
It is also possible to define your own filter, to only report specific metrics. To enable this we provide an interface that you must implement, to create a Coda Hale MetricFilter
:
public interface Filter {
MetricFilter getMetricFilter();
}
The constructor for your filter class can optionally accept a Config
object.
Custom filter example
Let’s pretend that you only would like to report data containing the word “apple” because you like apples a lot. To do so, implement the method above to create a MetricFilter
, which can match the name in whichever way you like:
package sample.reporter;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricFilter;
import com.lightbend.cinnamon.chmetrics.reporter.Filter;
import com.typesafe.config.Config;
public class AppleFilter implements Filter {
private static final String Apple = "apple";
public AppleFilter(final Config config) {
// parse any specific parameters set in config
}
@Override
public MetricFilter getMetricFilter() {
return new MetricFilter() {
@Override
public boolean matches(String name, Metric metric) {
return name.contains(Apple);
}
};
}
}
To use this filter, configure the reporter with the filter and the fully qualified class name for the filter class. For example:
cinnamon.chmetrics {
// Use the Console reporter
reporters += console-reporter
// Override the default filter in Console reporter by specifying your own
console-reporter {
filter = apple-filter
// Define filter specific details (filter-class is mandatory)
apple-filter {
filter-class = "sample.reporter.AppleFilter"
}
}
}
Now the reporter only accepts metrics with keys containing “apple”. For example, the reporter will report metrics for keys like:
"metrics.apples.count"
But it won’t report metrics for keys like:
"metrics.oranges.count"