Skip to content

Commit

Permalink
Merge pull request #9 from kenshoo/customizable-filter
Browse files Browse the repository at this point in the history
Resolve Issue #4: customizable filters + switch to builder API
  • Loading branch information
tzachz authored Apr 23, 2017
2 parents 3a0681b + bbd9f7c commit 971e89d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.codahale.metrics.MetricRegistry;
import com.kenshoo.metrics.anodot.*;
import com.kenshoo.metrics.anodot.metrics3.Anodot3ReporterFactory;
import com.kenshoo.metrics.anodot.metrics3.Anodot3ReporterBuilder;

/**
* Created by tzachz on 4/21/17
Expand All @@ -19,32 +19,20 @@ public static void main(String[] args) {
// It might already contain some metrics:
metricRegistry.counter("some.counter").inc();

// Create an AnodotGlobalProperties object with your global properties;
// These properties will be added to each reported metric
//
// You can use the supplied DefaultAnodotGlobalProperties which adds version / build / server properties
// Overloads for constructor with fewer arguments exist -
// For example, if you want the server name to be inferred automatically
//
// Alternatively, you can create your own implementation of AnodotGlobalProperties with the properties of your choice,
// or use the EmptyAnodotGlobalProperties if you wish no global properties to be added
final AnodotGlobalProperties globalProperties = new DefaultAnodotGlobalProperties(
/* version */ "1.0.1",
/* build */ "b2232",
/* server */ "server1.prod");

// Create Anodot configuration with Token, Interval (seconds) and URI
final AnodotReporterConfiguration anodotConf = new DefaultAnodotReporterConfiguration(
/* token */ "fake-token",
/* token */ "your-token",
/* interval */ 60,
/* Anodot URI */ "https://api.anodot.com/api/v1/metrics");

// Create the reporter factory based on the given configuration.
final Anodot3ReporterFactory reporterFactory = new Anodot3ReporterFactory(anodotConf, globalProperties);

// Create the reporter "wrapper" which provides a simple API for starting / stopping the reporting
// All metrics added to metricsRegistry (before or after reporter was created) will be reported to Anodot
final AnodotReporterWrapper reporter = reporterFactory.anodot3Reporter(metricRegistry);
// Use builder optional methods to customize your reporter
final AnodotReporterWrapper reporter = Anodot3ReporterBuilder.builderFor(anodotConf)
// .withGlobalProperties(/* any AnodotGlobalProperties */ ) // you can add global properties to be added to
// .addFilter(/* any AnodotMetricFilter */) // you can add your custom filters
// .turnZeroFilterOff() // by default, a filter prevents always-zero metrics from being sent; This disables it
.build(metricRegistry);

reporter.start(); // will now start reporting

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.kenshoo.metrics.anodot.metrics2;

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.MetricsRegistry;

import java.util.List;

/**
* Created by tzachz on 4/21/17
*/
public class Anodot2ReporterBuilder {

private static final Anodot2NonZeroFilter NON_ZERO_FILTER = new Anodot2NonZeroFilter();

private final AnodotReporterConfiguration conf;
private final List<AnodotMetricFilter> filters = Lists.<AnodotMetricFilter>newArrayList(NON_ZERO_FILTER);
private AnodotGlobalProperties globalProperties = new EmptyAnodotGlobalProperties();

public static Anodot2ReporterBuilder builderFor(AnodotReporterConfiguration conf) {
return new Anodot2ReporterBuilder(conf);
}

public Anodot2ReporterBuilder withGlobalProperties(AnodotGlobalProperties globalProperties) {
this.globalProperties = globalProperties;
return this;
}

public Anodot2ReporterBuilder addFilter(AnodotMetricFilter filter) {
this.filters.add(filter);
return this;
}

public Anodot2ReporterBuilder turnZeroFilterOff() {
this.filters.remove(NON_ZERO_FILTER);
return this;
}

public AnodotReporterWrapper build(MetricsRegistry metricRegistry) {
return new Anodot2ReporterFactory(conf, globalProperties).anodot2Reporter(metricRegistry);

}
private Anodot2ReporterBuilder(AnodotReporterConfiguration conf) {
this.conf = conf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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.Anodot;
import com.yammer.metrics.core.AnodotMetricRegistry;
import com.yammer.metrics.core.AnodotReporter;
Expand All @@ -14,21 +13,17 @@
/**
* Created by tzachz on 2/21/17
*/
public class Anodot2ReporterFactory {
class Anodot2ReporterFactory {

private final AnodotReporterConfiguration conf;
private final AnodotGlobalProperties globalProperties;

public Anodot2ReporterFactory(AnodotReporterConfiguration conf) {
this(conf, new EmptyAnodotGlobalProperties());
}

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

public AnodotReporterWrapper anodot2Reporter(MetricsRegistry metricRegistry) {
AnodotReporterWrapper anodot2Reporter(MetricsRegistry metricRegistry) {
final Anodot2MetricNameConverter converter = new Anodot2MetricNameConverter(globalProperties);
final Anodot2RegistryFactory registryFactory = new Anodot2RegistryFactory(converter);
final AnodotMetricRegistry anodotMetricRegistry = registryFactory.anodot2Registry(metricRegistry);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.kenshoo.metrics.anodot.metrics3;

import com.anodot.metrics.AnodotMetricFilter;
import com.codahale.metrics.MetricRegistry;
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 java.util.List;

/**
* Created by tzachz on 4/21/17
*/
public class Anodot3ReporterBuilder {

private static final Anodot3NonZeroFilter NON_ZERO_FILTER = new Anodot3NonZeroFilter();

private final AnodotReporterConfiguration conf;
private final List<AnodotMetricFilter> filters = Lists.<AnodotMetricFilter>newArrayList(NON_ZERO_FILTER);
private AnodotGlobalProperties globalProperties = new EmptyAnodotGlobalProperties();

public static Anodot3ReporterBuilder builderFor(AnodotReporterConfiguration conf) {
return new Anodot3ReporterBuilder(conf);
}

public Anodot3ReporterBuilder withGlobalProperties(AnodotGlobalProperties globalProperties) {
this.globalProperties = globalProperties;
return this;
}

public Anodot3ReporterBuilder addFilter(AnodotMetricFilter filter) {
this.filters.add(filter);
return this;
}

public Anodot3ReporterBuilder turnZeroFilterOff() {
this.filters.remove(NON_ZERO_FILTER);
return this;
}

public AnodotReporterWrapper build(MetricRegistry metricRegistry) {
return new Anodot3ReporterFactory(conf, globalProperties).anodot3Reporter(metricRegistry);

}
private Anodot3ReporterBuilder(AnodotReporterConfiguration conf) {
this.conf = conf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,23 @@
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 java.util.concurrent.TimeUnit;

/**
* Created by tzachz on 2/21/17
*/
public class Anodot3ReporterFactory {
class Anodot3ReporterFactory {

private final AnodotReporterConfiguration conf;
private final AnodotGlobalProperties globalProperties;

public Anodot3ReporterFactory(AnodotReporterConfiguration conf) {
this(conf, new EmptyAnodotGlobalProperties());
}

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

public AnodotReporterWrapper anodot3Reporter(MetricRegistry metricRegistry) {
AnodotReporterWrapper anodot3Reporter(MetricRegistry metricRegistry) {
final Anodot3MetricNameConverter converter = new Anodot3MetricNameConverter(globalProperties);
final Anodot3RegistryFactory registryFactory = new Anodot3RegistryFactory(converter);
final AnodotMetricRegistry anodotMetricRegistry = registryFactory.anodot3Registry(metricRegistry);
Expand Down

0 comments on commit 971e89d

Please sign in to comment.