Skip to content

Commit

Permalink
Support attaching base units to distribution summaries (issue #80)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 18, 2017
1 parent 1d15e01 commit eb5f737
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ private class DistributionSummaryBuilder implements DistributionSummary.Builder
private Histogram<?> histogram;
private final List<Tag> tags = new ArrayList<>();
private String description;
private String baseUnit;

private DistributionSummaryBuilder(String name) {
this.name = name;
Expand Down Expand Up @@ -233,10 +234,17 @@ public DistributionSummary.Builder description(String description) {
return this;
}

@Override
public DistributionSummary.Builder baseUnit(String unit) {
this.baseUnit = unit;
return this;
}

@Override
public DistributionSummary create() {
return registerMeterIfNecessary(DistributionSummary.class, name, tags, id ->
newDistributionSummary(id.getConventionName(Meter.Type.DistributionSummary), id.getTags(), description, quantiles, histogram));
newDistributionSummary(id.getConventionName(Meter.Type.DistributionSummary, baseUnit), id.getTags(),
description, quantiles, histogram));
}
}

Expand Down Expand Up @@ -449,8 +457,12 @@ String getName() {
/**
* The formatted name matching this registry's naming convention
*/
String getConventionName(Meter.Type type, String baseUnit) {
return namingConvention.name(name, type, baseUnit);
}

String getConventionName(Meter.Type type) {
return namingConvention.name(name, type);
return getConventionName(type, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ default Builder tags(String... tags) {

Builder description(String description);

Builder baseUnit(String unit);

DistributionSummary create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public interface NamingConvention {
NamingConvention snakeCase = new NamingConvention() {
@Override
public String name(String name, Meter.Type type) {
public String name(String name, Meter.Type type, String baseUnit) {
return toSnakeCase(name);
}

Expand All @@ -50,7 +50,7 @@ private String toSnakeCase(String value) {

NamingConvention camelCase = new NamingConvention() {
@Override
public String name(String name, Meter.Type type) {
public String name(String name, Meter.Type type, String baseUnit) {
return toCamelCase(name);
}

Expand Down Expand Up @@ -82,7 +82,11 @@ private String toCamelCase(String value) {
}
};

String name(String name, Meter.Type type);
default String name(String name, Meter.Type type) {
return name(name, type, null);
}

String name(String name, Meter.Type type, String baseUnit);

default String tagKey(String key) { return key; }
default String tagValue(String value) { return value; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class DatadogNamingConvention implements NamingConvention {
* all non-alphanumeric characters with '_'.
*/
@Override
public String name(String name, Meter.Type type) {
String sanitized = NamingConvention.camelCase.name(name, type);
public String name(String name, Meter.Type type, String baseUnit) {
String sanitized = NamingConvention.camelCase.name(name, type, baseUnit);

// Metrics that don't start with a letter get dropped on the floor by the Datadog publish API,
// so we will prepend them with 'm_'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class GraphiteNamingConvention implements NamingConvention {
private static final Pattern blacklistedChars = Pattern.compile("[{}(),=\\[\\]/]");

@Override
public String name(String name, Meter.Type type) {
public String name(String name, Meter.Type type, String baseUnit) {
return format(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/
public class InfluxNamingConvention implements NamingConvention {
@Override
public String name(String name, Meter.Type type) {
public String name(String name, Meter.Type type, String baseUnit) {
return format(name.replace("=", "_"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class PrometheusNamingConvention implements NamingConvention {
* [a-zA-Z_:][a-zA-Z0-9_:]*
*/
@Override
public String name(String name, Meter.Type type) {
String conventionName = NamingConvention.snakeCase.name(name, type);
public String name(String name, Meter.Type type, String baseUnit) {
String conventionName = NamingConvention.snakeCase.name(name, type, baseUnit);

switch (type) {
case Counter:
Expand All @@ -51,6 +51,10 @@ public String name(String name, Meter.Type type) {
else if(!conventionName.endsWith("_seconds"))
conventionName += "_duration_seconds";
break;
case DistributionSummary:
if(baseUnit != null && !conventionName.endsWith("_" + baseUnit))
conventionName += "_" + baseUnit;
break;
}

String sanitized = nameChars.matcher(conventionName).replaceAll("_");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public SlidingWindow(Integer windowSize) {
windowSize *= -1;
}
this.windowSize = windowSize;
this.elements = new LinkedList<Element<T>>();
this.elements = new LinkedList<>();
}

/**
Expand All @@ -63,7 +63,7 @@ public SlidingWindow(Integer windowSize) {
* @param element
*/
public void add(T element) {
Element<T> newElement = new Element<T>(element, this.windowSize, 0);
Element<T> newElement = new Element<>(element, this.windowSize, 0);
this.elements.addFirst(newElement);
}

Expand All @@ -85,7 +85,7 @@ public void add(T element, Integer size) {
throw new RuntimeException("The size of an element can't be a negative integer.");
}

Element<T> newElement = new Element<T>(element, this.windowSize - size, size);
Element<T> newElement = new Element<>(element, this.windowSize - size, size);
this.elements.addFirst(newElement);
}

Expand Down Expand Up @@ -156,7 +156,7 @@ public T getNewestElement() {
* @return a {@link Collection} of element of type <b>T</b>
*/
public Collection<T> getAll() {
Collection<T> elements = new LinkedList<T>();
Collection<T> elements = new LinkedList<>();

for (int i = 0; i < this.elements.size(); i++) {
elements.add(this.elements.get(i).getElement());
Expand All @@ -171,7 +171,7 @@ public Collection<T> getAll() {
* @return a {@link Collection} of element of type {@link Integer}
*/
public Collection<Integer> getAllLifeTimes() {
Collection<Integer> lifeTimes = new LinkedList<Integer>();
Collection<Integer> lifeTimes = new LinkedList<>();

for (int i = 0; i < this.elements.size(); i++) {
lifeTimes.add(this.elements.get(i).getTimeToLive());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DatadogNamingConventionTest {

@Test
void nameStartsWithLetter() {
assertThat(convention.name("123", Meter.Type.Gauge)).isEqualTo("m_123");
assertThat(convention.name("123", Meter.Type.Gauge, null)).isEqualTo("m_123");
}

@Test
Expand All @@ -35,6 +35,6 @@ void tagKeyStartsWithLetter() {

@Test
void dotNotationIsConvertedToCamelCase() {
assertThat(convention.name("gauge.size", Meter.Type.Gauge)).isEqualTo("gaugeSize");
assertThat(convention.name("gauge.size", Meter.Type.Gauge, null)).isEqualTo("gaugeSize");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ void unitsAreAppendedToTimers() {
assertThat(convention.name("timer", Meter.Type.Timer)).isEqualTo("timer_duration_seconds");
}

@Test
void unitsAreAppendedToDistributionSummaries() {
assertThat(convention.name("response.size", Meter.Type.DistributionSummary, "bytes")).isEqualTo("response_size_bytes");
assertThat(convention.name("summary", Meter.Type.DistributionSummary)).isEqualTo("summary");
}

@Test
void dotNotationIsConvertedToSnakeCase() {
assertThat(convention.name("gauge.size", Meter.Type.Gauge)).isEqualTo("gauge_size");
Expand Down

0 comments on commit eb5f737

Please sign in to comment.