func statsOK() *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } return m }
// New creates a new CircuitBreaker middleware func New(next http.Handler, expression string, options ...CircuitBreakerOption) (*CircuitBreaker, error) { cb := &CircuitBreaker{ m: &sync.RWMutex{}, next: next, // Default values. Might be overwritten by options below. clock: &timetools.RealTime{}, checkPeriod: defaultCheckPeriod, fallbackDuration: defaultFallbackDuration, recoveryDuration: defaultRecoveryDuration, fallback: defaultFallback, log: utils.NullLogger, } for _, s := range options { if err := s(cb); err != nil { return nil, err } } condition, err := parseExpression(expression) if err != nil { return nil, err } cb.condition = condition mt, err := memmetrics.NewRTMetrics() if err != nil { return nil, err } cb.metrics = mt return cb, nil }
func statsLatencyAtQuantile(quantile float64, value time.Duration) *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } m.Record(http.StatusOK, value) return m }
func statsResponseCodes(codes ...statusCode) *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } for _, c := range codes { for i := int64(0); i < c.Count; i++ { m.Record(c.Code, 0) } } return m }
func statsNetErrors(threshold float64) *memmetrics.RTMetrics { m, err := memmetrics.NewRTMetrics() if err != nil { panic(err) } for i := 0; i < 100; i++ { if i < int(threshold*100) { m.Record(http.StatusGatewayTimeout, 0) } else { m.Record(http.StatusOK, 0) } } return m }