Search in sources :

Example 16 with Timer

use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.

the class SimulatedEndpointInstrumentation method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    Timer e1Success = Timer.builder("http.server.requests").tags("uri", "/api/bar").tags("response", "200").publishPercentiles(0.5, 0.95).register(registry);
    Timer e2Success = Timer.builder("http.server.requests").tags("uri", "/api/foo").tags("response", "200").publishPercentiles(0.5, 0.95).register(registry);
    Timer e1Fail = Timer.builder("http.server.requests").tags("uri", "/api/bar").tags("response", "500").publishPercentiles(0.5, 0.95).register(registry);
    Timer e2Fail = Timer.builder("http.server.requests").tags("uri", "/api/foo").tags("response", "500").publishPercentiles(0.5, 0.95).register(registry);
    RandomEngine r = new MersenneTwister64(0);
    Normal incomingRequests = new Normal(0, 1, r);
    Normal successOrFail = new Normal(0, 1, r);
    Normal duration = new Normal(250, 50, r);
    Normal duration2 = new Normal(250, 50, r);
    AtomicInteger latencyForThisSecond = new AtomicInteger(duration.nextInt());
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond.set(duration.nextInt())).subscribe();
    AtomicInteger latencyForThisSecond2 = new AtomicInteger(duration2.nextInt());
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond2.set(duration2.nextInt())).subscribe();
    // the potential for an "incoming request" every 10 ms
    Flux.interval(Duration.ofMillis(10)).doOnEach(d -> {
        // are we going to receive a request for /api/foo?
        if (incomingRequests.nextDouble() + 0.4 > 0) {
            if (successOrFail.nextDouble() + 0.8 > 0) {
                // pretend the request took some amount of time, such that the time is
                // distributed normally with a mean of 250ms
                e1Success.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS);
            } else {
                e1Fail.record(latencyForThisSecond.get(), TimeUnit.MILLISECONDS);
            }
        }
    }).subscribe();
    // the potential for an "incoming request" every 1 ms
    Flux.interval(Duration.ofMillis(1)).doOnEach(d -> {
        // are we going to receive a request for /api/bar?
        if (incomingRequests.nextDouble() + 0.4 > 0) {
            if (successOrFail.nextDouble() + 0.8 > 0) {
                // pretend the request took some amount of time, such that the time is
                // distributed normally with a mean of 250ms
                e2Success.record(latencyForThisSecond2.get(), TimeUnit.MILLISECONDS);
            } else {
                e2Fail.record(latencyForThisSecond2.get(), TimeUnit.MILLISECONDS);
            }
        }
    }).blockLast();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) Timer(io.micrometer.core.instrument.Timer) RandomEngine(cern.jet.random.engine.RandomEngine) SampleConfig(io.micrometer.core.samples.utils.SampleConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Duration(java.time.Duration) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) Timer(io.micrometer.core.instrument.Timer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) Normal(cern.jet.random.Normal) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 17 with Timer

use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.

the class PersonController method stats.

@GetMapping("/api/stats")
public Map<String, Number> stats() {
    Timer t = registry.find("http.server.requests").tags("uri", "/api/people").timer();
    Map<String, Number> result = null;
    if (t != null) {
        result = new HashMap<>();
        result.put("count", t.count());
        result.put("max", t.max(TimeUnit.MILLISECONDS));
        result.put("mean", t.mean(TimeUnit.MILLISECONDS));
    }
    return result;
}
Also used : Timer(io.micrometer.core.instrument.Timer) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 18 with Timer

use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.

the class TimerMemory method main.

public static void main(String[] args) throws InterruptedException {
    MeterRegistry registry = SampleRegistries.wavefront();
    Timer t = null;
    for (Integer i = 0; i < 80; i++) {
        t = Timer.builder("my.timer").tag("index", i.toString()).sla(Stream.of(1, 150, 300, 500, 900, 1000, 1200, 1500, 2000, 3000, 4000).map(Duration::ofMillis).toArray(Duration[]::new)).publishPercentiles(0.95).percentilePrecision(1).register(registry);
    }
    // Breakpoint somewhere after the first couple outputs to test pause detection
    // for (int i = 0; ; i = (i + 1) % 2000) {
    // Thread.sleep(2);
    // t.record(1, TimeUnit.MILLISECONDS);
    // if (i == 1000) {
    // t.takeSnapshot().outputSummary(System.out, 1e6);
    // }
    // }
    Flux.never().blockLast();
}
Also used : Timer(io.micrometer.core.instrument.Timer) Duration(java.time.Duration) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 19 with Timer

use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.

the class TimerTest method recordZero.

@Test
@DisplayName("zero times contribute to the count of overall events but do not add to total time")
default void recordZero(MeterRegistry registry) {
    Timer t = registry.timer("myTimer");
    t.record(0, TimeUnit.MILLISECONDS);
    clock(registry).add(step());
    assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(0L, t.totalTime(TimeUnit.NANOSECONDS)));
}
Also used : Timer(io.micrometer.core.instrument.Timer) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 20 with Timer

use of io.micrometer.core.instrument.Timer in project micrometer by micrometer-metrics.

the class TimerTest method recordCallableException.

@Test
@DisplayName("callable task that throws exception is still recorded")
default void recordCallableException(MeterRegistry registry) {
    Timer t = registry.timer("myTimer");
    assertThrows(Exception.class, () -> {
        t.recordCallable(() -> {
            clock(registry).add(10, TimeUnit.NANOSECONDS);
            throw new Exception("uh oh");
        });
    });
    clock(registry).add(step());
    assertAll(() -> assertEquals(1L, t.count()), () -> assertEquals(10, t.totalTime(TimeUnit.NANOSECONDS), 1.0e-12));
}
Also used : Timer(io.micrometer.core.instrument.Timer) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Timer (io.micrometer.core.instrument.Timer)26 Test (org.junit.jupiter.api.Test)11 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)9 DisplayName (org.junit.jupiter.api.DisplayName)7 LongTaskTimer (io.micrometer.core.instrument.LongTaskTimer)6 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)6 Duration (java.time.Duration)5 TimeUnit (java.util.concurrent.TimeUnit)5 Test (org.junit.Test)5 Normal (cern.jet.random.Normal)4 MersenneTwister64 (cern.jet.random.engine.MersenneTwister64)4 RandomEngine (cern.jet.random.engine.RandomEngine)4 SampleConfig (io.micrometer.core.samples.utils.SampleConfig)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Flux (reactor.core.publisher.Flux)4 FunctionTimer (io.micrometer.core.instrument.FunctionTimer)3 Timed (io.micrometer.core.annotation.Timed)2 DistributionSummary (io.micrometer.core.instrument.DistributionSummary)2 Match (io.vertx.micrometer.Match)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1