/** * Clears all state from metrics. If new requests come in instances will be recreated and metrics started from scratch. */ /* package */ static void reset() { for (HystrixCommandMetrics metricsInstance: getInstances()) { metricsInstance.unsubscribeAll(); } metrics.clear(); }
@Override public Map<HystrixCommandKey, HystrixCommandUtilization> call(Long timestamp) { Map<HystrixCommandKey, HystrixCommandUtilization> commandUtilizationPerKey = new HashMap<HystrixCommandKey, HystrixCommandUtilization>(); for (HystrixCommandMetrics commandMetrics: HystrixCommandMetrics.getInstances()) { HystrixCommandKey commandKey = commandMetrics.getCommandKey(); commandUtilizationPerKey.put(commandKey, sampleCommandUtilization(commandMetrics)); } return commandUtilizationPerKey; } };
@Override public Map<HystrixCommandKey, HystrixCommandConfiguration> call(Long timestamp) { Map<HystrixCommandKey, HystrixCommandConfiguration> commandConfigPerKey = new HashMap<HystrixCommandKey, HystrixCommandConfiguration>(); for (HystrixCommandMetrics commandMetrics: HystrixCommandMetrics.getInstances()) { HystrixCommandKey commandKey = commandMetrics.getCommandKey(); HystrixThreadPoolKey threadPoolKey = commandMetrics.getThreadPoolKey(); HystrixCommandGroupKey groupKey = commandMetrics.getCommandGroup(); commandConfigPerKey.put(commandKey, sampleCommandConfiguration(commandKey, threadPoolKey, groupKey, commandMetrics.getProperties())); } return commandConfigPerKey; } };
@Before public void init() { for (HystrixCommandMetrics metricsInstance: HystrixCommandMetrics.getInstances()) { metricsInstance.resetStream(); } HystrixCommandMetrics.reset(); HystrixCircuitBreaker.Factory.reset(); Hystrix.reset(); }
/** * Clears all state from metrics. If new requests come in instances will be recreated and metrics started from scratch. */ /* package */ static void reset() { for (HystrixCommandMetrics metricsInstance: getInstances()) { metricsInstance.unsubscribeAll(); } metrics.clear(); }
@Override public Map<HystrixCommandKey, HystrixCommandUtilization> call(Long timestamp) { Map<HystrixCommandKey, HystrixCommandUtilization> commandUtilizationPerKey = new HashMap<HystrixCommandKey, HystrixCommandUtilization>(); for (HystrixCommandMetrics commandMetrics: HystrixCommandMetrics.getInstances()) { HystrixCommandKey commandKey = commandMetrics.getCommandKey(); commandUtilizationPerKey.put(commandKey, sampleCommandUtilization(commandMetrics)); } return commandUtilizationPerKey; } };
@Inject public HystrixCommandMetricsPeriodicPublisher(HystrixModule.Config config, ExecController execController) { super( execController.getExecutor(), integer -> HystrixCommandMetrics.getInstances(), Duration.ofSeconds(config.getStreamInterval()) ); }
@Override public Map<HystrixCommandKey, HystrixCommandConfiguration> call(Long timestamp) { Map<HystrixCommandKey, HystrixCommandConfiguration> commandConfigPerKey = new HashMap<HystrixCommandKey, HystrixCommandConfiguration>(); for (HystrixCommandMetrics commandMetrics: HystrixCommandMetrics.getInstances()) { HystrixCommandKey commandKey = commandMetrics.getCommandKey(); HystrixThreadPoolKey threadPoolKey = commandMetrics.getThreadPoolKey(); HystrixCommandGroupKey groupKey = commandMetrics.getCommandGroup(); commandConfigPerKey.put(commandKey, sampleCommandConfiguration(commandKey, threadPoolKey, groupKey, commandMetrics.getProperties())); } return commandConfigPerKey; } };
@Override public void call(Long tick) { if (!ctx.channel().isOpen()) { subscription.unsubscribe(); logger.debug("Stopping Hystrix Turbine stream to connection"); return; } try { Collection<HystrixCommandMetrics> hystrixCommandMetrics = HystrixCommandMetrics.getInstances(); Collection<HystrixThreadPoolMetrics> hystrixThreadPoolMetrics = HystrixThreadPoolMetrics.getInstances(); logger.debug("Found {} hystrix command metrics", hystrixCommandMetrics.size()); logger.debug("Found {} hystrix thread pool metrics", hystrixThreadPoolMetrics.size()); for (HystrixCommandMetrics commandMetrics : hystrixCommandMetrics) { writeMetric(toJson(commandMetrics), ctx); } for (HystrixThreadPoolMetrics threadPoolMetrics : hystrixThreadPoolMetrics) { writeMetric(toJson(threadPoolMetrics), ctx); } if (hystrixCommandMetrics.isEmpty() && hystrixThreadPoolMetrics.isEmpty()) { ctx.writeAndFlush(PING.duplicate()).addListener(CLOSE_ON_FAILURE); } else { ctx.flush(); } } catch (Exception e) { logger.error("Unexpected error", e); subject.onError(e); } } });
@Override public String call() { List<TpsAndLatencyData> tpsAndLatencyData = HystrixCommandMetrics.getInstances().stream().map(instance -> new TpsAndLatencyData(instance.getRollingCount(HystrixEventType.SUCCESS), instance.getRollingCount(HystrixEventType.FAILURE) , instance.getExecutionTimeMean(), instance.getProperties().metricsRollingStatisticalWindowInMilliseconds().get())) .collect(Collectors.toList()); return calculateTpsAndLatency(tpsAndLatencyData); } };
@Override protected void doHealthCheck(Builder builder) throws Exception { List<String> openCircuitBreakers = new ArrayList<>(); // Collect all open circuit breakers from Hystrix for (HystrixCommandMetrics metrics : HystrixCommandMetrics.getInstances()) { HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory .getInstance(metrics.getCommandKey()); if (circuitBreaker != null && circuitBreaker.isOpen()) { openCircuitBreakers.add(metrics.getCommandGroup().name() + "::" + metrics.getCommandKey().name()); } } // If there is at least one open circuit report OUT_OF_SERVICE adding the command // group // and key name if (!openCircuitBreakers.isEmpty()) { builder.status(CIRCUIT_OPEN).withDetail("openCircuitBreakers", openCircuitBreakers); } else { builder.up(); } }
@Override public void run() { try { for (HystrixCommandMetrics commandMetrics : HystrixCommandMetrics.getInstances()) { String jsonString = getCommandJson(commandMetrics); listener.handleJsonMetric(jsonString); } for (HystrixThreadPoolMetrics threadPoolMetrics : HystrixThreadPoolMetrics.getInstances()) { if (hasExecutedCommandsOnThread(threadPoolMetrics)) { String jsonString = getThreadPoolJson(threadPoolMetrics); listener.handleJsonMetric(jsonString); } } for (HystrixCollapserMetrics collapserMetrics : HystrixCollapserMetrics.getInstances()) { String jsonString = getCollapserJson(collapserMetrics); listener.handleJsonMetric(jsonString); } } catch (Exception e) { logger.warn("Failed to output metrics as JSON", e); // shutdown pause(); return; } }