public void Before() { context = HystrixRequestContext.InitializeContext(); HystrixCommandMetrics.Reset(); HystrixThreadPoolMetrics.Reset(); HystrixCollapserMetrics.Reset(); // clear collapsers RequestCollapserFactory.Reset(); // clear circuit breakers HystrixCircuitBreakerFactory.Reset(); HystrixPlugins.Reset(); HystrixOptionsFactory.Reset(); // clear up all streams CumulativeCollapserEventCounterStream.Reset(); CumulativeCommandEventCounterStream.Reset(); CumulativeThreadPoolEventCounterStream.Reset(); RollingCollapserBatchSizeDistributionStream.Reset(); RollingCollapserEventCounterStream.Reset(); RollingCommandEventCounterStream.Reset(); RollingCommandLatencyDistributionStream.Reset(); RollingCommandMaxConcurrencyStream.Reset(); RollingCommandUserLatencyDistributionStream.Reset(); RollingThreadPoolEventCounterStream.Reset(); RollingThreadPoolMaxConcurrencyStream.Reset(); }
internal void OnNext(DashboardData dashboardData) { if (dashboardData != null) { try { foreach (var commandMetrics in dashboardData.CommandMetrics) { var circuitBreaker = HystrixCircuitBreakerFactory.GetInstance(commandMetrics.CommandKey); var isOpen = circuitBreaker?.IsOpen; HystrixMetricsEventSource.EventLogger.CommandMetrics( commandKey: commandMetrics.CommandKey.Name, commandGroup: commandMetrics.CommandGroup.Name, isCiruitBreakerOpen: isOpen.HasValue && isOpen.Value, errorCount: commandMetrics.Healthcounts.ErrorCount, requestCount: commandMetrics.Healthcounts.TotalRequests, currentConcurrentExecutionCount: commandMetrics.CurrentConcurrentExecutionCount, latencyExecute_mean: commandMetrics.ExecutionTimeMean, latencyTotal_mean: commandMetrics.TotalTimeMean, reportingHosts: 1, // this will get summed across all instances in a cluster threadPool: commandMetrics.ThreadPoolKey.Name); } foreach (var threadPoolMetrics in dashboardData.ThreadPoolMetrics) { HystrixMetricsEventSource.EventLogger.ThreadPoolMetrics( threadpoolKey: threadPoolMetrics.ThreadPoolKey.Name, cumulativeCountThreadsExecuted: threadPoolMetrics.CumulativeCountThreadsExecuted, currentActiveCount: threadPoolMetrics.CurrentActiveCount, currentCompletedTaskCount: threadPoolMetrics.CurrentCompletedTaskCount, currentCorePoolSize: threadPoolMetrics.CurrentCorePoolSize, currentLargestPoolSize: threadPoolMetrics.CurrentLargestPoolSize, currentMaximumPoolSize: threadPoolMetrics.CurrentMaximumPoolSize, currentPoolSize: threadPoolMetrics.CurrentPoolSize, currentQueueSize: threadPoolMetrics.CurrentQueueSize, currentTaskCount: threadPoolMetrics.CurrentTaskCount, reportingHosts: 1); // this will get summed across all instances in a cluster } foreach (var collapserMetrics in dashboardData.CollapserMetrics) { HystrixMetricsEventSource.EventLogger.CollapserMetrics( collapserKey: collapserMetrics.CollapserKey.Name, rollingCountRequestsBatched: collapserMetrics.GetRollingCount(CollapserEventType.ADDED_TO_BATCH), rollingCountBatches: collapserMetrics.GetRollingCount(CollapserEventType.BATCH_EXECUTED), rollingCountResponsesFromCache: collapserMetrics.GetRollingCount(CollapserEventType.RESPONSE_FROM_CACHE), batchSize_mean: collapserMetrics.BatchSizeMean, reportingHosts: 1); // this will get summed across all instances in a cluster } } catch (Exception ex) { Console.WriteLine(ex); } } }
public static void Reset(TimeSpan timeout) { HystrixThreadPoolFactory.Shutdown(timeout); HystrixCommandMetrics.Reset(); //HystrixCollapser.Reset(); HystrixCircuitBreakerFactory.Reset(); HystrixPlugins.Reset(); //HystrixPropertiesFactory.reset(); //currentCommand.set(new LinkedList<HystrixCommandKey>()); }
public HystrixTestBase() { HystrixCommandMetrics.Reset(); HystrixThreadPoolMetrics.Reset(); HystrixCollapserMetrics.Reset(); // clear collapsers RequestCollapserFactory.Reset(); // clear circuit breakers HystrixCircuitBreakerFactory.Reset(); HystrixPlugins.Reset(); HystrixOptionsFactory.Reset(); }
public HystrixTestBase() { this.context = HystrixRequestContext.InitializeContext(); HystrixCommandMetrics.Reset(); HystrixThreadPoolMetrics.Reset(); HystrixCollapserMetrics.Reset(); // clear collapsers RequestCollapserFactory.Reset(); // clear circuit breakers HystrixCircuitBreakerFactory.Reset(); HystrixPlugins.Reset(); HystrixOptionsFactory.Reset(); }
public HystrixCommand(string servicePath, string opName, string serviceName, string fullServiceName, string metricPrefix /*"soa.service"*/, HystrixCommandPropertiesSetter commandPropertiesDefaults) { // op name & service name initialization if (opName == null) { throw new ArgumentNullException("opName"); } if (serviceName == null) { throw new ArgumentNullException("serviceName"); } if (metricPrefix == null) { throw new ArgumentNullException("metricPrefix"); } if (fullServiceName == null) { throw new ArgumentNullException("fullServiceName"); } CommandKey = servicePath + "." + opName; // Properties initialization commandPropertiesDefaults 只设置了 电容器的开关和方法的执行timeout 2个参数 this.properties = HystrixPropertiesFactory.GetCommandProperties(CommandKey, commandPropertiesDefaults); // Metrics initializtion this.metrics = HystrixCommandMetrics.GetInstance(CommandKey, opName, serviceName, fullServiceName, metricPrefix, this.properties); // CircuitBreaker initializtion if (this.properties.CircuitBreakerEnabled.Get()) { this.circuitBreaker = HystrixCircuitBreakerFactory.GetInstance(CommandKey, this.properties, this.metrics); } else { this.circuitBreaker = new NoOpCircuitBreaker(); } }
private static void WriteCommandMetrics(JsonTextWriter writer, HystrixCommandMetrics commandMetrics) { IHystrixCommandKey key = commandMetrics.CommandKey; IHystrixCircuitBreaker circuitBreaker = HystrixCircuitBreakerFactory.GetInstance(key); writer.WriteStartObject(); writer.WriteStringField("type", "HystrixCommand"); writer.WriteStringField("name", key.Name); writer.WriteStringField("group", commandMetrics.CommandGroup.Name); writer.WriteLongField("currentTime", Time.CurrentTimeMillisJava); // circuit breaker if (circuitBreaker == null) { // circuit breaker is disabled and thus never open writer.WriteBooleanField("isCircuitBreakerOpen", false); } else { writer.WriteBooleanField("isCircuitBreakerOpen", circuitBreaker.IsOpen); } HealthCounts healthCounts = commandMetrics.Healthcounts; writer.WriteIntegerField("errorPercentage", healthCounts.ErrorPercentage); writer.WriteLongField("errorCount", healthCounts.ErrorCount); writer.WriteLongField("requestCount", healthCounts.TotalRequests); // rolling counters writer.WriteLongField("rollingCountBadRequests", commandMetrics.GetRollingCount(HystrixEventType.BAD_REQUEST)); writer.WriteLongField("rollingCountCollapsedRequests", commandMetrics.GetRollingCount(HystrixEventType.COLLAPSED)); writer.WriteLongField("rollingCountEmit", commandMetrics.GetRollingCount(HystrixEventType.EMIT)); writer.WriteLongField("rollingCountExceptionsThrown", commandMetrics.GetRollingCount(HystrixEventType.EXCEPTION_THROWN)); writer.WriteLongField("rollingCountFailure", commandMetrics.GetRollingCount(HystrixEventType.FAILURE)); writer.WriteLongField("rollingCountFallbackEmit", commandMetrics.GetRollingCount(HystrixEventType.FALLBACK_EMIT)); writer.WriteLongField("rollingCountFallbackFailure", commandMetrics.GetRollingCount(HystrixEventType.FALLBACK_FAILURE)); writer.WriteLongField("rollingCountFallbackMissing", commandMetrics.GetRollingCount(HystrixEventType.FALLBACK_MISSING)); writer.WriteLongField("rollingCountFallbackRejection", commandMetrics.GetRollingCount(HystrixEventType.FALLBACK_REJECTION)); writer.WriteLongField("rollingCountFallbackSuccess", commandMetrics.GetRollingCount(HystrixEventType.FALLBACK_SUCCESS)); writer.WriteLongField("rollingCountResponsesFromCache", commandMetrics.GetRollingCount(HystrixEventType.RESPONSE_FROM_CACHE)); writer.WriteLongField("rollingCountSemaphoreRejected", commandMetrics.GetRollingCount(HystrixEventType.SEMAPHORE_REJECTED)); writer.WriteLongField("rollingCountShortCircuited", commandMetrics.GetRollingCount(HystrixEventType.SHORT_CIRCUITED)); writer.WriteLongField("rollingCountSuccess", commandMetrics.GetRollingCount(HystrixEventType.SUCCESS)); writer.WriteLongField("rollingCountThreadPoolRejected", commandMetrics.GetRollingCount(HystrixEventType.THREAD_POOL_REJECTED)); writer.WriteLongField("rollingCountTimeout", commandMetrics.GetRollingCount(HystrixEventType.TIMEOUT)); writer.WriteIntegerField("currentConcurrentExecutionCount", commandMetrics.CurrentConcurrentExecutionCount); writer.WriteLongField("rollingMaxConcurrentExecutionCount", commandMetrics.RollingMaxConcurrentExecutions); // latency percentiles writer.WriteIntegerField("latencyExecute_mean", commandMetrics.ExecutionTimeMean); writer.WriteObjectFieldStart("latencyExecute"); writer.WriteIntegerField("0", commandMetrics.GetExecutionTimePercentile(0)); writer.WriteIntegerField("25", commandMetrics.GetExecutionTimePercentile(25)); writer.WriteIntegerField("50", commandMetrics.GetExecutionTimePercentile(50)); writer.WriteIntegerField("75", commandMetrics.GetExecutionTimePercentile(75)); writer.WriteIntegerField("90", commandMetrics.GetExecutionTimePercentile(90)); writer.WriteIntegerField("95", commandMetrics.GetExecutionTimePercentile(95)); writer.WriteIntegerField("99", commandMetrics.GetExecutionTimePercentile(99)); writer.WriteIntegerField("99.5", commandMetrics.GetExecutionTimePercentile(99.5)); writer.WriteIntegerField("100", commandMetrics.GetExecutionTimePercentile(100)); writer.WriteEndObject(); writer.WriteIntegerField("latencyTotal_mean", commandMetrics.TotalTimeMean); writer.WriteObjectFieldStart("latencyTotal"); writer.WriteIntegerField("0", commandMetrics.GetTotalTimePercentile(0)); writer.WriteIntegerField("25", commandMetrics.GetTotalTimePercentile(25)); writer.WriteIntegerField("50", commandMetrics.GetTotalTimePercentile(50)); writer.WriteIntegerField("75", commandMetrics.GetTotalTimePercentile(75)); writer.WriteIntegerField("90", commandMetrics.GetTotalTimePercentile(90)); writer.WriteIntegerField("95", commandMetrics.GetTotalTimePercentile(95)); writer.WriteIntegerField("99", commandMetrics.GetTotalTimePercentile(99)); writer.WriteIntegerField("99.5", commandMetrics.GetTotalTimePercentile(99.5)); writer.WriteIntegerField("100", commandMetrics.GetTotalTimePercentile(100)); writer.WriteEndObject(); // property values for reporting what is actually seen by the command rather than what was set somewhere IHystrixCommandOptions commandProperties = commandMetrics.Properties; writer.WriteIntegerField("propertyValue_circuitBreakerRequestVolumeThreshold", commandProperties.CircuitBreakerRequestVolumeThreshold); writer.WriteIntegerField("propertyValue_circuitBreakerSleepWindowInMilliseconds", commandProperties.CircuitBreakerSleepWindowInMilliseconds); writer.WriteIntegerField("propertyValue_circuitBreakerErrorThresholdPercentage", commandProperties.CircuitBreakerErrorThresholdPercentage); writer.WriteBooleanField("propertyValue_circuitBreakerForceOpen", commandProperties.CircuitBreakerForceOpen); writer.WriteBooleanField("propertyValue_circuitBreakerForceClosed", commandProperties.CircuitBreakerForceClosed); writer.WriteBooleanField("propertyValue_circuitBreakerEnabled", commandProperties.CircuitBreakerEnabled); writer.WriteStringField("propertyValue_executionIsolationStrategy", commandProperties.ExecutionIsolationStrategy.ToString()); writer.WriteIntegerField("propertyValue_executionIsolationThreadTimeoutInMilliseconds", commandProperties.ExecutionTimeoutInMilliseconds); writer.WriteIntegerField("propertyValue_executionTimeoutInMilliseconds", commandProperties.ExecutionTimeoutInMilliseconds); writer.WriteBooleanField("propertyValue_executionIsolationThreadInterruptOnTimeout", false); writer.WriteStringField("propertyValue_executionIsolationThreadPoolKeyOverride", commandProperties.ExecutionIsolationThreadPoolKeyOverride); writer.WriteIntegerField("propertyValue_executionIsolationSemaphoreMaxConcurrentRequests", commandProperties.ExecutionIsolationSemaphoreMaxConcurrentRequests); writer.WriteIntegerField("propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests", commandProperties.FallbackIsolationSemaphoreMaxConcurrentRequests); writer.WriteIntegerField("propertyValue_metricsRollingStatisticalWindowInMilliseconds", commandProperties.MetricsRollingStatisticalWindowInMilliseconds); writer.WriteBooleanField("propertyValue_requestCacheEnabled", commandProperties.RequestCacheEnabled); writer.WriteBooleanField("propertyValue_requestLogEnabled", commandProperties.RequestLogEnabled); writer.WriteIntegerField("reportingHosts", 1); // this will get summed across all instances in a cluster writer.WriteStringField("threadPool", commandMetrics.ThreadPoolKey.Name); writer.WriteEndObject(); }
/// <summary> /// Produces JSON formatted metrics data from an instance of <see cref="HystrixCommandMetrics"/>. /// </summary> /// <param name="commandMetrics">The metrics of a command.</param> /// <returns>JSON formatted metrics data.</returns> private static string CreateCommandSampleData(HystrixCommandMetrics commandMetrics) { IHystrixCircuitBreaker circuitBreaker = HystrixCircuitBreakerFactory.GetInstance(commandMetrics.CommandKey); HealthCounts healthCounts = commandMetrics.GetHealthCounts(); IHystrixCommandProperties commandProperties = commandMetrics.Properties; JObject data = new JObject( new JProperty("type", "HystrixCommand"), new JProperty("name", commandMetrics.CommandKey.Name), new JProperty("group", commandMetrics.CommandGroup.Name), new JProperty("currentTime", GetCurrentTimeForJavascript()), circuitBreaker == null ? new JProperty("isCircuitBreakerOpen", false) : new JProperty("isCircuitBreakerOpen", circuitBreaker.IsOpen()), new JProperty("errorPercentage", healthCounts.ErrorPercentage), // health counts new JProperty("errorCount", healthCounts.ErrorCount), new JProperty("requestCount", healthCounts.TotalRequests), new JProperty("rollingCountCollapsedRequests", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.Collapsed)), // rolling counters new JProperty("rollingCountExceptionsThrown", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.ExceptionThrown)), new JProperty("rollingCountFailure", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.Failure)), new JProperty("rollingCountFallbackFailure", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.FallbackFailure)), new JProperty("rollingCountFallbackRejection", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.FallbackRejection)), new JProperty("rollingCountFallbackSuccess", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.FallbackSuccess)), new JProperty("rollingCountResponsesFromCache", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.ResponseFromCache)), new JProperty("rollingCountSemaphoreRejected", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.SemaphoreRejected)), new JProperty("rollingCountShortCircuited", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.ShortCircuited)), new JProperty("rollingCountSuccess", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.Success)), new JProperty("rollingCountThreadPoolRejected", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.ThreadPoolRejected)), new JProperty("rollingCountTimeout", commandMetrics.GetRollingCount(HystrixRollingNumberEvent.Timeout)), new JProperty("currentConcurrentExecutionCount", commandMetrics.CurrentConcurrentExecutionCount), new JProperty("latencyExecute_mean", commandMetrics.GetExecutionTimeMean()), // latency percentiles new JProperty( "latencyExecute", new JObject( new JProperty("0", commandMetrics.GetExecutionTimePercentile(0)), new JProperty("25", commandMetrics.GetExecutionTimePercentile(25)), new JProperty("50", commandMetrics.GetExecutionTimePercentile(50)), new JProperty("75", commandMetrics.GetExecutionTimePercentile(75)), new JProperty("90", commandMetrics.GetExecutionTimePercentile(90)), new JProperty("95", commandMetrics.GetExecutionTimePercentile(95)), new JProperty("99", commandMetrics.GetExecutionTimePercentile(99)), new JProperty("99.5", commandMetrics.GetExecutionTimePercentile(99.5)), new JProperty("100", commandMetrics.GetExecutionTimePercentile(100)))), new JProperty("latencyTotal_mean", commandMetrics.GetTotalTimeMean()), new JProperty( "latencyTotal", new JObject( new JProperty("0", commandMetrics.GetTotalTimePercentile(0)), new JProperty("25", commandMetrics.GetTotalTimePercentile(25)), new JProperty("50", commandMetrics.GetTotalTimePercentile(50)), new JProperty("75", commandMetrics.GetTotalTimePercentile(75)), new JProperty("90", commandMetrics.GetTotalTimePercentile(90)), new JProperty("95", commandMetrics.GetTotalTimePercentile(95)), new JProperty("99", commandMetrics.GetTotalTimePercentile(99)), new JProperty("99.5", commandMetrics.GetTotalTimePercentile(99.5)), new JProperty("100", commandMetrics.GetTotalTimePercentile(100)))), new JProperty("propertyValue_circuitBreakerRequestVolumeThreshold", commandProperties.CircuitBreakerRequestVolumeThreshold.Get()), // property values for reporting what is actually seen by the command rather than what was set somewhere new JProperty("propertyValue_circuitBreakerSleepWindowInMilliseconds", (long)commandProperties.CircuitBreakerSleepWindow.Get().TotalMilliseconds), new JProperty("propertyValue_circuitBreakerErrorThresholdPercentage", commandProperties.CircuitBreakerErrorThresholdPercentage.Get()), new JProperty("propertyValue_circuitBreakerForceOpen", commandProperties.CircuitBreakerForceOpen.Get()), new JProperty("propertyValue_circuitBreakerForceClosed", commandProperties.CircuitBreakerForceClosed.Get()), new JProperty("propertyValue_circuitBreakerEnabled", commandProperties.CircuitBreakerEnabled.Get()), new JProperty("propertyValue_executionIsolationStrategy", commandProperties.ExecutionIsolationStrategy.Get()), new JProperty("propertyValue_executionIsolationThreadTimeoutInMilliseconds", (long)commandProperties.ExecutionIsolationThreadTimeout.Get().TotalMilliseconds), new JProperty("propertyValue_executionIsolationThreadInterruptOnTimeout", commandProperties.ExecutionIsolationThreadInterruptOnTimeout.Get()), new JProperty("propertyValue_executionIsolationThreadPoolKeyOverride", commandProperties.ExecutionIsolationThreadPoolKeyOverride.Get()), new JProperty("propertyValue_executionIsolationSemaphoreMaxConcurrentRequests", commandProperties.ExecutionIsolationSemaphoreMaxConcurrentRequests.Get()), new JProperty("propertyValue_fallbackIsolationSemaphoreMaxConcurrentRequests", commandProperties.FallbackIsolationSemaphoreMaxConcurrentRequests.Get()), new JProperty("propertyValue_metricsRollingStatisticalWindowInMilliseconds", commandProperties.MetricsRollingStatisticalWindowInMilliseconds.Get()), new JProperty("propertyValue_requestCacheEnabled", commandProperties.RequestCacheEnabled.Get()), new JProperty("propertyValue_requestLogEnabled", commandProperties.RequestLogEnabled.Get()), new JProperty("reportingHosts", 1)); return(data.ToString(Formatting.None)); }