Skip to content

Commit

Permalink
Merge branch 'master' into perm-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanPMiller authored Oct 30, 2019
2 parents 8f69220 + 1f0c936 commit 54cfc89
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: java

jdk:
- oraclejdk8
- openjdk8
- oraclejdk11

before_cache:
Expand All @@ -20,11 +20,11 @@ deploy:
script: ./gradlew publish
on:
branch: master
jdk: oraclejdk8
jdk: openjdk8
# releases from tags
- provider: script
skip_cleanup: true
script: ./gradlew publish
on:
tags: true
jdk: oraclejdk8
jdk: openjdk8
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ buildscript {
classpath group: 'com.bmuschko', name: 'gradle-clover-plugin', version: '2.2.2'
classpath group: 'com.github.ben-manes', name: 'gradle-versions-plugin', version: '0.20.0'
classpath group: 'me.champeau.gradle', name: 'jmh-gradle-plugin', version: '0.4.8'
classpath group: 'org.owasp', name: 'dependency-check-gradle', version: '4.0.0.1'
classpath group: 'gradle.plugin.com.github.spotbugs', name: 'spotbugs-gradle-plugin', version: '1.6.6'
classpath group: 'org.owasp', name: 'dependency-check-gradle', version: '5.2.2'
classpath group: 'gradle.plugin.com.github.spotbugs', name: 'spotbugs-gradle-plugin', version: '2.0.0'
classpath group: 'org.ajoberstar.reckon', name: 'reckon-gradle', version: '0.9.0'
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

package io.ultrabrew.metrics.data;

import java.util.function.Predicate;
import java.util.Arrays;
import java.util.function.Predicate;

/**
* A distribution bucket specification used for histograms. A distribution bucket is an sorted array
Expand All @@ -22,21 +22,26 @@
* </ul>
*
* @see BasicHistogramAggregator
* @see NameSpec
*/
public class DistributionBucket {

private static final String UNDERFLOW = "underflow";
private static final String OVERFLOW = "overflow";

private final long[] buckets;
private final NameSpec nameSpec;

/**
* Creates a distribution for given bucket spec.
*
* @param buckets sorted array of unique values
*/
public DistributionBucket(final long[] buckets) {
this(buckets, new DefaultNameSpec());
}

public DistributionBucket(final long[] buckets, final NameSpec nameSpec) {
if (buckets.length < 2) {
throw new IllegalArgumentException("Minimum bucket length is 2");
}
Expand All @@ -48,6 +53,7 @@ public DistributionBucket(final long[] buckets) {
}

this.buckets = buckets.clone();
this.nameSpec = nameSpec;
}

public int getCount() {
Expand Down Expand Up @@ -98,14 +104,15 @@ public int getBucketIndex(long value) {
/**
* Generates the bucket names for representation purpose.
*
* <P>For a given spec: [0, 10, 100, 500, 1000], the names would look like:</P>
* <p>For a given spec: [0, 10, 100, 500, 1000], the names would look like:
*
* <ul>
* <li>0_10</li>
* <li>10_100</li>
* <li>100_500</li>
* <li>500_1000</li>
* <li>overflow</li>
* <li>underflow</li>
* <li>0_10
* <li>10_100
* <li>100_500
* <li>500_1000
* <li>overflow
* <li>underflow
* </ul>
*
* @return array of bucket names
Expand All @@ -115,7 +122,7 @@ public String[] getBucketNames() {
String[] names = new String[bucketCount + 1];
int i = 0;
for (; i < bucketCount - 1; i++) {
names[i] = Long.toString(buckets[i]) + '_' + buckets[i + 1];
names[i] = nameSpec.getBucketName(buckets[i]) + '_' + nameSpec.getBucketName(buckets[i + 1]);
}
names[i++] = OVERFLOW;
names[i++] = UNDERFLOW;
Expand Down Expand Up @@ -143,4 +150,36 @@ private static boolean matchAny(final long[] buckets, Predicate<Integer> predica
return false;
}

/**
* The bucket name spec. The default spec uses the String representation fo the bucket value as
* the name.
*
* <p>For a given spec: [0, 10, 100, 500, 1000], the names would look like:
*
* <ul>
* <li>0_10
* <li>10_100
* <li>100_500
* <li>500_1000
* <li>overflow
* <li>underflow
* </ul>
*
* For a measurement unit of nano seconds, the default naming scheme would look ugly. User can use
* a custom spec to prettify the bucket names. For example the bucket the names can milliseconds
* even though internally it tracks the values in nanoseconds.
*/
public interface NameSpec {
String getBucketName(long bucket);
}

/**
* Uses the String representation fo the bucket value as the name.
*/
public static class DefaultNameSpec implements NameSpec {
@Override
public String getBucketName(long bucket) {
return Long.toString(bucket);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

package io.ultrabrew.metrics.data;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -88,4 +91,21 @@ void duplicateEntriesNotAllowed(final long[] invalidBuckets) {
assertThrows(IllegalArgumentException.class, () -> new DistributionBucket(invalidBuckets),
"Bucket should not have duplicate entries");
}

@Test
void testBucketNamesInMillieAndValuesInNanoSeconds() {
DistributionBucket bucket =
new DistributionBucket(
new long[] {
MILLISECONDS.toNanos(0),
MILLISECONDS.toNanos(10),
MILLISECONDS.toNanos(50),
MILLISECONDS.toNanos(100),
MILLISECONDS.toNanos(1000)
},
(v) -> Long.toString(NANOSECONDS.toMillis(v)));
String[] expected = {"0_10", "10_50", "50_100", "100_1000", "overflow", "underflow"};
assertArrayEquals(expected, bucket.getBucketNames());
}

}
2 changes: 1 addition & 1 deletion examples/undertow-httphandler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ dependencies {

compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.11.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.11.1'
compile group: 'io.undertow', name: 'undertow-core', version: '2.0.16.Final'
compile group: 'io.undertow', name: 'undertow-core', version: '2.0.27.Final'
}

0 comments on commit 54cfc89

Please sign in to comment.