@Override public void inc(long n) { delegate.inc(n); }
/** * Updates fallback metrics and adjust failed invocations based on outcome of fallback. */ private void updateMetrics(Throwable throwable) { final Method method = context.getMethod(); FaultToleranceMetrics.getCounter(method, FaultToleranceMetrics.FALLBACK_CALLS_TOTAL).inc(); // If fallback was successful, it is not a failed invocation if (throwable == null) { FaultToleranceMetrics.getCounter(method, FaultToleranceMetrics.INVOCATIONS_FAILED_TOTAL).dec(); } } }
@Override public long getCount() { return delegate.getCount(); }
@Override public void dec() { delegate.dec(); }
@Override public void dec(long n) { delegate.dec(n); }
@Override public void inc() { delegate.inc(); }
@InSequence(3) public void incrementCountersFromInjection(@Metric(name = "ratioGauge", absolute = true) Gauge<Double> gauge, @Metric(name = "counter1", absolute = true) Counter counter1, @Metric(name = "counter2", absolute = true) Counter counter2) { counter1.inc(Math.round(Math.random() * Integer.MAX_VALUE)); counter2.inc(Math.round(Math.random() * Integer.MAX_VALUE)); assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) counter1.getCount()) / ((double) counter2.getCount())))); assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey("ratioGauge")); @SuppressWarnings("unchecked") Gauge<Double> gaugeFromRegistry = (Gauge<Double>) registry.getGauges().get("ratioGauge"); assertThat("Gauge values from registry and injection do not match", gauge.getValue(), is(equalTo(gaugeFromRegistry.getValue()))); } }
static long getCounter(Object bean, String methodName, String name, Class<?>... params) throws Exception { Method method = getRealClass(bean).getMethod(methodName, params); return getCounter(method, name).getCount(); }
@Override protected void postInvoke(Counter counter, Counted annot, InvocationContext context, Exception ex) { if (!annot.monotonic()) { counter.dec(); } } }
@Override protected Object prepareAndInvoke(Counter counter, Counted annot, InvocationContext context) throws Exception { counter.inc(); return context.proceed(); }
private <E extends Member & AnnotatedElement> Object applyInterceptor(InvocationContext context, E member) throws Exception { Metadata metadata = AnnotationMetadata.buildMetadata(bean.getBeanClass(), member, Counted.class); Counter counter = applicationRegistry.getCounters().get(metadata.getName()); if (counter == null) { throw new IllegalStateException("No counter with name [" + metadata.getName() + "] found in registry [" + applicationRegistry + "]"); } counter.inc(); try { return context.proceed(); } finally { Counted annotation = AnnotationMetadata.getAnnotation(bean.getBeanClass(), member, Counted.class); if(annotation != null && !annotation.monotonic()) { counter.dec(); } } } }
@Test @InSequence(2) public void incrementCountersFromRegistry() { assertThat("Counters are not registered correctly", registry.getCounters(), allOf( hasKey("counter1"), hasKey("counter2"), not(hasKey("not_registered_counter")) ) ); Counter counter1 = registry.getCounters().get("counter1"); Counter counter2 = registry.getCounters().get("counter2"); assertThat("Gauge is not registered correctly", registry.getGauges(), hasKey("ratioGauge")); @SuppressWarnings("unchecked") Gauge<Double> gauge = (Gauge<Double>) registry.getGauges().get("ratioGauge"); counter1.inc(Math.round(Math.random() * Integer.MAX_VALUE)); counter2.inc(Math.round(Math.random() * Integer.MAX_VALUE)); assertThat("Gauge value is incorrect", gauge.getValue(), is(equalTo(((double) counter1.getCount()) / ((double) counter2.getCount())))); }
@Override protected void postInvoke(Counter counter, Counted annot, InvocationContext context, Exception ex) { if (!annot.monotonic()) { counter.dec(); } } }
private void updateMetricsAfter(Throwable throwable, boolean wasBreakerOpen, boolean isClosedNow, boolean breakerWillOpen) { if (!isFaultToleranceMetricsEnabled()) { return; } assert introspector.hasCircuitBreaker(); Method method = introspector.getMethod(); if (throwable == null) { // If no errors increment success counter getCounter(method, BREAKER_CALLS_SUCCEEDED_TOTAL).inc(); } else if (!wasBreakerOpen) { // If error and breaker was closed, increment failed counter getCounter(method, BREAKER_CALLS_FAILED_TOTAL).inc(); // If it will open, increment counter if (breakerWillOpen) { getCounter(method, BREAKER_OPENED_TOTAL).inc(); } } // If breaker was open and still is, increment prevented counter if (wasBreakerOpen && !isClosedNow) { getCounter(method, BREAKER_CALLS_PREVENTED_TOTAL).inc(); } }