This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
metrics.go
162 lines (139 loc) · 4.35 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
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
// Copyright (C) 2018 MediBloc
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package metrics
import (
"fmt"
"os"
"runtime"
"strings"
"time"
medletpb "github.com/medibloc/go-medibloc/medlet/pb"
"github.com/medibloc/go-medibloc/util/logging"
metrics "github.com/rcrowley/go-metrics"
"github.com/rcrowley/go-metrics/exp"
)
// MetricsEnabledFlag metrics enable flag
const (
interval = 2 * time.Second
chainID = "chainID"
EnabledFlag = "metrics"
)
var (
enable = false
quitCh chan bool
)
// Medlet interface breaks cycle import dependency.
type Medlet interface {
Config() *medletpb.Config
}
func init() {
for _, arg := range os.Args {
if strings.TrimLeft(arg, "-") == EnabledFlag {
EnableMetrics()
return
}
}
}
// EnableMetrics enable the metrics service
func EnableMetrics() {
enable = true
exp.Exp(metrics.DefaultRegistry)
}
// Start metrics monitor
func Start(med Medlet) {
logging.Info("Starting Metrics...")
go (func() {
tags := make(map[string]string)
metricsConfig := med.Config().Stats.MetricsTags
for _, v := range metricsConfig {
values := strings.Split(v, ":")
if len(values) != 2 {
continue
}
tags[values[0]] = values[1]
}
tags[chainID] = fmt.Sprintf("%d", med.Config().Global.ChainId)
go collectSystemMetrics()
InfluxDBWithTags(metrics.DefaultRegistry, interval, med.Config().Stats.Influxdb.Host, med.Config().Stats.Influxdb.Db, med.Config().Stats.Influxdb.User, med.Config().Stats.Influxdb.Password, tags)
logging.Info("Started Metrics.")
})()
logging.Info("Started Metrics.")
}
func collectSystemMetrics() {
memstats := make([]*runtime.MemStats, 2)
for i := 0; i < len(memstats); i++ {
memstats[i] = new(runtime.MemStats)
}
allocs := metrics.GetOrRegisterMeter("system_allocs", nil)
// totalAllocs := metrics.GetOrRegisterMeter("system_total_allocs", nil)
sys := metrics.GetOrRegisterMeter("system_sys", nil)
frees := metrics.GetOrRegisterMeter("system_frees", nil)
heapInuse := metrics.GetOrRegisterMeter("system_heapInuse", nil)
stackInuse := metrics.GetOrRegisterMeter("system_stackInuse", nil)
for i := 1; ; i++ {
select {
case <-quitCh:
return
default:
runtime.ReadMemStats(memstats[i%2])
allocs.Mark(int64(memstats[i%2].Alloc - memstats[(i-1)%2].Alloc))
sys.Mark(int64(memstats[i%2].Sys - memstats[(i-1)%2].Sys))
frees.Mark(int64(memstats[i%2].Frees - memstats[(i-1)%2].Frees))
heapInuse.Mark(int64(memstats[i%2].HeapInuse - memstats[(i-1)%2].HeapInuse))
stackInuse.Mark(int64(memstats[i%2].StackInuse - memstats[(i-1)%2].StackInuse))
time.Sleep(2 * time.Second)
}
}
}
// Stop metrics monitor
func Stop() {
logging.Info("Stopping Metrics...")
quitCh <- true
}
// NewCounter create a new metrics Counter
func NewCounter(name string) metrics.Counter {
if !enable {
return new(metrics.NilCounter)
}
return metrics.GetOrRegisterCounter(name, metrics.DefaultRegistry)
}
// NewMeter create a new metrics Meter
func NewMeter(name string) metrics.Meter {
if !enable {
return new(metrics.NilMeter)
}
return metrics.GetOrRegisterMeter(name, metrics.DefaultRegistry)
}
// NewTimer create a new metrics Timer
func NewTimer(name string) metrics.Timer {
if !enable {
return new(metrics.NilTimer)
}
return metrics.GetOrRegisterTimer(name, metrics.DefaultRegistry)
}
// NewGauge create a new metrics Gauge
func NewGauge(name string) metrics.Gauge {
if !enable {
return new(metrics.NilGauge)
}
return metrics.GetOrRegisterGauge(name, metrics.DefaultRegistry)
}
// NewHistogramWithUniformSample create a new metrics History with Uniform Sample algorithm.
func NewHistogramWithUniformSample(name string, reservoirSize int) metrics.Histogram {
if !enable {
return new(metrics.NilHistogram)
}
return metrics.GetOrRegisterHistogram(name, nil, metrics.NewUniformSample(reservoirSize))
}