-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathmonitor.go
More file actions
164 lines (136 loc) · 3.47 KB
/
monitor.go
File metadata and controls
164 lines (136 loc) · 3.47 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package monitor
import (
"net"
"net/http"
"config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/xelabs/go-mysqlstack/xlog"
)
var (
webMonitorURL = "/metrics"
clientConnectionNum = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "connection_number_client",
Help: "client connection Number",
},
[]string{"user"},
)
backendConnectionNum = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "connection_number_backend",
Help: "backend connection Number",
},
[]string{"address"},
)
queryTotalCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "query_total",
Help: "Counter of queries.",
},
[]string{"command", "result"},
)
backendNum = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "backend_number",
Help: "backend Number",
},
[]string{"type"},
)
diskUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "disk_usage",
Help: "disk usage",
},
[]string{"description"},
)
slowQueryTotalCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "slow_query_total",
Help: "Counter of slow queries.",
},
[]string{"command", "result"},
)
peerNum = prometheus.NewGauge(
prometheus.GaugeOpts{
Name: "peer_number",
Help: "radon peer Number",
})
)
func init() {
prometheus.MustRegister(clientConnectionNum)
prometheus.MustRegister(backendConnectionNum)
prometheus.MustRegister(queryTotalCounter)
prometheus.MustRegister(backendNum)
prometheus.MustRegister(diskUsage)
prometheus.MustRegister(slowQueryTotalCounter)
prometheus.MustRegister(peerNum)
}
// Start monitor
func Start(log *xlog.Log, conf *config.Config) {
webMonitorIP, webMonitorPort, err := net.SplitHostPort(conf.Monitor.MonitorAddress)
if err != nil {
panic(err)
}
log.Info("[prometheus metrics]:\thttp://%s:%s%s\n",
webMonitorIP, webMonitorPort, webMonitorURL)
log.Info("[pprof web]:\t\thttp://%s:%s/debug/pprof/\n",
webMonitorIP, webMonitorPort)
http.Handle(webMonitorURL, promhttp.Handler())
go http.ListenAndServe(webMonitorIP+":"+webMonitorPort, nil)
}
// ClientConnectionInc add 1
func ClientConnectionInc(user string) {
clientConnectionNum.WithLabelValues(user).Inc()
}
// ClientConnectionDec dec 1
func ClientConnectionDec(user string) {
clientConnectionNum.WithLabelValues(user).Dec()
}
// BackendConnectionInc add 1
func BackendConnectionInc(address string) {
backendConnectionNum.WithLabelValues(address).Inc()
}
// BackendConnectionDec dec 1
func BackendConnectionDec(address string) {
backendConnectionNum.WithLabelValues(address).Dec()
}
// QueryTotalCounterInc add 1
func QueryTotalCounterInc(command string, result string) {
queryTotalCounter.WithLabelValues(command, result).Inc()
}
// BackendInc add 1
func BackendInc(btype string) {
backendNum.WithLabelValues(btype).Inc()
}
// BackendDec dec 1
func BackendDec(btype string) {
backendNum.WithLabelValues(btype).Dec()
}
// DiskUsageSet set usage of disk
func DiskUsageSet(v float64) {
diskUsage.WithLabelValues("percent").Set(v)
}
// SlowQueryTotalCounterInc add 1
func SlowQueryTotalCounterInc(command string, result string) {
slowQueryTotalCounter.WithLabelValues(command, result).Inc()
}
//PeerNumInc add 1
func PeerNumInc() {
peerNum.Inc()
}
//PeerNumDec dec 1
func PeerNumDec() {
peerNum.Dec()
}
//PeerNumSet set value
func PeerNumSet(v float64) {
peerNum.Set(v)
}