Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sanitizeKey property #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
public class StatsDReporter extends ScheduledReporter {
private static final Logger LOG = LoggerFactory.getLogger(StatsDReporter.class);

private final boolean sanitizeKey;
private final StatsD statsD;
private final String prefix;

Expand All @@ -55,10 +56,12 @@ private StatsDReporter(final MetricRegistry registry,
final String prefix,
final TimeUnit rateUnit,
final TimeUnit durationUnit,
final MetricFilter filter) {
final MetricFilter filter,
final boolean sanitizeKey) {
super(registry, "statsd-reporter", filter, rateUnit, durationUnit);
this.statsD = statsD;
this.prefix = prefix;
this.sanitizeKey = sanitizeKey;
}

/**
Expand All @@ -79,6 +82,7 @@ public static Builder forRegistry(final MetricRegistry registry) {
@NotThreadSafe
public static final class Builder {
private final MetricRegistry registry;
private boolean sanitizeKey;
private String prefix;
private TimeUnit rateUnit;
private TimeUnit durationUnit;
Expand All @@ -90,6 +94,17 @@ private Builder(final MetricRegistry registry) {
this.rateUnit = TimeUnit.SECONDS;
this.durationUnit = TimeUnit.MILLISECONDS;
this.filter = MetricFilter.ALL;
this.sanitizeKey = false;
}

/**
* Sanitize metric key, replace incorrect symbols with -.
*
* @return {@code this}
*/
public Builder sanitizeKey() {
this.sanitizeKey = true;
return this;
}

/**
Expand Down Expand Up @@ -155,7 +170,7 @@ public StatsDReporter build(final String host, final int port) {
* @return a {@link StatsDReporter}
*/
public StatsDReporter build(final StatsD statsD) {
return new StatsDReporter(registry, statsD, prefix, rateUnit, durationUnit, filter);
return new StatsDReporter(registry, statsD, prefix, rateUnit, durationUnit, filter, sanitizeKey);
}
}

Expand Down Expand Up @@ -275,8 +290,16 @@ private String format(final Object o) {
return null;
}

private String[] sanitize(final String... components) {
final String[] sanitized = new String[components.length];
for(int i = 0; i < components.length; i++) {
sanitized[i] = components[i].replaceAll("[^a-zA-Z_\\-0-9\\.]", "-");
}
return sanitized;
}

private String prefix(final String... components) {
return MetricRegistry.name(prefix, components);
return MetricRegistry.name(prefix, sanitizeKey ? sanitize(components) : components);
}

private String formatNumber(final BigInteger n) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.Test;
import org.mockito.InOrder;

import java.io.IOException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -204,6 +205,25 @@ public void reportsMeters() throws Exception {

}

@Test
public void replacesIncorrectSymbolsInMetricName() throws IOException {
final StatsDReporter sanitizedReporter = StatsDReporter.forRegistry(registry)
.prefixedWith("prefix")
.convertRatesTo(TimeUnit.SECONDS)
.convertDurationsTo(TimeUnit.MILLISECONDS)
.filter(MetricFilter.ALL)
.sanitizeKey()
.build(statsD);

sanitizedReporter.report(map("gauge.me:tr#ic", gauge((byte) 1)), this.<Counter>map(), this.<Histogram>map(), this.<Meter>map(),
this.<Timer>map());

final InOrder inOrder = inOrder(statsD);
inOrder.verify(statsD).connect();
inOrder.verify(statsD).send("prefix.gauge.me-tr-ic", "1");
inOrder.verify(statsD).close();
}

@Test
public void reportsTimers() throws Exception {
final Timer timer = mock(Timer.class);
Expand Down