Skip to content

Commit 9a5346f

Browse files
committed
Polish "Fix statsd metrics collection for names with ":""
Closes gh-8906
1 parent 9e705c8 commit 9a5346f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ private static String trimPrefix(String prefix) {
8888

8989
@Override
9090
public void increment(Delta<?> delta) {
91-
this.client.count(sanitizeMetricName(delta.getName()), delta.getValue().longValue());
91+
this.client.count(sanitizeMetricName(delta.getName()),
92+
delta.getValue().longValue());
9293
}
9394

9495
@Override
@@ -119,13 +120,12 @@ public void close() {
119120
}
120121

121122
/**
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.
123+
* Sanitize the metric name if necessary.
124124
* @param name The metric name
125125
* @return The sanitized metric name
126126
*/
127127
private String sanitizeMetricName(String name) {
128-
return name.replace(":", "");
128+
return name.replace(":", "-");
129129
}
130130

131131
private static final class LoggingStatsdErrorHandler

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,16 +100,16 @@ public void periodPrefix() throws Exception {
100100

101101
@Test
102102
public void incrementMetricWithInvalidCharsInName() throws Exception {
103-
this.writer.increment(new Delta<>("counter.fo:o", 3L));
103+
this.writer.increment(new Delta<Long>("counter.fo:o", 3L));
104104
this.server.waitForMessage();
105-
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.counter.foo:3|c");
105+
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.counter.fo-o:3|c");
106106
}
107107

108108
@Test
109109
public void setMetricWithInvalidCharsInName() throws Exception {
110-
this.writer.set(new Metric<>("gauge.f:o:o", 3L));
110+
this.writer.set(new Metric<Long>("gauge.f:o:o", 3L));
111111
this.server.waitForMessage();
112-
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.gauge.foo:3|g");
112+
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.gauge.f-o-o:3|g");
113113
}
114114

115115
private static final class DummyStatsDServer implements Runnable {

0 commit comments

Comments
 (0)