HystrixCommandMetrics extends HystrixMetrics { public HealthCounts getHealthCounts() { return healthCountsStream.getLatest(); } HystrixCommandMetrics(final HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixThreadPoolKey threadPoolKey, HystrixCommandProperties properties, HystrixEventNotifier eventNotifier); static HystrixCommandMetrics getInstance(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixCommandProperties properties); static HystrixCommandMetrics getInstance(HystrixCommandKey key, HystrixCommandGroupKey commandGroup, HystrixThreadPoolKey threadPoolKey, HystrixCommandProperties properties); static HystrixCommandMetrics getInstance(HystrixCommandKey key); static Collection<HystrixCommandMetrics> getInstances(); HystrixCommandKey getCommandKey(); HystrixCommandGroupKey getCommandGroup(); HystrixThreadPoolKey getThreadPoolKey(); HystrixCommandProperties getProperties(); long getRollingCount(HystrixEventType eventType); long getCumulativeCount(HystrixEventType eventType); @Override long getCumulativeCount(HystrixRollingNumberEvent event); @Override long getRollingCount(HystrixRollingNumberEvent event); int getExecutionTimePercentile(double percentile); int getExecutionTimeMean(); int getTotalTimePercentile(double percentile); int getTotalTimeMean(); long getRollingMaxConcurrentExecutions(); int getCurrentConcurrentExecutionCount(); HealthCounts getHealthCounts(); static final Func2<long[], HystrixCommandCompletion, long[]> appendEventToBucket; static final Func2<long[], long[], long[]> bucketAggregator; }
@Test public void testGetErrorPercentage() { String key = "cmd-metrics-A"; try { HystrixCommand<Boolean> cmd1 = new SuccessCommand(key, 1); HystrixCommandMetrics metrics = cmd1.metrics; cmd1.execute(); Thread.sleep(100); assertEquals(0, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1); cmd2.execute(); Thread.sleep(100); assertEquals(50, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd3 = new SuccessCommand(key, 1); HystrixCommand<Boolean> cmd4 = new SuccessCommand(key, 1); cmd3.execute(); cmd4.execute(); Thread.sleep(100); assertEquals(25, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd5 = new TimeoutCommand(key); HystrixCommand<Boolean> cmd6 = new TimeoutCommand(key); cmd5.execute(); cmd6.execute(); Thread.sleep(100); assertEquals(50, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd7 = new SuccessCommand(key, 1); HystrixCommand<Boolean> cmd8 = new SuccessCommand(key, 1); HystrixCommand<Boolean> cmd9 = new SuccessCommand(key, 1); cmd7.execute(); cmd8.execute(); cmd9.execute(); HystrixCommand<Boolean> cmd10 = new SuccessCommand(key, 60); cmd10.execute(); Thread.sleep(100); assertEquals(30, metrics.getHealthCounts().getErrorPercentage()); } catch (Exception e) { e.printStackTrace(); fail("Error occurred: " + e.getMessage()); } }
@Test public void testBadRequestsDoNotAffectErrorPercentage() { String key = "cmd-metrics-B"; try { HystrixCommand<Boolean> cmd1 = new SuccessCommand(key ,1); HystrixCommandMetrics metrics = cmd1.metrics; cmd1.execute(); Thread.sleep(100); assertEquals(0, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd2 = new FailureCommand(key, 1); cmd2.execute(); Thread.sleep(100); assertEquals(50, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd3 = new BadRequestCommand(key, 1); HystrixCommand<Boolean> cmd4 = new BadRequestCommand(key, 1); try { cmd3.execute(); } catch (HystrixBadRequestException ex) { System.out.println("Caught expected HystrixBadRequestException from cmd3"); } try { cmd4.execute(); } catch (HystrixBadRequestException ex) { System.out.println("Caught expected HystrixBadRequestException from cmd4"); } Thread.sleep(100); assertEquals(50, metrics.getHealthCounts().getErrorPercentage()); HystrixCommand<Boolean> cmd5 = new FailureCommand(key, 1); HystrixCommand<Boolean> cmd6 = new FailureCommand(key, 1); cmd5.execute(); cmd6.execute(); Thread.sleep(100); assertEquals(75, metrics.getHealthCounts().getErrorPercentage()); } catch (Exception e) { e.printStackTrace(); fail("Error occurred : " + e.getMessage()); } }