Skip to content

Commit

Permalink
Merge pull request #10 from kenshoo/fix-filter-customization
Browse files Browse the repository at this point in the history
Integration tests + fixing filter setup
  • Loading branch information
erezlotan authored Apr 25, 2017
2 parents bd237ef + 3210291 commit 352ff76
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 10 deletions.
1 change: 1 addition & 0 deletions anodot-reporter2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencies {

testCompile libraries.junit
testCompile libraries.hamcrestLibrary
testCompile libraries.wiremock
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public boolean matches(MetricName metricName, Metric metric) {
if (value instanceof Float) return (Float) value != 0;
if (value instanceof Double) return (Double) value != 0;
else return true;
} else if (metric instanceof Counter) {
return ((Counter)metric).count() != 0;
} else if (metric instanceof Metered) {
return ((Metered)metric).count() != 0;
} else if (metric instanceof Histogram) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.kenshoo.metrics.anodot.metrics2;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.kenshoo.metrics.anodot.AnodotGlobalProperties;
import com.kenshoo.metrics.anodot.AnodotReporterConfiguration;
import com.kenshoo.metrics.anodot.AnodotReporterWrapper;
import com.kenshoo.metrics.anodot.EmptyAnodotGlobalProperties;
import com.yammer.metrics.core.AnodotMetricFilter;
import com.yammer.metrics.core.Metric;
import com.yammer.metrics.core.MetricsRegistry;
import com.yammer.metrics.core.spec.MetricName;

import java.util.List;

import static com.google.common.collect.FluentIterable.from;

/**
* Created by tzachz on 4/21/17
*/
Expand Down Expand Up @@ -41,9 +47,25 @@ public Anodot2ReporterBuilder turnZeroFilterOff() {
}

public AnodotReporterWrapper build(MetricsRegistry metricRegistry) {
return new Anodot2ReporterFactory(conf, globalProperties).anodot2Reporter(metricRegistry);
final AnodotMetricFilter filter = composeFiltersIntoOne();
return new Anodot2ReporterFactory(conf, globalProperties, filter).anodot2Reporter(metricRegistry);
}

private AnodotMetricFilter composeFiltersIntoOne() {
final ImmutableList<AnodotMetricFilter> filtersImmutable = ImmutableList.copyOf(filters);
return new AnodotMetricFilter() {
@Override
public boolean matches(final MetricName metricName, final Metric metric) {
return from(filtersImmutable).allMatch(new Predicate<AnodotMetricFilter>() {
@Override
public boolean apply(AnodotMetricFilter input) {
return input.matches(metricName, metric);
}
});
}
};
}

private Anodot2ReporterBuilder(AnodotReporterConfiguration conf) {
this.conf = conf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import com.kenshoo.metrics.anodot.AnodotGlobalProperties;
import com.kenshoo.metrics.anodot.AnodotReporterConfiguration;
import com.kenshoo.metrics.anodot.AnodotReporterWrapper;
import com.yammer.metrics.core.Anodot;
import com.yammer.metrics.core.AnodotMetricRegistry;
import com.yammer.metrics.core.AnodotReporter;
import com.yammer.metrics.core.MetricsRegistry;
import com.yammer.metrics.core.*;

import java.util.concurrent.TimeUnit;

Expand All @@ -17,10 +14,12 @@ class Anodot2ReporterFactory {

private final AnodotReporterConfiguration conf;
private final AnodotGlobalProperties globalProperties;
private final AnodotMetricFilter filter;

Anodot2ReporterFactory(AnodotReporterConfiguration conf, AnodotGlobalProperties globalProperties) {
Anodot2ReporterFactory(AnodotReporterConfiguration conf, AnodotGlobalProperties globalProperties, AnodotMetricFilter filter) {
this.conf = conf;
this.globalProperties = globalProperties;
this.filter = filter;
}

AnodotReporterWrapper anodot2Reporter(MetricsRegistry metricRegistry) {
Expand All @@ -32,7 +31,7 @@ AnodotReporterWrapper anodot2Reporter(MetricsRegistry metricRegistry) {

private AnodotReporterWrapper anodot2Reporter(AnodotMetricRegistry anodot2Registry) {
final AnodotReporter reporter = AnodotReporter.forRegistry(anodot2Registry)
.filter(new Anodot2NonZeroFilter())
.filter(filter)
.build(new Anodot(conf.getUri(), conf.getToken()));

return new AnodotReporterWrapper() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.kenshoo.metrics.anodot.metrics2;

import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;
import com.yammer.metrics.core.Gauge;
import com.yammer.metrics.core.spec.MetricName;
import org.junit.Test;
Expand Down Expand Up @@ -32,6 +34,13 @@ public void nonZeroGaugesNotFilteredOut() throws Exception {
assertThat(filter.matches(METRIC_NAME, gauge("1")), is(true));
}

@Test
public void nonZeroCounterNotFilteredOut() throws Exception {
final Counter counter = Metrics.newCounter(this.getClass(), "counter1");
counter.inc();
assertThat(filter.matches(METRIC_NAME, counter), is(true));
}

private <T> Gauge<T> gauge(final T value) {
return new Gauge<T>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.kenshoo.metrics.anodot.metrics2;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.kenshoo.metrics.anodot.AnodotReporterConfiguration;
import com.kenshoo.metrics.anodot.AnodotReporterWrapper;
import com.kenshoo.metrics.anodot.DefaultAnodotReporterConfiguration;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.AnodotMetricFilter;
import com.yammer.metrics.core.Metric;
import com.yammer.metrics.core.spec.MetricName;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

/**
* Created by tzachz on 4/24/17
*/
public class Anodot2ReporterBuilderTest {

private final AnodotReporterConfiguration conf = new DefaultAnodotReporterConfiguration("t", 1, "http://localhost:8080/anodot");

@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);

@Test
public void zeroesNotReportedByDefault() throws Exception {
Metrics.newCounter(this.getClass(), "counter3333"); // create zero counter
final AnodotReporterWrapper reporter = Anodot2ReporterBuilder.builderFor(conf).build(Metrics.defaultRegistry());
runReportCycle(reporter);
verify(exactly(0), postRequestedFor(urlEqualTo("/anodot?token=t")).withRequestBody(containing("counter3333")));
}

@Test
public void zeroFilterTurnedOffMeansNoFilters() throws Exception {
Metrics.newCounter(this.getClass(), "counter"); // create zero counter
final AnodotReporterWrapper reporter = Anodot2ReporterBuilder.builderFor(conf).turnZeroFilterOff().build(Metrics.defaultRegistry());
runReportCycle(reporter);
verify(postRequestedFor(urlEqualTo("/anodot?token=t")));
}

@Test
public void mustPassAllFilters() throws Exception {
Metrics.newCounter(this.getClass(), "counter1111").inc();
Metrics.newCounter(this.getClass(), "counter2222").inc();
Metrics.newCounter(this.getClass(), "meter1111").inc();

final AnodotReporterWrapper reporter = Anodot2ReporterBuilder.builderFor(conf)
.addFilter(whatPropertyContains("count")) // both counters pass this one
.addFilter(whatPropertyContains("1111")) // but only the first passes this one
.build(Metrics.defaultRegistry());

runReportCycle(reporter);
verify(postRequestedFor(urlEqualTo("/anodot?token=t"))
.withRequestBody(containing("counter1111"))
.withRequestBody(notMatching(".*counter2222.*"))
.withRequestBody(notMatching(".*meter1111.*"))
);
}

private AnodotMetricFilter whatPropertyContains(final String substring) {
return new AnodotMetricFilter() {
@Override
public boolean matches(MetricName metricName, Metric metric) {
return metricName.getProperty("what").getValue().contains(substring);
}
};
}

private void runReportCycle(AnodotReporterWrapper reporter) throws InterruptedException {
reporter.start();
Thread.sleep(1300);
reporter.stop();
}
}
1 change: 1 addition & 0 deletions anodot-reporter3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dependencies {

testCompile libraries.junit
testCompile libraries.hamcrestLibrary
testCompile libraries.wiremock
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.kenshoo.metrics.anodot.metrics3;

import com.anodot.metrics.AnodotMetricFilter;
import com.anodot.metrics.spec.MetricName;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.kenshoo.metrics.anodot.AnodotGlobalProperties;
import com.kenshoo.metrics.anodot.AnodotReporterConfiguration;
Expand All @@ -10,6 +14,8 @@

import java.util.List;

import static com.google.common.collect.FluentIterable.from;

/**
* Created by tzachz on 4/21/17
*/
Expand Down Expand Up @@ -41,9 +47,25 @@ public Anodot3ReporterBuilder turnZeroFilterOff() {
}

public AnodotReporterWrapper build(MetricRegistry metricRegistry) {
return new Anodot3ReporterFactory(conf, globalProperties).anodot3Reporter(metricRegistry);
final AnodotMetricFilter filter = composeFiltersIntoOne();
return new Anodot3ReporterFactory(conf, globalProperties, filter).anodot3Reporter(metricRegistry);
}

private AnodotMetricFilter composeFiltersIntoOne() {
final ImmutableList<AnodotMetricFilter> filtersImmutable = ImmutableList.copyOf(filters);
return new AnodotMetricFilter() {
@Override
public boolean matches(final MetricName metricName, final Metric metric) {
return from(filtersImmutable).allMatch(new Predicate<AnodotMetricFilter>() {
@Override
public boolean apply(AnodotMetricFilter input) {
return input.matches(metricName, metric);
}
});
}
};
}

private Anodot3ReporterBuilder(AnodotReporterConfiguration conf) {
this.conf = conf;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kenshoo.metrics.anodot.metrics3;

import com.anodot.metrics.Anodot;
import com.anodot.metrics.AnodotMetricFilter;
import com.anodot.metrics.AnodotMetricRegistry;
import com.anodot.metrics.AnodotReporter;
import com.codahale.metrics.MetricRegistry;
Expand All @@ -17,10 +18,12 @@ class Anodot3ReporterFactory {

private final AnodotReporterConfiguration conf;
private final AnodotGlobalProperties globalProperties;
private final AnodotMetricFilter filter;

Anodot3ReporterFactory(AnodotReporterConfiguration conf, AnodotGlobalProperties globalProperties) {
Anodot3ReporterFactory(AnodotReporterConfiguration conf, AnodotGlobalProperties globalProperties, AnodotMetricFilter filter) {
this.conf = conf;
this.globalProperties = globalProperties;
this.filter = filter;
}

AnodotReporterWrapper anodot3Reporter(MetricRegistry metricRegistry) {
Expand All @@ -33,7 +36,7 @@ AnodotReporterWrapper anodot3Reporter(MetricRegistry metricRegistry) {
private AnodotReporterWrapper anodot3Reporter(AnodotMetricRegistry anodotRegistry) {
final AnodotReporter reporter = AnodotReporter
.forRegistry(anodotRegistry)
.filter(new Anodot3NonZeroFilter())
.filter(filter)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.convertRatesTo(TimeUnit.SECONDS)
.build(new Anodot(conf.getUri(), conf.getToken()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.kenshoo.metrics.anodot.metrics3;

import com.anodot.metrics.AnodotMetricFilter;
import com.anodot.metrics.spec.MetricName;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.kenshoo.metrics.anodot.AnodotReporterConfiguration;
import com.kenshoo.metrics.anodot.AnodotReporterWrapper;
import com.kenshoo.metrics.anodot.DefaultAnodotReporterConfiguration;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

/**
* Created by tzachz on 4/24/17
*/
public class Anodot3ReporterBuilderTest {

private final AnodotReporterConfiguration conf = new DefaultAnodotReporterConfiguration("t", 1, "http://localhost:8080/anodot");

private final MetricRegistry registry = new MetricRegistry();

@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);

@Test
public void zeroesNotReportedByDefault() throws Exception {
registry.counter("counter"); // create zero counter
final AnodotReporterWrapper reporter = Anodot3ReporterBuilder.builderFor(conf).build(registry);
runReportCycle(reporter);
verify(exactly(0), postRequestedFor(urlEqualTo("/anodot?token=t")));
}

@Test
public void zeroFilterTurnedOffMeansNoFilters() throws Exception {
registry.counter("counter"); // create zero counter
final AnodotReporterWrapper reporter = Anodot3ReporterBuilder.builderFor(conf).turnZeroFilterOff().build(registry);
runReportCycle(reporter);
verify(postRequestedFor(urlEqualTo("/anodot?token=t")));
}

@Test
public void mustPassAllFilters() throws Exception {
registry.counter("counter1111").inc();
registry.counter("counter2222").inc();
registry.counter("meter1111").inc();

final AnodotReporterWrapper reporter = Anodot3ReporterBuilder.builderFor(conf)
.addFilter(whatPropertyContains("count")) // both counters pass this one
.addFilter(whatPropertyContains("1111")) // but only the first passes this one
.build(registry);

runReportCycle(reporter);
verify(postRequestedFor(urlEqualTo("/anodot?token=t"))
.withRequestBody(containing("counter1111"))
.withRequestBody(notMatching(".*counter2222.*"))
.withRequestBody(notMatching(".*meter1111.*"))
);
}

private AnodotMetricFilter whatPropertyContains(final String substring) {
return new AnodotMetricFilter() {
@Override
public boolean matches(MetricName metricName, Metric metric) {
return metricName.getProperty("what").getValue().contains(substring);
}
};
}

private void runReportCycle(AnodotReporterWrapper reporter) throws InterruptedException {
reporter.start();
Thread.sleep(1500);
reporter.stop();
}

}
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ project.libraries = [
//Test libs
junit : 'junit:junit-dep:4.11',
hamcrestLibrary : 'org.hamcrest:hamcrest-library:1.3',
wiremock : 'com.github.tomakehurst:wiremock:2.6.0',

]

0 comments on commit 352ff76

Please sign in to comment.