Search in sources :

Example 1 with LongTaskTimer

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

the class CompositeLongTaskTimerTest method mapIdsToEachLongTaskTimerInComposite.

@Test
void mapIdsToEachLongTaskTimerInComposite() {
    MockClock clock = new MockClock();
    MeterRegistry s1 = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock);
    LongTaskTimer anotherTimer = s1.more().longTaskTimer("long.task");
    LongTaskTimer.Sample anotherSample = anotherTimer.start();
    clock.add(10, TimeUnit.NANOSECONDS);
    CompositeMeterRegistry registry = new CompositeMeterRegistry(clock);
    registry.add(s1);
    LongTaskTimer longTaskTimer = registry.more().longTaskTimer("long.task");
    LongTaskTimer.Sample sample = longTaskTimer.start();
    clock.add(100, TimeUnit.NANOSECONDS);
    assertThat(anotherSample.stop()).isEqualTo(110);
    // if this fails, the composite is using a timer ID that overlaps with a separate timer in a member
    // of the composite rather than mapping the ID to a separate ID in the composite member.
    assertThat(sample.stop()).isEqualTo(100);
}
Also used : SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) MockClock(io.micrometer.core.instrument.MockClock) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Test(org.junit.jupiter.api.Test)

Example 2 with LongTaskTimer

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

the class MetricsRequestEventListener method onEvent.

@Override
public void onEvent(RequestEvent event) {
    ContainerRequest containerRequest = event.getContainerRequest();
    Set<Timed> timedAnnotations;
    switch(event.getType()) {
        case ON_EXCEPTION:
            if (!(event.getException() instanceof NotFoundException)) {
                break;
            }
        case REQUEST_MATCHED:
            timedAnnotations = annotations(event);
            timedAnnotationsOnRequest.put(containerRequest, timedAnnotations);
            shortTaskSample.put(containerRequest, Timer.start(registry));
            List<LongTaskTimer.Sample> longTaskSamples = longTaskTimers(timedAnnotations, event).stream().map(LongTaskTimer::start).collect(Collectors.toList());
            if (!longTaskSamples.isEmpty()) {
                this.longTaskSamples.put(containerRequest, longTaskSamples);
            }
            break;
        case FINISHED:
            timedAnnotations = timedAnnotationsOnRequest.remove(containerRequest);
            Timer.Sample shortSample = shortTaskSample.remove(containerRequest);
            if (shortSample != null) {
                for (Timer timer : shortTimers(timedAnnotations, event)) {
                    shortSample.stop(timer);
                }
            }
            Collection<LongTaskTimer.Sample> longSamples = this.longTaskSamples.remove(containerRequest);
            if (longSamples != null) {
                for (LongTaskTimer.Sample longSample : longSamples) {
                    longSample.stop();
                }
            }
            break;
    }
}
Also used : LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Timer(io.micrometer.core.instrument.Timer) Timed(io.micrometer.core.annotation.Timed) NotFoundException(javax.ws.rs.NotFoundException) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer)

Example 3 with LongTaskTimer

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

the class LongTaskTimerSample method main.

public static void main(String[] args) {
    MeterRegistry registry = SampleConfig.myMonitoringSystem();
    LongTaskTimer timer = registry.more().longTaskTimer("longTaskTimer");
    RandomEngine r = new MersenneTwister64(0);
    Normal incomingRequests = new Normal(0, 1, r);
    Normal duration = new Normal(30, 50, r);
    AtomicInteger latencyForThisSecond = new AtomicInteger(duration.nextInt());
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> latencyForThisSecond.set(duration.nextInt())).subscribe();
    final Map<LongTaskTimer.Sample, CountDownLatch> tasks = new ConcurrentHashMap<>();
    // the potential for an "incoming request" every 10 ms
    Flux.interval(Duration.ofSeconds(1)).doOnEach(d -> {
        if (incomingRequests.nextDouble() + 0.4 > 0 && tasks.isEmpty()) {
            int taskDur;
            while ((taskDur = duration.nextInt()) < 0) ;
            synchronized (tasks) {
                tasks.put(timer.start(), new CountDownLatch(taskDur));
            }
        }
        synchronized (tasks) {
            for (Map.Entry<LongTaskTimer.Sample, CountDownLatch> e : tasks.entrySet()) {
                e.getValue().countDown();
                if (e.getValue().getCount() == 0) {
                    e.getKey().stop();
                    tasks.remove(e.getKey());
                }
            }
        }
    }).blockLast();
}
Also used : LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Flux(reactor.core.publisher.Flux) CountDownLatch(java.util.concurrent.CountDownLatch) 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) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MersenneTwister64(cern.jet.random.engine.MersenneTwister64) RandomEngine(cern.jet.random.engine.RandomEngine) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Normal(cern.jet.random.Normal) CountDownLatch(java.util.concurrent.CountDownLatch) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MeterRegistry(io.micrometer.core.instrument.MeterRegistry)

Example 4 with LongTaskTimer

use of io.micrometer.core.instrument.LongTaskTimer in project tutorials by eugenp.

the class MicrometerAtlasTest method givenLongTimer_whenRunTasks_thenTimerRecorded.

@Test
public void givenLongTimer_whenRunTasks_thenTimerRecorded() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    LongTaskTimer longTaskTimer = LongTaskTimer.builder("3rdPartyService").register(registry);
    long currentTaskId = longTaskTimer.start();
    try {
        TimeUnit.MILLISECONDS.sleep(2);
    } catch (InterruptedException ignored) {
    }
    long timeElapsed = longTaskTimer.stop(currentTaskId);
    assertTrue(timeElapsed / (int) 1e6 == 2);
}
Also used : SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer) Test(org.junit.Test)

Example 5 with LongTaskTimer

use of io.micrometer.core.instrument.LongTaskTimer in project spring-boot by spring-projects.

the class LongTaskTimingHandlerInterceptor method getLongTaskTimerSamples.

private Collection<LongTaskTimer.Sample> getLongTaskTimerSamples(HttpServletRequest request, Object handler, Set<Timed> annotations) {
    List<LongTaskTimer.Sample> samples = new ArrayList<>();
    try {
        annotations.stream().filter(Timed::longTask).forEach((annotation) -> {
            Iterable<Tag> tags = this.tagsProvider.getLongRequestTags(request, handler);
            LongTaskTimer.Builder builder = LongTaskTimer.builder(annotation).tags(tags);
            LongTaskTimer timer = builder.register(this.registry);
            samples.add(timer.start());
        });
    } catch (Exception ex) {
        logger.warn("Failed to start long task timers", ex);
    // Allow request-response exchange to continue, unaffected by metrics problem
    }
    return samples;
}
Also used : ArrayList(java.util.ArrayList) Tag(io.micrometer.core.instrument.Tag) LongTaskTimer(io.micrometer.core.instrument.LongTaskTimer)

Aggregations

LongTaskTimer (io.micrometer.core.instrument.LongTaskTimer)7 Timed (io.micrometer.core.annotation.Timed)2 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)2 Timer (io.micrometer.core.instrument.Timer)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 Test (org.junit.jupiter.api.Test)2 Normal (cern.jet.random.Normal)1 MersenneTwister64 (cern.jet.random.engine.MersenneTwister64)1 RandomEngine (cern.jet.random.engine.RandomEngine)1 MockClock (io.micrometer.core.instrument.MockClock)1 Tag (io.micrometer.core.instrument.Tag)1 SampleConfig (io.micrometer.core.samples.utils.SampleConfig)1 Method (java.lang.reflect.Method)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NotFoundException (javax.ws.rs.NotFoundException)1