Skip to content

Commit

Permalink
Allow custom key names instead of fixed 'type' key name
Browse files Browse the repository at this point in the history
  • Loading branch information
clorenz committed Jul 10, 2018
1 parent ae95d7d commit 5d042b8
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion dc-commons-feign/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons REST Feign</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-jdbi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons JDBI</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons-prosemirror</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons ProseMirror Model Jackson</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons-prosemirror</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons ProseMirror Model</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-prosemirror/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons ProseMirror (Parent)</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Server</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-springaop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Spring AOP</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-springboot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Spring Boot (Spring Boot 2)</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,29 @@ public MetricsService(MeterRegistry meterRegistry) {
* @param value Value of the gauge
*/
public void setGauge(String name, long value) {
handleCounter(name, null, null, value, null, false);
handleCounter(name, null,null, null, value, null, false);
}

/**
* Sets the value of a gauge
* @param name Name of the gauge, postfixed with <tt>.amount</tt>
* @param tag Name of the tag
* @param tag Name of the tag (key is <tt>type</tt>)
* @param value Value of the gauge
*/
public void setGauge(String name, String tag, long value) {
handleCounter(name, tag, null, value, null, false);
handleCounter(name, "type", tag, null, value, null, false);
}


/**
* Sets the value of a gauge
* @param name Name of the gauge, postfixed with <tt>.amount</tt>
* @param tagKey Name of the tag
* @param tagValue Value of the tag
* @param value Value of the gauge
*/
public void setGauge(String name, String tagKey, String tagValue, long value) {
handleCounter(name, tagKey, tagValue, null, value, null, false);
}

/**
Expand All @@ -49,7 +61,7 @@ public void setGauge(String name, String tag, long value) {
* @param tag Name of the tag
*/
public void increaseCounter(String name, String tag) {
handleCounter(name, tag, 1L, null, null, false);
handleCounter(name, "type", tag, 1L, null, null, false);
}

/**
Expand All @@ -59,7 +71,7 @@ public void increaseCounter(String name, String tag) {
* @param increment Increment value
*/
public void increaseCounter(String name, String tag, long increment) {
handleCounter(name, tag, increment, null,null, false);
handleCounter(name, "type", tag, increment, null,null, false);
}

/**
Expand All @@ -69,7 +81,7 @@ public void increaseCounter(String name, String tag, long increment) {
* @param durationMillis Duration in milliseconds
*/
public void increaseCounterWithDuration(String name, String tag, Long durationMillis) {
handleCounter(name, tag, 1L, null, durationMillis, false);
handleCounter(name, "type", tag, 1L, null, durationMillis, false);
}

/**
Expand All @@ -79,11 +91,11 @@ public void increaseCounterWithDuration(String name, String tag, Long durationMi
* @param durationMillis Duration in milliseconds
*/
public void increaseCounterWithDurationAndPercentiles(String name, String tag, Long durationMillis) {
handleCounter(name, tag, 1L, null, durationMillis, true);
handleCounter(name, "type", tag, 1L, null, durationMillis, true);
}

private void handleCounter(String name, String tag, Long increment, Long absoluteValue, Long durationMillis, Boolean publishPercentiles) {
String key = name + ( tag != null ? "." + tag : "");
private void handleCounter(String name, String tagKey, String tagValue, Long increment, Long absoluteValue, Long durationMillis, Boolean publishPercentiles) {
String key = name + ( ( tagKey != null && tagValue != null ) ? "." + tagValue : "");

if ( increment != null ) {
// Increase counter value
Expand All @@ -95,19 +107,19 @@ private void handleCounter(String name, String tag, Long increment, Long absolut
// Register counter, if it doesn't exist yet
if (counterTags.get(key) == null) {
counterTags.put(key, new HashSet<>());
if ( tag != null ) {
counterTags.get(key).add(new ImmutableTag("type", tag));
if ( tagKey != null && tagValue != null ) {
counterTags.get(key).add(new ImmutableTag(tagKey, tagValue));
meterRegistry.gauge(name + ".amount", counterTags.get(key), key, counters::get);
} else {
meterRegistry.gauge(name + ".amount", counters.get(key));
}
}

if (durationMillis != null && tag != null) {
if (durationMillis != null && tagKey != null && tagValue != null) {
// Register Timer
if (timers.get(key) == null) {
Timer.Builder timerBuilder = Timer.builder(name + ".duration")
.tag("type", tag);
.tag(tagKey, tagValue);

if ( publishPercentiles ) {
timerBuilder = timerBuilder.publishPercentiles(0.5, 0.95)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ void multipleValuesOfAGauge() {
assertThat(metricsService.getCounters().get("foo.bar")).isEqualTo(42);
}

@Test
@DisplayName("Gauges with different tag keys")
void gaugesWithDifferentTagKeys() {
metricsService.setGauge("foo", "bar", "blub", 2);
assertThat(metricsService.getCounters().get("foo.blub")).isEqualTo(2);
metricsService.setGauge("foo", "two", 42);
assertThat(metricsService.getCounters().get("foo.two")).isEqualTo(42);
}

@Test
@DisplayName("Gauges without tag")
void gaugeWithoutTag() {
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-springdata/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Spring Data</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-springmvc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Spring MVC</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-springsecurity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Spring Security (Spring Security 5)</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-validation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons Validation</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-xml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons XML</name>
Expand Down
2 changes: 1 addition & 1 deletion dc-commons-yaml/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
</parent>

<name>DigitalCollections: Commons YAML</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>de.digitalcollections.commons</groupId>
<artifactId>dc-commons</artifactId>
<version>1.4.0</version>
<version>1.4.1</version>
<packaging>pom</packaging>

<modules>
Expand Down

0 comments on commit 5d042b8

Please sign in to comment.