Skip to content

Commit e44ffa2

Browse files
committed
WIP toward tags
1 parent 0e7bbb7 commit e44ffa2

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/main/java/com/arpnetworking/metrics/mad/sources/MappingSource.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.arpnetworking.tsdcore.model.MetricType;
3232
import com.arpnetworking.tsdcore.model.Quantity;
3333
import com.google.common.base.MoreObjects;
34+
import com.google.common.base.Splitter;
3435
import com.google.common.collect.ImmutableList;
3536
import com.google.common.collect.ImmutableMap;
3637
import com.google.common.collect.Maps;
@@ -40,6 +41,7 @@
4041
import java.util.Map;
4142
import java.util.regex.Matcher;
4243
import java.util.regex.Pattern;
44+
import java.util.stream.Collectors;
4345

4446
/**
4547
* Implementation of <code>Source</code> which wraps another <code>Source</code>
@@ -95,6 +97,7 @@ private MappingSource(final Builder builder) {
9597
private final Map<Pattern, List<String>> _findAndReplace;
9698

9799
private static final Logger LOGGER = LoggerFactory.getLogger(MappingSource.class);
100+
private static final Splitter.MapSplitter TAG_SPLITTER = Splitter.on(';').omitEmptyStrings().trimResults().withKeyValueSeparator('=');
98101

99102
// NOTE: Package private for testing
100103
/* package private */ static final class MappingObserver implements Observer {
@@ -123,7 +126,27 @@ public void notify(final Observable observable, final Object event) {
123126
final Matcher matcher = findAndReplace.getKey().matcher(metric.getKey());
124127
if (matcher.find()) {
125128
for (final String replacement : findAndReplace.getValue()) {
126-
merge(metric.getValue(), matcher.replaceAll(replacement), mergedMetrics);
129+
final String replacedString = matcher.replaceAll(replacement);
130+
131+
final int tagsStart = replacedString.indexOf(';');
132+
if (tagsStart == -1) {
133+
// We just have a metric name. Optimize for this common case
134+
merge(metric.getValue(), replacedString, mergedMetrics);
135+
} else {
136+
final Map<String, String> parsedTags = TAG_SPLITTER.split(replacedString.substring(tagsStart + 1));
137+
final Map<String, String> finalTags = Maps.newTreeMap();
138+
finalTags.putAll(record.getDimensions());
139+
finalTags.putAll(parsedTags);
140+
final StringBuilder keyBuilder = new StringBuilder();
141+
keyBuilder.append(replacedString.substring(0, tagsStart + 1));
142+
keyBuilder.append(
143+
finalTags.entrySet()
144+
.stream()
145+
.map(entry -> String.format("%s=%s", entry.getKey(), entry.getValue()))
146+
.collect(Collectors.joining(";")));
147+
148+
merge(metric.getValue(), keyBuilder.toString(), mergedMetrics);
149+
}
127150
}
128151
//Having "found" set here means that mapping a metric to an empty list suppresses that metric
129152
found = true;

0 commit comments

Comments
 (0)