Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for publishing statsd tags in native statsd format #88

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
punit-kulal marked this conversation as resolved.
Show resolved Hide resolved
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.add(this.getGlobalTags());
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
Loading