vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/throttle/base/throttle_metric.go (about) 1 /* 2 Copyright 2017 GitHub Inc. 3 4 Licensed under MIT License. See https://github.com/github/freno/blob/master/LICENSE 5 */ 6 7 package base 8 9 import ( 10 "errors" 11 "strings" 12 ) 13 14 // MetricResult is what we expect our probes to return. This can be a numeric result, or 15 // a special type of result indicating more meta-information 16 type MetricResult interface { 17 Get() (float64, error) 18 } 19 20 // MetricResultFunc is a function that returns a metric result 21 type MetricResultFunc func() (metricResult MetricResult, threshold float64) 22 23 // ErrThresholdExceeded is the common error one may get checking on metric result 24 var ErrThresholdExceeded = errors.New("Threshold exceeded") 25 var errNoResultYet = errors.New("Metric not collected yet") 26 27 // ErrNoSuchMetric is for when a user requests a metric by an unknown metric name 28 var ErrNoSuchMetric = errors.New("No such metric") 29 30 // ErrInvalidCheckType is an internal error indicating an unknown check type 31 var ErrInvalidCheckType = errors.New("Unknown throttler check type") 32 33 // IsDialTCPError sees if th egiven error indicates a TCP issue 34 func IsDialTCPError(e error) bool { 35 if e == nil { 36 return false 37 } 38 return strings.HasPrefix(e.Error(), "dial tcp") 39 } 40 41 type noHostsMetricResult struct{} 42 43 // Get implements MetricResult 44 func (metricResult *noHostsMetricResult) Get() (float64, error) { 45 return 0, nil 46 } 47 48 // NoHostsMetricResult is a result indicating "no hosts" 49 var NoHostsMetricResult = &noHostsMetricResult{} 50 51 type noMetricResultYet struct{} 52 53 // Get implements MetricResult 54 func (metricResult *noMetricResultYet) Get() (float64, error) { 55 return 0, errNoResultYet 56 } 57 58 // NoMetricResultYet is a result indicating "no data" 59 var NoMetricResultYet = &noMetricResultYet{} 60 61 type noSuchMetric struct{} 62 63 // Get implements MetricResult 64 func (metricResult *noSuchMetric) Get() (float64, error) { 65 return 0, ErrNoSuchMetric 66 } 67 68 // NoSuchMetric is a metric results for an unknown metric name 69 var NoSuchMetric = &noSuchMetric{} 70 71 // simpleMetricResult is a result with float value 72 type simpleMetricResult struct { 73 Value float64 74 } 75 76 // NewSimpleMetricResult creates a simpleMetricResult 77 func NewSimpleMetricResult(value float64) MetricResult { 78 return &simpleMetricResult{Value: value} 79 } 80 81 // Get implements MetricResult 82 func (metricResult *simpleMetricResult) Get() (float64, error) { 83 return metricResult.Value, nil 84 }