Skip to content

Commit 38b7d0e

Browse files
committed
Merge pull request #8906 from odin-delrio:fix-statsd-metrics-collection
* pr/8906: Polish "Fix statsd metrics collection for names with ":"" Fix statsd metrics collection for names with ":"
2 parents cdf3ead + 9a5346f commit 38b7d0e

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
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,13 @@ 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()),
92+
delta.getValue().longValue());
9193
}
9294

9395
@Override
9496
public void set(Metric<?> value) {
95-
String name = value.getName();
97+
String name = sanitizeMetricName(value.getName());
9698
if (name.contains("timer.") && !name.contains("gauge.")
9799
&& !name.contains("counter.")) {
98100
this.client.recordExecutionTime(name, value.getValue().longValue());
@@ -117,6 +119,15 @@ public void close() {
117119
this.client.stop();
118120
}
119121

122+
/**
123+
* Sanitize the metric name if necessary.
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: 16 additions & 1 deletion
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.
@@ -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<Long>("counter.fo:o", 3L));
104+
this.server.waitForMessage();
105+
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.counter.fo-o:3|c");
106+
}
107+
108+
@Test
109+
public void setMetricWithInvalidCharsInName() throws Exception {
110+
this.writer.set(new Metric<Long>("gauge.f:o:o", 3L));
111+
this.server.waitForMessage();
112+
assertThat(this.server.messagesReceived().get(0)).isEqualTo("me.gauge.f-o-o: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)