-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
metrics.go
65 lines (58 loc) · 1.68 KB
/
metrics.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
package hbase
import (
"context"
"io"
"strings"
"time"
"github.com/tsuna/gohbase"
"github.com/tsuna/gohbase/hrpc"
"github.com/go-kratos/kratos/pkg/stat/metric"
)
const namespace = "hbase_client"
var (
_metricReqDur = metric.NewHistogramVec(&metric.HistogramVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "duration_ms",
Help: "hbase client requests duration(ms).",
Labels: []string{"name", "addr", "command"},
Buckets: []float64{5, 10, 25, 50, 100, 250, 500, 1000, 2500},
})
_metricReqErr = metric.NewCounterVec(&metric.CounterVecOpts{
Namespace: namespace,
Subsystem: "requests",
Name: "error_total",
Help: "mysql client requests error count.",
Labels: []string{"name", "addr", "command", "error"},
})
)
func codeFromErr(err error) string {
code := "unknown_error"
switch err {
case gohbase.ErrClientClosed:
code = "client_closed"
case gohbase.ErrCannotFindRegion:
code = "connot_find_region"
case gohbase.TableNotFound:
code = "table_not_found"
//case gohbase.ErrRegionUnavailable:
// code = "region_unavailable"
}
return code
}
// MetricsHook if stats is nil use stat.DB as default.
func MetricsHook(config *Config) HookFunc {
return func(ctx context.Context, call hrpc.Call, customName string) func(err error) {
now := time.Now()
if customName == "" {
customName = call.Name()
}
return func(err error) {
durationMs := int64(time.Since(now) / time.Millisecond)
_metricReqDur.Observe(durationMs, strings.Join(config.Zookeeper.Addrs, ","), "", customName)
if err != nil && err != io.EOF {
_metricReqErr.Inc(strings.Join(config.Zookeeper.Addrs, ","), "", customName, codeFromErr(err))
}
}
}
}