Skip to content

Commit

Permalink
Fix FunctionCounter in SingleIndicator.Builder.count() (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
izeye committed Feb 8, 2021
1 parent 8739d13 commit f2fe365
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Expand Up @@ -40,7 +40,10 @@
import static io.micrometer.health.QueryUtils.SUM_OR_NAN;

/**
* Service level objective.
*
* @author Jon Schneider
* @author Johnny Lim
* @since 1.6.0
*/
public abstract class ServiceLevelObjective {
Expand Down Expand Up @@ -240,7 +243,7 @@ public final NumericQuery count(Function<Search, Search> search) {
} else if (m instanceof FunctionTimer) {
return ((FunctionTimer) m).count();
} else if (m instanceof FunctionCounter) {
((FunctionCounter) m).count();
return ((FunctionCounter) m).count();
} else if (m instanceof LongTaskTimer) {
return (double) ((LongTaskTimer) m).activeTasks();
}
Expand Down
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.health;

import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.MockClock;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.util.TimeUtils;
Expand All @@ -27,12 +28,20 @@
import static io.micrometer.core.instrument.MockClock.clock;
import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for queries.
*
* @author Jon Schneider
* @author Johnny Lim
*/
class QueryTest {
HealthMeterRegistry registry = HealthMeterRegistry
.builder(HealthConfig.DEFAULT)
// just so my.timer doesn't get filtered out eagerly
.serviceLevelObjectives(ServiceLevelObjective.build("timer")
.count(s -> s.name("my.timer")).isGreaterThan(0))
.serviceLevelObjectives(ServiceLevelObjective.build("function.counter")
.count(s -> s.name("my.function.counter")).isGreaterThan(0))
.clock(new MockClock())
.build();

Expand All @@ -44,6 +53,8 @@ void before() {
t2.record(2, TimeUnit.SECONDS);
t2.record(2, TimeUnit.SECONDS);

FunctionCounter.builder("my.function.counter", 1d, Number::doubleValue).register(registry);

clock(registry).addSeconds(10);
registry.tick();
}
Expand All @@ -58,6 +69,15 @@ void count() {
).isEqualTo(3);
}

@Test
void countWhenMeterIsFunctionCounter() {
assertThat(
ServiceLevelObjective.build("function.counter.objective")
.count(s -> s.name("my.function.counter"))
.getValue(registry)
).isEqualTo(1);
}

@Test
void max() {
assertThat(
Expand Down

0 comments on commit f2fe365

Please sign in to comment.