internal StandardCommandMetrics(GroupKey key, IFailurePercentageCircuitBreakerConfig config, IClock clock, IMjolnirLogFactory logFactory) { _config = config ?? throw new ArgumentNullException(nameof(config)); _clock = clock ?? throw new ArgumentNullException(nameof(clock)); _key = key; _resettingNumbersBucket = new ResettingNumbersBucket(_key, _clock, _config, logFactory); }
public CircuitBreakerFactory(IMetricEvents metricEvents, IFailurePercentageCircuitBreakerConfig breakerConfig, IMjolnirLogFactory logFactory) { _metricEvents = metricEvents ?? throw new ArgumentNullException(nameof(metricEvents)); _breakerConfig = breakerConfig ?? throw new ArgumentNullException(nameof(breakerConfig)); _logFactory = logFactory ?? throw new ArgumentNullException(nameof(logFactory)); _log = logFactory.CreateLog <FailurePercentageCircuitBreaker>(); if (_log == null) { throw new InvalidOperationException($"{nameof(IMjolnirLogFactory)} implementation returned null from {nameof(IMjolnirLogFactory.CreateLog)} for type {typeof(CircuitBreakerFactory)}, please make sure the implementation returns a non-null log for all calls to {nameof(IMjolnirLogFactory.CreateLog)}"); } _timer = new GaugeTimer(state => { try { var keys = _circuitBreakers.Keys; foreach (var key in keys) { if (_circuitBreakers.TryGetValue(key, out Lazy <FailurePercentageCircuitBreaker> lazy) && lazy.IsValueCreated) { var breaker = lazy.Value; _metricEvents.BreakerGauge( breaker.Name, _breakerConfig.GetMinimumOperations(key), _breakerConfig.GetWindowMillis(key), _breakerConfig.GetThresholdPercentage(key), _breakerConfig.GetTrippedDurationMillis(key), _breakerConfig.GetForceTripped(key), _breakerConfig.GetForceFixed(key), breaker.IsTripped(), breaker.Metrics.SuccessCount, breaker.Metrics.FailureCount); } } } catch (Exception e) { _log.Error($"Error sending {nameof(IMetricEvents.BreakerGauge)} metric event", e); } }); }
internal ResettingNumbersBucket(GroupKey key, IClock clock, IFailurePercentageCircuitBreakerConfig config, IMjolnirLogFactory logFactory) { _key = key; _clock = clock ?? throw new ArgumentNullException(nameof(clock)); _config = config ?? throw new ArgumentNullException(nameof(config)); if (logFactory == null) { throw new ArgumentNullException(nameof(logFactory)); } _log = logFactory.CreateLog <ResettingNumbersBucket>(); if (_log == null) { throw new InvalidOperationException($"{nameof(IMjolnirLogFactory)} implementation returned null from {nameof(IMjolnirLogFactory.CreateLog)} for type {typeof(ResettingNumbersBucket)}, please make sure the implementation returns a non-null log for all calls to {nameof(IMjolnirLogFactory.CreateLog)}"); } _counters = CreateCounters(); _lastResetAtTime = clock.GetMillisecondTimestamp(); }
internal FailurePercentageCircuitBreaker(GroupKey key, IClock clock, ICommandMetrics metrics, IMetricEvents metricEvents, IFailurePercentageCircuitBreakerConfig config, IMjolnirLogFactory logFactory) { _metrics = metrics ?? throw new ArgumentNullException(nameof(metrics)); _clock = clock ?? throw new ArgumentNullException(nameof(config)); _config = config ?? throw new ArgumentNullException(nameof(config)); _metricEvents = metricEvents ?? throw new ArgumentNullException(nameof(metricEvents)); if (logFactory == null) { throw new ArgumentNullException(nameof(logFactory)); } _key = key; _log = logFactory.CreateLog <FailurePercentageCircuitBreaker>(); if (_log == null) { throw new InvalidOperationException($"{nameof(IMjolnirLogFactory)} implementation returned null from {nameof(IMjolnirLogFactory.CreateLog)} for type {typeof(FailurePercentageCircuitBreaker)}, please make sure the implementation returns a non-null log for all calls to {nameof(IMjolnirLogFactory.CreateLog)}"); } _state = State.Fixed; // Start off assuming everything's fixed. _lastTrippedTimestamp = 0; // 0 is fine since it'll be far less than the first compared value. }
internal StandardCommandMetrics(GroupKey key, IFailurePercentageCircuitBreakerConfig config, IMjolnirLogFactory logFactory) : this(key, config, new UtcSystemClock(), logFactory) { }
internal FailurePercentageCircuitBreaker(GroupKey key, ICommandMetrics metrics, IMetricEvents metricEvents, IFailurePercentageCircuitBreakerConfig config, IMjolnirLogFactory logFactory) : this(key, new UtcSystemClock(), metrics, metricEvents, config, logFactory) { }
internal ResettingNumbersBucket(GroupKey key, IFailurePercentageCircuitBreakerConfig config, IMjolnirLogFactory logFactory) : this(key, new UtcSystemClock(), config, logFactory) { }