Skip to content

Commit

Permalink
Review comments: use common service loader with priority ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
tjquinno committed Apr 12, 2021
1 parent d8bc178 commit a177b2b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
4 changes: 4 additions & 0 deletions metrics/metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config-mp</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-service-loader</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.metrics</groupId>
<artifactId>microprofile-metrics-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package io.helidon.metrics;

import io.helidon.common.serviceloader.HelidonServiceLoader;

import java.util.Comparator;
import java.util.List;
import java.util.ServiceLoader;
Expand All @@ -36,6 +38,7 @@ class ExemplarServiceManager {

private static final List<ExemplarService> EXEMPLAR_SERVICES = collectExemplarServices();


private static final Supplier<String> EXEMPLAR_SUPPLIER = EXEMPLAR_SERVICES.isEmpty()
? () -> ""
: () -> EXEMPLAR_SERVICES.stream()
Expand All @@ -57,21 +60,11 @@ static String exemplarLabel() {

private static List<ExemplarService> collectExemplarServices() {
List<ExemplarService> exemplarServices =
ServiceLoader.load(ExemplarService.class)
.stream()
.sorted(Comparator.comparing(ExemplarServiceManager::priorityValue))
.map(ServiceLoader.Provider::get)
.collect(Collectors.toList());

HelidonServiceLoader.create(ServiceLoader.load(ExemplarService.class)).asList();
if (!exemplarServices.isEmpty()) {
LOGGER.log(Level.INFO, "Using metrics ExemplarServices " + exemplarServices.toString());
}

return exemplarServices;
}

private static int priorityValue(ServiceLoader.Provider<ExemplarService> exemplarSupportProvider) {
Priority p = exemplarSupportProvider.type().getAnnotation(Priority.class);
return p == null ? ExemplarService.DEFAULT_PRIORITY : p.value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class TestNearestValueSearch {
class TestNearestValueSearch {

private static final WeightedSample[] VALUES = new WeightedSample[] {
new WeightedSample(1L),
Expand All @@ -32,37 +32,37 @@ public class TestNearestValueSearch {
new WeightedSample(7L)};

@Test
public void testExactMatch() {
void testExactMatch() {
assertThat("Exact match of 1", WeightedSnapshot.slotNear(derived(1.0), VALUES), is(0));
assertThat("Exact match of 3", WeightedSnapshot.slotNear(derived(3.0), VALUES), is(1));
assertThat("Exact match of 5", WeightedSnapshot.slotNear(derived(5.0), VALUES), is(2));
}

@Test
public void testBeforeFirst() {
void testBeforeFirst() {
assertThat("Approx match before first element", WeightedSnapshot.slotNear(derived(0.5), VALUES), is(0));
}

@Test
public void testAfterLast() {
void testAfterLast() {
assertThat("Approx match after last element", WeightedSnapshot.slotNear(derived(9.0), VALUES),
is(VALUES.length - 1));
}

@Test
public void testCloserToLower() {
void testCloserToLower() {
assertThat("Closer to lowest", WeightedSnapshot.slotNear(derived(1.2), VALUES), is(0));
assertThat("Closer to inside", WeightedSnapshot.slotNear(derived(3.2), VALUES), is(1));
}

@Test
public void testCloserToHigher() {
void testCloserToHigher() {
assertThat("Closer to highest", WeightedSnapshot.slotNear(derived(6.5), VALUES), is(3));
assertThat("Closer to inside", WeightedSnapshot.slotNear(derived(2.5), VALUES), is(1));
}

@Test
public void testMidpoint() {
void testMidpoint() {
assertThat("Midpoint", WeightedSnapshot.slotNear(derived(2.0), VALUES), is(0));
}
}

0 comments on commit a177b2b

Please sign in to comment.