Skip to content

Commit

Permalink
Merge pull request #36 from Peppi-Ressler/statsd-builder
Browse files Browse the repository at this point in the history
added statsd builder
  • Loading branch information
tomasherman authored Oct 9, 2018
2 parents b645adf + 3ac264f commit 76856f6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public StatsDMetricsMonitor(String host, int port, String prefix, final Naming n
this(host, port, prefix, naming, gaugeSendPeriod, scheduler, MetricsFilter.ALL_ENABLED);
}

/**
* @deprecated This constructor is only for backward compatibility and should be no longer used.
* It internally builds an instance of scheduler that will be never shutted down and causes threads leak.
* Prefer to use builder or other constructors.
*/
@Deprecated
public StatsDMetricsMonitor(String host, int port, String prefix, final Naming naming) {
this(host, port, prefix, naming, getDefaultGaugeSendPeriod(), createScheduler());
}
Expand All @@ -58,11 +64,22 @@ public StatsDMetricsMonitor(String host, int port, String prefix, final Duration
this(host, port, prefix, Naming.defaultNaming(), gaugeSendPeriod, scheduler);
}

/**
* @deprecated This constructor is only for backward compatibility and should be no longer used.
* It internally builds an instance of scheduler that will be never shutted down and causes threads leak.
* Prefer to use builder or other constructors.
*/
@Deprecated
public StatsDMetricsMonitor(String host, int port, String prefix) {
this(host, port, prefix, Naming.defaultNaming(), getDefaultGaugeSendPeriod(), createScheduler());
}


/**
* @deprecated This constructor is only for backward compatibility and should be no longer used.
* It internally builds an instance of scheduler that will be never shutted down and causes threads leak.
* Prefer to use builder or other constructors.
*/
@Deprecated
public StatsDMetricsMonitor(String host, int port, String prefix, MetricsFilter metricsFilter) {
this(host, port, prefix, Naming.defaultNaming(), getDefaultGaugeSendPeriod(), createScheduler(), metricsFilter);
}
Expand Down Expand Up @@ -218,7 +235,6 @@ public boolean isAutoRegisterMetric() {

@Override
public void close() {
scheduler.shutdown();
client.stop();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.avast.metrics.statsd;

import com.avast.metrics.api.Naming;
import com.avast.metrics.filter.MetricsFilter;

import java.time.Duration;
import java.util.concurrent.ScheduledExecutorService;

public class StatsDMetricsMonitorBuilder {
private final String host;
private final int port;
private final String prefix;
private final ScheduledExecutorService scheduler;
private boolean autoRegisterMetrics = false;
private Naming naming = Naming.defaultNaming();
private Duration gaugeSendPeriod = Duration.ofSeconds(1);
private MetricsFilter metricsFilter = MetricsFilter.ALL_ENABLED;

public StatsDMetricsMonitorBuilder(String host, int port, String prefix, final ScheduledExecutorService scheduler) {
this.host = host;
this.port = port;
this.prefix = prefix;
this.scheduler = scheduler;
}

public StatsDMetricsMonitorBuilder withAutoRegisterMetrics(boolean autoRegisterMetrics) {
this.autoRegisterMetrics = autoRegisterMetrics;
return this;
}

public StatsDMetricsMonitorBuilder withNaming(Naming naming) {
this.naming = naming;
return this;
}

public StatsDMetricsMonitorBuilder withGaugeSendPeriod(Duration gaugeSendPeriod) {
this.gaugeSendPeriod = gaugeSendPeriod;
return this;
}

public StatsDMetricsMonitorBuilder withMetricsFilter(MetricsFilter metricsFilter) {
this.metricsFilter = metricsFilter;
return this;
}

public StatsDMetricsMonitor build() {
return new StatsDMetricsMonitor(host, port, autoRegisterMetrics, prefix, naming, gaugeSendPeriod, scheduler, metricsFilter);
}
}

0 comments on commit 76856f6

Please sign in to comment.