Skip to content

Commit 9e705c8

Browse files
odin-delriosnicoll
authored andcommitted
Fix statsd metrics collection for names with ":"
Statsd server is ignoring malformed metrics. This change introduces a basic sanitizing to metric names for avoid losing those metrics. See gh-8906
1 parent cdf3ead commit 9e705c8

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/statsd/StatsdMetricWriter.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* a gauge.
4040
*
4141
* @author Dave Syer
42+
* @author Odín del Río
4243
* @since 1.3.0
4344
*/
4445
public class StatsdMetricWriter implements MetricWriter, Closeable {
@@ -87,12 +88,12 @@ private static String trimPrefix(String prefix) {
8788

8889
@Override
8990
public void increment(Delta<?> delta) {
90-
this.client.count(delta.getName(), delta.getValue().longValue());
91+
this.client.count(sanitizeMetricName(delta.getName()), delta.getValue().longValue());
9192
}
9293

9394
@Override
9495
public void set(Metric<?> value) {
95-
String name = value.getName();
96+
String name = sanitizeMetricName(value.getName());
9697
if (name.contains("timer.") && !name.contains("gauge.")
9798
&& !name.contains("counter.")) {
9899
this.client.recordExecutionTime(name, value.getValue().longValue());
@@ -117,6 +118,16 @@ public void close() {
117118
this.client.stop();
118119
}
119120

121+
/**
122+
* The statsd server does not allow ":" in metric names. Since the the statsd client
123+
* is not dealing with this, we have to sanitize the metric name.
124+
* @param name The metric name
125+
* @return The sanitized metric name
126+
*/
127+
private String sanitizeMetricName(String name) {
128+
return name.replace(":", "");
129+
}
130+
120131
private static final class LoggingStatsdErrorHandler
121132
implements StatsDClientErrorHandler {
122133

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/statsd/StatsdMetricWriterTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Tests for {@link StatsdMetricWriter}.
3737
*
3838
* @author Dave Syer
39+
* @author Odín del Río
3940
*/
4041
public class StatsdMetricWriterTests {
4142

@@ -97,6 +98,20 @@ public void periodPrefix() throws Exception {
9798
assertThat(this.server.messagesReceived().get(0)).isEqualTo("my.gauge.foo:3|g");
9899
}
99100

101+
@Test
102+
public void incrementMetricWithInvalidCharsInName() throws Exception {
103+
this.writer.increment(new Delta<>("counter.fo:o", 3L));
104+
this.server.waitForMessage();
105+
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.counter.foo:3|c");
106+
}
107+
108+
@Test
109+
public void setMetricWithInvalidCharsInName() throws Exception {
110+
this.writer.set(new Metric<>("gauge.f:o:o", 3L));
111+
this.server.waitForMessage();
112+
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.gauge.foo:3|g");
113+
}
114+
100115
private static final class DummyStatsDServer implements Runnable {
101116

102117
private final List<String> messagesReceived = new ArrayList<String>();

0 commit comments

Comments
 (0)