Skip to content
This repository was archived by the owner on Nov 27, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import java.nio.charset.StandardCharsets;
import java.util.function.Consumer;

import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flozano.metrics.Tags;
import com.flozano.metrics.client.MetricValue;
import com.flozano.metrics.client.MetricsClient;

Expand Down Expand Up @@ -51,6 +53,16 @@ public static void toStringParts(MetricValue msg, Consumer<String> parts) {
parts.accept("|@");
parts.accept(String.format("%1.2f", r));
}
}

Tags tags = msg.getTags();
if (!tags.isEmpty()) {
parts.accept("|#");
parts.accept(tags
.stream()
.sorted()
.map(tag -> String.valueOf(tag.name) + ':' + tag.value)
.collect(Collectors.joining(","))
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.Assert.assertEquals;


import com.flozano.metrics.Tags;
import org.junit.Test;

import com.flozano.metrics.client.GaugeValue;
Expand All @@ -27,4 +29,14 @@ public void deltaValue() {
public void deltaNegativeValue() {
assertEquals("gauge:-1234|g", MetricToBytesEncoder.toString(new GaugeValue("gauge", -1234, true)));
}

@Test
public void tagsWithDeltaValue() {
assertEquals("gauge:+1|g|#lab:val", MetricToBytesEncoder.toString(
new GaugeValue("gauge", 1, true, Tags.empty().with("lab", "val"))));

assertEquals("gauge:-1|g|#gel:lib,lab:val", MetricToBytesEncoder.toString(
new GaugeValue("gauge", -1, true, Tags.empty()
.with("lab", "val").with("gel", "lib"))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;


import com.flozano.metrics.Tags;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -22,14 +25,15 @@

@RunWith(Parameterized.class)
public class MetricToBytesEncoderTest {
@Parameters(name = "{index}: type={0}, name={1}, value={2}, sampleRate={3}")
@Parameters(name = "{index}: type={0}, name={1}, value={2}, sampleRate={3}, tags={4}")
public static Collection<Object[]> params() {
List<Object[]> params = new LinkedList<>();
Tags tags = Tags.empty().with("lab", "val").with("gel", "lib");
for (Class<? extends MetricValue> c : Arrays.asList(GaugeValue.class, CountValue.class, TimingValue.class)) {
for (String name : Arrays.asList("some", "thing")) {
for (long value : Arrays.asList(100, 100000, 20000)) {
for (Double sampleRate : Arrays.<Double> asList(0.1, 0.5, 1d, null)) {
params.add(new Object[] { c, name, value, sampleRate });
params.add(new Object[] { c, name, value, sampleRate, tags });
}
}
}
Expand All @@ -43,12 +47,14 @@ public static Collection<Object[]> params() {
String name = "something";
long value;
Double sampleRate;
Tags tags;

public MetricToBytesEncoderTest(Class<? extends MetricValue> type, String name, long value, Double sampleRate) {
public MetricToBytesEncoderTest(Class<? extends MetricValue> type, String name, long value, Double sampleRate, Tags tags) {
this.type = type;
this.name = name;
this.value = value;
this.sampleRate = sampleRate;
this.tags = tags;
}

@Test
Expand All @@ -61,15 +67,20 @@ public void toStringPartsTest() {


String expectedValue(String suffix) {
String formattedTags = tags
.stream()
.sorted()
.map(tag -> String.valueOf(tag.name) + ':' + tag.value)
.collect(Collectors.joining(","));
if (sampleRate == null) {
return String.format("%s:%d|%s", name, value, suffix);
return String.format("%s:%d|%s|#%s", name, value, suffix, formattedTags);
}
return String.format("%s:%d|%s|@%.2f", name, value, suffix, sampleRate);
return String.format("%s:%d|%s|@%.2f|#%s", name, value, suffix, sampleRate, formattedTags);
}

MetricValue newMetric() {
try {
return type.getConstructor(String.class, long.class, Double.class).newInstance(name, value, sampleRate);
return type.getConstructor(String.class, long.class, Double.class, Tags.class).newInstance(name, value, sampleRate, tags);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw new RuntimeException(e);
Expand Down