@Override public void time(Runnable event) { delegate.time(event); }
@Override protected Object prepareAndInvoke(Timer timer, Timed annotation, InvocationContext context) throws Exception { return timer.time(context::proceed); } }
@Override public <T> T time(Callable<T> event) throws Exception { return delegate.time(event); }
@Override public Context time() { return delegate.time(); }
@Override protected Object prepareAndInvoke(Timer timer, Timed annotation, InvocationContext context) throws Exception { return timer.time(context::proceed); } }
public void timeMe() { Timer timer = metrics.timer("metricTest.test1.timer"); Timer.Context context = timer.time(); try { Thread.sleep((long) (Math.random() * 1000)); } catch (InterruptedException e) { } finally { context.stop(); } }
@AroundInvoke public Object onMethod(final InvocationContext context) throws Exception { return findTimer(context.getMethod()).time(context::proceed); }
@Test @InSequence(2) public void testTime() throws Exception { Timer timer = registry.timer("testTime"); double beforeStartTime = System.nanoTime(); Context context = timer.time(); double afterStartTime = System.nanoTime(); Thread.sleep(1000); double beforeStopTime = System.nanoTime(); double time = context.stop(); double afterStopTime = System.nanoTime(); double delta = (afterStartTime - beforeStartTime) + (afterStopTime - beforeStopTime); Assert.assertEquals(beforeStopTime - beforeStartTime, time, delta); }
@Test @InSequence(5) public void timesRunnableInstances() throws Exception { Timer timer = registry.timer("testRunnable"); final AtomicBoolean called = new AtomicBoolean(); timer.time(() -> called.set(true)); Assert.assertEquals(timer.getCount(), 1); Assert.assertEquals(called.get(), true); }
@AroundConstruct public Object onConstructor(final InvocationContext context) throws Exception { return findTimer(context.getConstructor()).time(context::proceed); }
private <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E member) throws Exception { Metadata metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Timed.class); Timer timer = applicationRegistry.getTimers().get(metadata.getName()); if (timer == null) { throw new IllegalStateException("No timer with name [" + metadata.getName() + "] found in registry [" + applicationRegistry + "]"); } Timer.Context timerContext = timer.time(); try { return context.proceed(); } finally { timerContext.stop(); } } }
@Test @InSequence(4) public void timesCallableInstances() throws Exception { Timer timer = registry.timer("testCallable"); final String value = timer.time(() -> "one"); Assert.assertEquals(timer.getCount(), 1); Assert.assertEquals(value, "one"); }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { final StatusExposingServletResponse wrappedResponse = new StatusExposingServletResponse((HttpServletResponse) response); activeRequests.inc(); final Timer.Context context = requestTimer.time(); boolean error = false; try { chain.doFilter(request, wrappedResponse); } catch (IOException | RuntimeException | ServletException e) { error = true; throw e; } finally { if (!error && request.isAsyncStarted()) { request.getAsyncContext().addListener(new AsyncResultListener(context)); } else { context.stop(); activeRequests.dec(); if (error) { errorsMeter.mark(); } else { markMeterForStatusCode(wrappedResponse.getStatus()); } } } }