Skip to content

Commit e81b526

Browse files
feat: add support for publishing statsd tags in native statsd format (#88)
* feat: add support for publishing statsd tags in native statsd format * refactor: use native boolean for statsd native tags config * fix: update global tags logic for native statsd tags * refactor: update global tags logic array creation for native statsd tags
1 parent 31a075f commit e81b526

File tree

4 files changed

+49
-15
lines changed

4 files changed

+49
-15
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
}
55
dependencies {
66
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.17'
7-
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.7"
7+
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:5.2.5"
88
classpath "org.ajoberstar:gradle-git:1.6.0"
99
}
1010
}
@@ -22,7 +22,7 @@ plugins {
2222
}
2323

2424
group 'org.raystack'
25-
version '0.4.0'
25+
version '0.4.1'
2626

2727
repositories {
2828
mavenCentral()

src/main/java/org/raystack/depot/config/MetricsConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,8 @@ public interface MetricsConfig extends Config {
1515
@Config.Key("METRIC_STATSD_TAGS")
1616
@DefaultValue("")
1717
String getMetricStatsDTags();
18+
19+
@Config.Key("METRIC_STATSD_TAGS_NATIVE_FORMAT_ENABLE")
20+
@DefaultValue("false")
21+
boolean getMetricStatsDTagsNativeFormatEnabled();
1822
}

src/main/java/org/raystack/depot/metrics/StatsDReporter.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,91 @@
88
import java.io.IOException;
99
import java.time.Duration;
1010
import java.time.Instant;
11+
import java.util.Arrays;
12+
import java.util.List;
1113
import java.util.stream.Collectors;
1214
import java.util.stream.Stream;
1315

1416
public class StatsDReporter implements Closeable {
1517

1618
private final StatsDClient client;
17-
private final String globalTags;
19+
private final boolean tagsNativeFormatEnabled;
1820
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+
}
1928

2029
public StatsDReporter(StatsDClient client, String... globalTags) {
2130
this.client = client;
22-
this.globalTags = String.join(",", globalTags).replaceAll(":", "=");
31+
this.tagsNativeFormatEnabled = false;
32+
this.globalTags = globalTags;
2333
}
2434

2535
public StatsDClient getClient() {
2636
return client;
2737
}
2838

2939
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));
3141
}
3242

3343
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));
3545
}
3646

3747
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));
3949
}
4050

4151
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));
4353
}
4454

4555
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));
4757
}
4858

4959
public void increment(String metric, String... tags) {
50-
captureCount(metric, 1L, tags);
60+
captureCount(metric, 1L, getTags(tags));
5161
}
5262

5363
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);
5578
}
5679

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(":", "=");
5985
}
6086

6187
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+
)
6392
.collect(Collectors.joining(","));
6493
}
6594

95+
6696
@Override
6797
public void close() throws IOException {
6898
LOGGER.info("StatsD connection closed");

src/main/java/org/raystack/depot/metrics/StatsDReporterBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ private static <T> T[] append(T[] arr, T[] second) {
4747

4848
public StatsDReporter build() {
4949
StatsDClient statsDClient = buildStatsDClient();
50-
return new StatsDReporter(statsDClient, append(metricsConfig.getMetricStatsDTags().split(","), extraTags));
50+
return new StatsDReporter(statsDClient, metricsConfig.getMetricStatsDTagsNativeFormatEnabled(), append(metricsConfig.getMetricStatsDTags().split(","), extraTags));
5151
}
5252

5353
private StatsDClient buildStatsDClient() {

0 commit comments

Comments
 (0)