/** * Get the rolling count for the given {@link HystrixRollingNumberEvent}. * <p> * The rolling window is defined by {@link HystrixCommandProperties#metricsRollingStatisticalWindowInMilliseconds()}. * * @param event * {@link HystrixRollingNumberEvent} of the event to retrieve a sum for * @return long rolling count */ public long getRollingCount(HystrixRollingNumberEvent event) { return counter.getRollingSum(event); }
@Test public void testEmptySum() { MockedTime time = new MockedTime(); HystrixRollingNumberEvent type = HystrixRollingNumberEvent.COLLAPSED; HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); assertEquals(0, counter.getRollingSum(type)); }
assertEquals(4, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE)); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE));
private void testCounterType(HystrixRollingNumberEvent type) { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(type); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 1 assertEquals(1, counter.buckets.getLast().getAdder(type).sum()); assertEquals(1, counter.getRollingSum(type)); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds * 3); // increment again in latest bucket counter.increment(type); // we should have 4 buckets assertEquals(4, counter.buckets.size()); // the counts of the last bucket assertEquals(1, counter.buckets.getLast().getAdder(type).sum()); // the total counts assertEquals(2, counter.getRollingSum(type)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
@Test public void testTimeout() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(HystrixRollingNumberEvent.TIMEOUT); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 1 assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT)); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds * 3); // incremenet again in latest bucket counter.increment(HystrixRollingNumberEvent.TIMEOUT); // we should have 4 buckets assertEquals(4, counter.buckets.size()); // the counts of the last bucket assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.TIMEOUT).sum()); // the total counts assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
@Test public void testShortCircuited() { MockedTime time = new MockedTime(); try { HystrixRollingNumber counter = new HystrixRollingNumber(time, 200, 10); // increment counter.increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // we should have 1 bucket assertEquals(1, counter.buckets.size()); // the count should be 1 assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).sum()); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); // sleep to get to a new bucket time.increment(counter.bucketSizeInMillseconds * 3); // incremenet again in latest bucket counter.increment(HystrixRollingNumberEvent.SHORT_CIRCUITED); // we should have 4 buckets assertEquals(4, counter.buckets.size()); // the counts of the last bucket assertEquals(1, counter.buckets.getLast().getAdder(HystrixRollingNumberEvent.SHORT_CIRCUITED).sum()); // the total counts assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); } catch (Exception e) { e.printStackTrace(); fail("Exception: " + e.getMessage()); } }
assertEquals(6, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(5, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE)); assertEquals(3, counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT)); assertEquals(2, counter.getRollingSum(HystrixRollingNumberEvent.SHORT_CIRCUITED)); assertEquals(1, counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.FAILURE)); assertEquals(0, counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT));
/** * Get the rolling count for the given {@link HystrixRollingNumberEvent}. * <p> * The rolling window is defined by {@link HystrixCommandProperties#metricsRollingStatisticalWindowInMilliseconds()}. * * @param event * {@link HystrixRollingNumberEvent} of the event to retrieve a sum for * @return long rolling count */ public long getRollingCount(HystrixRollingNumberEvent event) { return counter.getRollingSum(event); }
public HealthCounts getHealthCounts() { // we put an interval between snapshots so high-volume commands don't // spend too much unnecessary time calculating metrics in very small time periods long lastTime = lastHealthCountsSnapshot.get(); long currentTime = System.currentTimeMillis(); if (currentTime - lastTime >= properties.getMetricsHealthSnapshotIntervalInMilliseconds() || healthCountsSnapshot == null) { if (lastHealthCountsSnapshot.compareAndSet(lastTime, currentTime)) { // our thread won setting the snapshot time so we will proceed with generating a new snapshot // losing threads will continue using the old snapshot long success = counter.getRollingSum(HystrixRollingNumberEvent.SUCCESS); long failure = counter.getRollingSum(HystrixRollingNumberEvent.FAILURE); // fallbacks occur on this long timeout = counter.getRollingSum(HystrixRollingNumberEvent.TIMEOUT); // fallbacks occur on this long threadPoolRejected = counter.getRollingSum(HystrixRollingNumberEvent.THREAD_POOL_REJECTED); // fallbacks occur on this long semaphoreRejected = counter.getRollingSum(HystrixRollingNumberEvent.SEMAPHORE_REJECTED); // fallbacks occur on this long totalCount = failure + success + timeout + threadPoolRejected + semaphoreRejected; long errorCount = failure + timeout + threadPoolRejected + semaphoreRejected; int errorPercentage = 0; if (totalCount > 0) { errorPercentage = (int) ((double) errorCount / totalCount * 100); } healthCountsSnapshot = new HealthCounts(totalCount, errorCount, errorPercentage); } } return healthCountsSnapshot; }