-
Notifications
You must be signed in to change notification settings - Fork 2
/
metrics.go
75 lines (62 loc) · 1.64 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
66
67
68
69
70
71
72
73
74
75
package tracker
import (
"fmt"
"github.com/Zondax/zindexer/components/connections/zmetrics"
"go.uber.org/zap"
"regexp"
"strings"
)
var (
metricsMap = make(map[string]*zmetrics.Gauge)
matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])")
)
func setTotalMissingHeightsMetric(id string, count int) {
ind := getOrCreateIndicator(id)
if ind == nil {
zap.S().Warnf("Could not update metrics dor id '%s', nil indicator found!", id)
return
}
(*ind).Set(float64(count))
}
func updateMissingHeights(id string, delta int) {
ind := getOrCreateIndicator(id)
if ind == nil {
zap.S().Warnf("Could not update metrics dor id '%s', nil indicator found!", id)
return
}
(*ind).Sub(float64(delta))
}
func createNewIndicator(id string) (error, *zmetrics.Gauge) {
m := zmetrics.NewGauge(zmetrics.GaugeOpts{
Namespace: "zindexer",
Subsystem: "tracker",
Name: formatIdForName(id),
Help: fmt.Sprintf("Remaning blocks for indexer %s", id),
})
m.Set(0)
err := zmetrics.RegisterMetric(m)
if err != nil {
zap.S().Errorf("Could not register Metric for id: %s", id)
return err, nil
}
metricsMap[id] = &m
return nil, &m
}
func getOrCreateIndicator(id string) *zmetrics.Gauge {
if ind, ok := metricsMap[id]; ok {
return ind
}
err, ind := createNewIndicator(id)
if err != nil {
zap.S().Warnf("Could not create indicator: %v", err)
return nil
}
return ind
}
func formatIdForName(name string) string {
s := matchFirstCap.ReplaceAllString(name, "${1}_${2}")
s = matchAllCap.ReplaceAllString(s, "${1}_${2}")
s = strings.ToLower(s)
return fmt.Sprintf("remaning_blocks_%s", s)
}