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(); } } }
@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()); } } } }
@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); }
@Override public void onTimeout(AsyncEvent event) throws IOException { context.stop(); activeRequests.dec(); timeoutsMeter.mark(); done = true; }
@Override public void onComplete(AsyncEvent event) throws IOException { if (!done) { HttpServletResponse suppliedResponse = (HttpServletResponse) event.getSuppliedResponse(); context.stop(); activeRequests.dec(); markMeterForStatusCode(suppliedResponse.getStatus()); } }
@Override public void onError(AsyncEvent event) throws IOException { context.stop(); activeRequests.dec(); errorsMeter.mark(); done = true; }
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(); } }