|
8 | 8 | import java.io.IOException; |
9 | 9 | import java.time.Duration; |
10 | 10 | import java.time.Instant; |
| 11 | +import java.util.Arrays; |
| 12 | +import java.util.List; |
11 | 13 | import java.util.stream.Collectors; |
12 | 14 | import java.util.stream.Stream; |
13 | 15 |
|
14 | 16 | public class StatsDReporter implements Closeable { |
15 | 17 |
|
16 | 18 | private final StatsDClient client; |
17 | | - private final String globalTags; |
| 19 | + private final boolean tagsNativeFormatEnabled; |
18 | 20 | private static final Logger LOGGER = LoggerFactory.getLogger(StatsDReporter.class); |
| 21 | + private final String[] globalTags; |
| 22 | + |
| 23 | + public StatsDReporter(StatsDClient client, Boolean tagsNativeFormatEnabled, String... globalTags) { |
| 24 | + this.client = client; |
| 25 | + this.tagsNativeFormatEnabled = tagsNativeFormatEnabled; |
| 26 | + this.globalTags = globalTags; |
| 27 | + } |
19 | 28 |
|
20 | 29 | public StatsDReporter(StatsDClient client, String... globalTags) { |
21 | 30 | this.client = client; |
22 | | - this.globalTags = String.join(",", globalTags).replaceAll(":", "="); |
| 31 | + this.tagsNativeFormatEnabled = false; |
| 32 | + this.globalTags = globalTags; |
23 | 33 | } |
24 | 34 |
|
25 | 35 | public StatsDClient getClient() { |
26 | 36 | return client; |
27 | 37 | } |
28 | 38 |
|
29 | 39 | public void captureCount(String metric, Long delta, String... tags) { |
30 | | - client.count(withTags(metric, tags), delta); |
| 40 | + client.count(getMetrics(metric, tags), delta, getTags(tags)); |
31 | 41 | } |
32 | 42 |
|
33 | 43 | public void captureHistogram(String metric, long delta, String... tags) { |
34 | | - client.time(withTags(metric, tags), delta); |
| 44 | + client.time(getMetrics(metric, tags), delta, getTags(tags)); |
35 | 45 | } |
36 | 46 |
|
37 | 47 | public void captureDurationSince(String metric, Instant startTime, String... tags) { |
38 | | - client.recordExecutionTime(withTags(metric, tags), Duration.between(startTime, Instant.now()).toMillis()); |
| 48 | + client.recordExecutionTime(getMetrics(metric, tags), Duration.between(startTime, Instant.now()).toMillis(), getTags(tags)); |
39 | 49 | } |
40 | 50 |
|
41 | 51 | public void captureDuration(String metric, long duration, String... tags) { |
42 | | - client.recordExecutionTime(withTags(metric, tags), duration); |
| 52 | + client.recordExecutionTime(getMetrics(metric, tags), duration, getTags(tags)); |
43 | 53 | } |
44 | 54 |
|
45 | 55 | public void gauge(String metric, Integer value, String... tags) { |
46 | | - client.gauge(withTags(metric, tags), value); |
| 56 | + client.gauge(getMetrics(metric, tags), value, getTags(tags)); |
47 | 57 | } |
48 | 58 |
|
49 | 59 | public void increment(String metric, String... tags) { |
50 | | - captureCount(metric, 1L, tags); |
| 60 | + captureCount(metric, 1L, getTags(tags)); |
51 | 61 | } |
52 | 62 |
|
53 | 63 | public void recordEvent(String metric, String eventName, String... tags) { |
54 | | - client.recordSetValue(withTags(metric, tags), eventName); |
| 64 | + client.recordSetValue(getMetrics(metric, tags), eventName, getTags(tags)); |
| 65 | + } |
| 66 | + |
| 67 | + private String[] getTags(String[] tags) { |
| 68 | + if (!this.tagsNativeFormatEnabled) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + List<String> list = Arrays.stream(tags).map(s -> s.replaceAll("=", ":")).collect(Collectors.toList()); |
| 72 | + list.addAll(Arrays.asList(this.getGlobalTags().split(","))); |
| 73 | + return list.toArray(new String[0]); |
| 74 | + } |
| 75 | + |
| 76 | + private String getMetrics(String metric, String... tags) { |
| 77 | + return this.tagsNativeFormatEnabled ? metric : withTags(metric, tags); |
55 | 78 | } |
56 | 79 |
|
57 | | - private String withGlobalTags(String metric) { |
58 | | - return metric + "," + this.globalTags; |
| 80 | + private String getGlobalTags() { |
| 81 | + if (this.tagsNativeFormatEnabled) { |
| 82 | + return String.join(",", this.globalTags).replaceAll("=", ":"); |
| 83 | + } |
| 84 | + return String.join(",", this.globalTags).replaceAll(":", "="); |
59 | 85 | } |
60 | 86 |
|
61 | 87 | private String withTags(String metric, String... tags) { |
62 | | - return Stream.concat(Stream.of(withGlobalTags(metric)), Stream.of(tags)) |
| 88 | + return Stream.concat( |
| 89 | + Stream.of(metric + "," + this.getGlobalTags()), |
| 90 | + tags == null ? Stream.empty() : Arrays.stream(tags) |
| 91 | + ) |
63 | 92 | .collect(Collectors.joining(",")); |
64 | 93 | } |
65 | 94 |
|
| 95 | + |
66 | 96 | @Override |
67 | 97 | public void close() throws IOException { |
68 | 98 | LOGGER.info("StatsD connection closed"); |
|
0 commit comments