@Override public Integer getValue() { return metrics.getHealthCounts().getErrorPercentage(); } });
@Override public Integer getValue() { return metrics.getCurrentConcurrentExecutionCount(); } });
@Override public Integer getValue() { return metrics.getExecutionTimePercentile(5); } });
@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; } };
/** * 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 String apply(HystrixCommandMetrics commandMetrics) throws Exception { HystrixCommandKey key = commandMetrics.getCommandKey(); HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key); json.writeStringField("type", "HystrixCommand"); json.writeStringField("name", key.name()); json.writeStringField("group", commandMetrics.getCommandGroup().name()); json.writeNumberField("currentTime", System.currentTimeMillis()); HystrixCommandMetrics.HealthCounts healthCounts = commandMetrics.getHealthCounts(); json.writeNumberField("errorPercentage", healthCounts.getErrorPercentage()); json.writeNumberField("errorCount", healthCounts.getErrorCount()); json.writeNumberField("requestCount", healthCounts.getTotalRequests()); json.writeNumberField("rollingCountBadRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.BAD_REQUEST)); json.writeNumberField("rollingCountCollapsedRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSED)); json.writeNumberField("rollingCountEmit", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EMIT)); json.writeNumberField("rollingCountExceptionsThrown", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN)); json.writeNumberField("rollingCountFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FAILURE)); json.writeNumberField("rollingCountEmit", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_EMIT)); json.writeNumberField("rollingCountFallbackFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_FAILURE)); json.writeNumberField("rollingCountFallbackRejection", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_REJECTION)); json.writeNumberField("rollingCountFallbackSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_SUCCESS)); json.writeNumberField("rollingCountResponsesFromCache", commandMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE)); json.writeNumberField("rollingCountSemaphoreRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SEMAPHORE_REJECTED)); json.writeNumberField("rollingCountShortCircuited", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED)); json.writeNumberField("rollingCountSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS)); json.writeNumberField("rollingCountThreadPoolRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED)); json.writeNumberField("rollingCountTimeout", commandMetrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT));
public CircutBreakerEvent(HystrixCommandKey commandKey, Type type) { super(type); HystrixCommandMetrics hystrixCommandMetrics = HystrixCommandMetrics.getInstance(commandKey); if (hystrixCommandMetrics != null) { if (hystrixCommandMetrics.getCommandGroup() instanceof CustomizeCommandGroupKey) { CustomizeCommandGroupKey customCommandGroupKey = (CustomizeCommandGroupKey) hystrixCommandMetrics.getCommandGroup(); this.microservice = customCommandGroupKey.getInstance().getMicroserviceName(); this.role = customCommandGroupKey.getInstance().getInvocationType().name(); this.schema = customCommandGroupKey.getInstance().getSchemaId(); this.operation = customCommandGroupKey.getInstance().getOperationName(); } this.currentTotalRequest = hystrixCommandMetrics.getHealthCounts().getTotalRequests(); this.currentErrorPercentage = hystrixCommandMetrics.getHealthCounts().getErrorCount(); this.currentErrorCount = hystrixCommandMetrics.getHealthCounts().getErrorPercentage(); this.requestVolumeThreshold = hystrixCommandMetrics.getProperties().circuitBreakerRequestVolumeThreshold().get(); this.sleepWindowInMilliseconds = hystrixCommandMetrics.getProperties().circuitBreakerSleepWindowInMilliseconds().get(); this.errorThresholdPercentage = hystrixCommandMetrics.getProperties().circuitBreakerErrorThresholdPercentage().get(); } }
@Override public Long getValue() { return metrics.getRollingCount(event); } });
private String getStatsStringFromMetrics(HystrixCommandMetrics metrics) { StringBuilder m = new StringBuilder(); if (metrics != null) { HealthCounts health = metrics.getHealthCounts(); m.append("Requests: ").append(health.getTotalRequests()).append(" "); m.append("Errors: ").append(health.getErrorCount()).append(" (").append(health.getErrorPercentage()).append("%) "); m.append("Mean: ").append(metrics.getExecutionTimePercentile(50)).append(" "); m.append("75th: ").append(metrics.getExecutionTimePercentile(75)).append(" "); m.append("90th: ").append(metrics.getExecutionTimePercentile(90)).append(" "); m.append("99th: ").append(metrics.getExecutionTimePercentile(99)).append(" "); } return m.toString(); }
@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); } };
private static HystrixCommandMetrics initMetrics(HystrixCommandMetrics fromConstructor, HystrixCommandGroupKey groupKey, HystrixThreadPoolKey threadPoolKey, HystrixCommandKey commandKey, HystrixCommandProperties properties) { if (fromConstructor == null) { return HystrixCommandMetrics.getInstance(commandKey, groupKey, threadPoolKey, properties); } else { return fromConstructor; } }
@Override public Integer getValue() { return metrics.getTotalTimePercentile(50); } });
@Override public boolean isOpen() { System.out.println("metrics : " + metrics.getCommandKey().name() + " : " + metrics.getHealthCounts()); if (forceShortCircuit) { return true; } else { return metrics.getHealthCounts().getErrorCount() >= 3; } }
@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(); } }
@Before public void init() { for (HystrixCommandMetrics metricsInstance: HystrixCommandMetrics.getInstances()) { metricsInstance.resetStream(); } HystrixCommandMetrics.reset(); HystrixCircuitBreaker.Factory.reset(); Hystrix.reset(); }
@Override public Integer getValue() { return metrics.getExecutionTimeMean(); } });
@Inject public HystrixCommandMetricsPeriodicPublisher(HystrixModule.Config config, ExecController execController) { super( execController.getExecutor(), integer -> HystrixCommandMetrics.getInstances(), Duration.ofSeconds(config.getStreamInterval()) ); }
@Override public void onNext(HystrixDashboardStream.DashboardData dashboardData) { System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Received data with : " + dashboardData.commandMetrics.size() + " commands"); for (HystrixCommandMetrics metrics : dashboardData.commandMetrics) { if (metrics.getCommandKey().equals(commandKey)) { commandShowsUp.set(true); } } } });
public static String toJson(HystrixCommandMetrics commandMetrics) throws IOException { HystrixCommandKey key = commandMetrics.getCommandKey(); HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key); json.writeStringField("type", "HystrixCommand"); json.writeStringField("name", key.name()); json.writeStringField("group", commandMetrics.getCommandGroup().name()); json.writeNumberField("currentTime", System.currentTimeMillis()); HealthCounts healthCounts = commandMetrics.getHealthCounts(); json.writeNumberField("errorPercentage", healthCounts.getErrorPercentage()); json.writeNumberField("errorCount", healthCounts.getErrorCount()); json.writeNumberField("rollingCountCollapsedRequests", commandMetrics.getRollingCount(HystrixRollingNumberEvent.COLLAPSED)); json.writeNumberField("rollingCountExceptionsThrown", commandMetrics.getRollingCount(HystrixRollingNumberEvent.EXCEPTION_THROWN)); json.writeNumberField("rollingCountFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FAILURE)); json.writeNumberField("rollingCountFallbackFailure", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_FAILURE)); json.writeNumberField("rollingCountFallbackRejection", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_REJECTION)); json.writeNumberField("rollingCountFallbackSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.FALLBACK_SUCCESS)); json.writeNumberField("rollingCountResponsesFromCache", commandMetrics.getRollingCount(HystrixRollingNumberEvent.RESPONSE_FROM_CACHE)); json.writeNumberField("rollingCountSemaphoreRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SEMAPHORE_REJECTED)); json.writeNumberField("rollingCountShortCircuited", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED)); json.writeNumberField("rollingCountSuccess", commandMetrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS)); json.writeNumberField("rollingCountThreadPoolRejected", commandMetrics.getRollingCount(HystrixRollingNumberEvent.THREAD_POOL_REJECTED)); json.writeNumberField("rollingCountTimeout", commandMetrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT)); json.writeNumberField("currentConcurrentExecutionCount", commandMetrics.getCurrentConcurrentExecutionCount()); json.writeNumberField("latencyExecute_mean", commandMetrics.getExecutionTimeMean()); json.writeObjectFieldStart("latencyExecute");