Skip to content

Instantly share code, notes, and snippets.

@jrask
Created December 21, 2018 09:03
Show Gist options
  • Save jrask/fd777b5299a1cbabc27bcbcd68e98ebb to your computer and use it in GitHub Desktop.
Save jrask/fd777b5299a1cbabc27bcbcd68e98ebb to your computer and use it in GitHub Desktop.
Micrometer Timer.max rotation?
package se.flapsdown.micrometer.bugs;
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Timer;
import io.micrometer.core.instrument.step.StepMeterRegistry;
import io.micrometer.core.instrument.step.StepRegistryConfig;
import java.time.Duration;
import java.util.Random;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* Is this correct behaviour for a Timer.max value?
* It is not rotated in the same way as other values which causes
* weird results.
* test, max:120.0, count:5
* test, max:120.0, count:1 < (same as prev)
* test, max:120.0, count:0 < (same but count = 0)
* test, max:183.0, count:5
* test, max:183.0, count:0
* test, max:183.0, count:1 < should be max = 5
* test, max:5.0, count:0 < here is the 5 but count = 0
* test, max:162.0, count:5
* test, max:162.0, count:1
*
*/
public class TimerMaxDoesNotRotate {
static StepRegistryConfig sc = new StepRegistryConfig() {
@Override
public Duration step() {
return Duration.ofSeconds(3);
}
@Override
public String prefix() {
return null;
}
@Override
public String get(String s) {
return null;
}
};
static StepMeterRegistry stepMeterRegistry = new StepMeterRegistry(sc, Clock.SYSTEM) {
@Override
protected TimeUnit getBaseTimeUnit() {
return TimeUnit.MILLISECONDS;
}
@Override
protected void publish() {
// Print max + count
for (Meter m : getMeters()) {
if (m instanceof Timer) {
Timer t = (Timer)m;
StringBuilder sb = new StringBuilder()
.append(getConventionName(t.getId())).append(", ")
.append("max:").append(t.max(getBaseTimeUnit())) .append(", ")
.append("count:").append(t.count());
System.out.println(sb.toString());
}
}
}
};
public static void main(String args[]) throws InterruptedException {
stepMeterRegistry.start(Executors.defaultThreadFactory());
Random durationRandom = new Random();
Timer timer = Timer.builder("test").register(stepMeterRegistry);
int cnt = 0;
while (true != false) {
timer.record(Duration.ofMillis(durationRandom.nextInt(199)));
TimeUnit.MILLISECONDS.sleep(100);
cnt++;
// Force a low duration and make sure it is published
if (cnt == 5) {
TimeUnit.SECONDS.sleep(5);
timer.record(Duration.ofMillis(5));
TimeUnit.SECONDS.sleep(5);
cnt = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment