-
Notifications
You must be signed in to change notification settings - Fork 491
/
mysql_operation.go
40 lines (33 loc) · 1.36 KB
/
mysql_operation.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
package controllers
import (
"github.com/prometheus/client_golang/prometheus"
)
var (
defaultMySQLOperationTimeWidth = []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000}
)
// MySQLOperationController define the controller of mysql operation metric.
type MySQLOperationController struct {
histogramVec *prometheus.HistogramVec
}
// NewMySQLOperationController get a new MySQLOperationController.
func NewMySQLOperationController() *MySQLOperationController {
return &MySQLOperationController{
histogramVec: prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "mysql_operation_time_histogram",
Help: "Bucketed histogram of mysql operation time(millisecond).",
Buckets: defaultMySQLOperationTimeWidth,
}, []string{"operation"}),
}
}
// Describe provide metrics described by prometheus.
func (moc *MySQLOperationController) Describe(ch chan<- *prometheus.Desc) {
moc.histogramVec.Describe(ch)
}
// Collect provide metrics collected by prometheus.
func (moc *MySQLOperationController) Collect(ch chan<- prometheus.Metric) {
moc.histogramVec.Collect(ch)
}
// Observe record the data that about a single mysql operation. With duration time and operation name.
func (moc *MySQLOperationController) Observe(operation string, operationTimeMillisecond float64) {
go moc.histogramVec.WithLabelValues(operation).Observe(operationTimeMillisecond)
}