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);
}
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;
}
}
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();
}
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);
}
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;
}
Aggregations