Skip to content

Commit

Permalink
feat: add support for publishing statsd tags in native statsd format (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
prakharmathur82 authored Sep 18, 2024
1 parent 31a075f commit e81b526
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.17'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.4.7"
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:5.2.5"
classpath "org.ajoberstar:gradle-git:1.6.0"
}
}
Expand All @@ -22,7 +22,7 @@ plugins {
}

group 'org.raystack'
version '0.4.0'
version '0.4.1'

repositories {
mavenCentral()
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/raystack/depot/config/MetricsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ public interface MetricsConfig extends Config {
@Config.Key("METRIC_STATSD_TAGS")
@DefaultValue("")
String getMetricStatsDTags();

@Config.Key("METRIC_STATSD_TAGS_NATIVE_FORMAT_ENABLE")
@DefaultValue("false")
boolean getMetricStatsDTagsNativeFormatEnabled();
}
54 changes: 42 additions & 12 deletions src/main/java/org/raystack/depot/metrics/StatsDReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,91 @@
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StatsDReporter implements Closeable {

private final StatsDClient client;
private final String globalTags;
private final boolean tagsNativeFormatEnabled;
private static final Logger LOGGER = LoggerFactory.getLogger(StatsDReporter.class);
private final String[] globalTags;

public StatsDReporter(StatsDClient client, Boolean tagsNativeFormatEnabled, String... globalTags) {
this.client = client;
this.tagsNativeFormatEnabled = tagsNativeFormatEnabled;
this.globalTags = globalTags;
}

public StatsDReporter(StatsDClient client, String... globalTags) {
this.client = client;
this.globalTags = String.join(",", globalTags).replaceAll(":", "=");
this.tagsNativeFormatEnabled = false;
this.globalTags = globalTags;
}

public StatsDClient getClient() {
return client;
}

public void captureCount(String metric, Long delta, String... tags) {
client.count(withTags(metric, tags), delta);
client.count(getMetrics(metric, tags), delta, getTags(tags));
}

public void captureHistogram(String metric, long delta, String... tags) {
client.time(withTags(metric, tags), delta);
client.time(getMetrics(metric, tags), delta, getTags(tags));
}

public void captureDurationSince(String metric, Instant startTime, String... tags) {
client.recordExecutionTime(withTags(metric, tags), Duration.between(startTime, Instant.now()).toMillis());
client.recordExecutionTime(getMetrics(metric, tags), Duration.between(startTime, Instant.now()).toMillis(), getTags(tags));
}

public void captureDuration(String metric, long duration, String... tags) {
client.recordExecutionTime(withTags(metric, tags), duration);
client.recordExecutionTime(getMetrics(metric, tags), duration, getTags(tags));
}

public void gauge(String metric, Integer value, String... tags) {
client.gauge(withTags(metric, tags), value);
client.gauge(getMetrics(metric, tags), value, getTags(tags));
}

public void increment(String metric, String... tags) {
captureCount(metric, 1L, tags);
captureCount(metric, 1L, getTags(tags));
}

public void recordEvent(String metric, String eventName, String... tags) {
client.recordSetValue(withTags(metric, tags), eventName);
client.recordSetValue(getMetrics(metric, tags), eventName, getTags(tags));
}

private String[] getTags(String[] tags) {
if (!this.tagsNativeFormatEnabled) {
return null;
}
List<String> list = Arrays.stream(tags).map(s -> s.replaceAll("=", ":")).collect(Collectors.toList());
list.addAll(Arrays.asList(this.getGlobalTags().split(",")));
return list.toArray(new String[0]);
}

private String getMetrics(String metric, String... tags) {
return this.tagsNativeFormatEnabled ? metric : withTags(metric, tags);
}

private String withGlobalTags(String metric) {
return metric + "," + this.globalTags;
private String getGlobalTags() {
if (this.tagsNativeFormatEnabled) {
return String.join(",", this.globalTags).replaceAll("=", ":");
}
return String.join(",", this.globalTags).replaceAll(":", "=");
}

private String withTags(String metric, String... tags) {
return Stream.concat(Stream.of(withGlobalTags(metric)), Stream.of(tags))
return Stream.concat(
Stream.of(metric + "," + this.getGlobalTags()),
tags == null ? Stream.empty() : Arrays.stream(tags)
)
.collect(Collectors.joining(","));
}


@Override
public void close() throws IOException {
LOGGER.info("StatsD connection closed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static <T> T[] append(T[] arr, T[] second) {

public StatsDReporter build() {
StatsDClient statsDClient = buildStatsDClient();
return new StatsDReporter(statsDClient, append(metricsConfig.getMetricStatsDTags().split(","), extraTags));
return new StatsDReporter(statsDClient, metricsConfig.getMetricStatsDTagsNativeFormatEnabled(), append(metricsConfig.getMetricStatsDTags().split(","), extraTags));
}

private StatsDClient buildStatsDClient() {
Expand Down

0 comments on commit e81b526

Please sign in to comment.