-
Notifications
You must be signed in to change notification settings - Fork 2k
/
mysql_throttle_metric.go
122 lines (102 loc) · 3.84 KB
/
mysql_throttle_metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2017 GitHub Inc.
Licensed under MIT License. See https://github.com/github/freno/blob/master/LICENSE
*/
package mysql
import (
"fmt"
"strings"
"time"
"github.com/patrickmn/go-cache"
"vitess.io/vitess/go/stats"
)
// MetricsQueryType indicates the type of metrics query on MySQL backend. See following.
type MetricsQueryType int
const (
// MetricsQueryTypeDefault indictes the default, internal implementation. Specifically, our throttler runs a replication lag query
MetricsQueryTypeDefault MetricsQueryType = iota
// MetricsQueryTypeShowGlobal indicatesa SHOW GLOBAL (STATUS|VARIABLES) query
MetricsQueryTypeShowGlobal
// MetricsQueryTypeSelect indicates a custom SELECT query
MetricsQueryTypeSelect
// MetricsQueryTypeUnknown is an unknown query type, which we cannot run. This is an error
MetricsQueryTypeUnknown
)
var mysqlMetricCache = cache.New(cache.NoExpiration, 10*time.Second)
func getMySQLMetricCacheKey(probe *Probe) string {
return fmt.Sprintf("%s:%s", probe.Key, probe.MetricQuery)
}
func cacheMySQLThrottleMetric(probe *Probe, mySQLThrottleMetric *MySQLThrottleMetric) *MySQLThrottleMetric {
if mySQLThrottleMetric.Err != nil {
return mySQLThrottleMetric
}
if probe.CacheMillis > 0 {
mysqlMetricCache.Set(getMySQLMetricCacheKey(probe), mySQLThrottleMetric, time.Duration(probe.CacheMillis)*time.Millisecond)
}
return mySQLThrottleMetric
}
func getCachedMySQLThrottleMetric(probe *Probe) *MySQLThrottleMetric {
if probe.CacheMillis == 0 {
return nil
}
if metric, found := mysqlMetricCache.Get(getMySQLMetricCacheKey(probe)); found {
mySQLThrottleMetric, _ := metric.(*MySQLThrottleMetric)
return mySQLThrottleMetric
}
return nil
}
// GetMetricsQueryType analyzes the type of a metrics query
func GetMetricsQueryType(query string) MetricsQueryType {
if query == "" {
return MetricsQueryTypeDefault
}
if strings.HasPrefix(strings.ToLower(query), "select") {
return MetricsQueryTypeSelect
}
if strings.HasPrefix(strings.ToLower(query), "show global") {
return MetricsQueryTypeShowGlobal
}
return MetricsQueryTypeUnknown
}
// MySQLThrottleMetric has the probed metric for a mysql instance
type MySQLThrottleMetric struct { // nolint:revive
ClusterName string
Key InstanceKey
Value float64
Err error
}
// NewMySQLThrottleMetric creates a new MySQLThrottleMetric
func NewMySQLThrottleMetric() *MySQLThrottleMetric {
return &MySQLThrottleMetric{Value: 0}
}
// GetClusterInstanceKey returns the ClusterInstanceKey part of the metric
func (metric *MySQLThrottleMetric) GetClusterInstanceKey() ClusterInstanceKey {
return GetClusterInstanceKey(metric.ClusterName, &metric.Key)
}
// Get implements MetricResult
func (metric *MySQLThrottleMetric) Get() (float64, error) {
return metric.Value, metric.Err
}
// ReadThrottleMetric returns a metric for the given probe. Either by explicit query
// or via SHOW SLAVE STATUS
func ReadThrottleMetric(probe *Probe, clusterName string, overrideGetMetricFunc func() *MySQLThrottleMetric) (mySQLThrottleMetric *MySQLThrottleMetric) {
if mySQLThrottleMetric := getCachedMySQLThrottleMetric(probe); mySQLThrottleMetric != nil {
return mySQLThrottleMetric
// On cached results we avoid taking latency metrics
}
started := time.Now()
mySQLThrottleMetric = NewMySQLThrottleMetric()
mySQLThrottleMetric.ClusterName = clusterName
mySQLThrottleMetric.Key = probe.Key
defer func(metric *MySQLThrottleMetric, started time.Time) {
go func() {
stats.GetOrNewGauge("ThrottlerProbesLatency", "probes latency").Set(time.Since(started).Nanoseconds())
stats.GetOrNewCounter("ThrottlerProbesTotal", "total probes").Add(1)
if metric.Err != nil {
stats.GetOrNewCounter("ThrottlerProbesError", "total probes errors").Add(1)
}
}()
}(mySQLThrottleMetric, started)
mySQLThrottleMetric = overrideGetMetricFunc()
return cacheMySQLThrottleMetric(probe, mySQLThrottleMetric)
}