-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.metrics.go
221 lines (200 loc) · 5.82 KB
/
app.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package elio
import (
"fmt"
"net/http"
"time"
"github.com/rcrowley/go-metrics"
"github.com/rcrowley/go-metrics/exp"
)
const (
// CaptureInterval capture interval
CaptureInterval = 100 * time.Millisecond
// StatsDInterval capture interval
StatsDInterval = 2500 * time.Millisecond
// StatsDAddress statsd address
StatsDAddress = "127.0.0.1:8125"
)
const (
// MetricAppSessionsf app session metric
MetricAppSessionsf = "%s.sessions"
// MetricAppIoInCountf app io in count metric
MetricAppIoInCountf = "%s.io.incount"
// MetricAppIoInSizef app io in size metric
MetricAppIoInSizef = "%s.io.insize"
// MetricAppIoOutCountf app io out count metric
MetricAppIoOutCountf = "%s.io.outcount"
// MetricAppIoOutSizef app io out size metric
MetricAppIoOutSizef = "%s.io.outsize"
// MetricPubEchosf publish app echo metric
MetricPubEchosf = "%s.pub.echos"
// MetricSubEchosf subscribe app echo metric
MetricSubEchosf = "%s.sub.echos"
// MetricPubAppStatesf publish app state metric
MetricPubAppStatesf = "%s.pub.appstates"
// MetricSubAppStatesf subscribe app state metric
MetricSubAppStatesf = "%s.sub.appstates"
)
// AppMetrics app metrics
type AppMetrics struct {
Registry metrics.Registry
}
// String object to string
func (m *AppMetrics) String() string {
return fmt.Sprintf("AppMetrics::%p", m)
}
// Init init
func (m *AppMetrics) Init(inMetric int) {
m.Registry = metrics.NewRegistry() //metrics.DefaultRegistry //
// debug gc stats
metrics.RegisterDebugGCStats(m.Registry)
go metrics.CaptureDebugGCStats(m.Registry, CaptureInterval)
// runtime mem stats
metrics.RegisterRuntimeMemStats(m.Registry)
go metrics.CaptureRuntimeMemStats(m.Registry, CaptureInterval)
exp.Exp(m.Registry)
//if addr, err := net.ResolveUDPAddr("udp", StatsD_Address); nil != err {
// AlogError().Str(elf.LogObject, m.String()).
// Err(err).Msg("failed to resolve statd address")
//
//} else {
// p := fmt.Sprintf("%s-%s-%s", elf.GetAppParams().GetProject(), elf.GetAppParams().GetStage(), elf.GetAppParams().GetSuid())
// go statsd.StatsD(m.Registry, StatsD_Interval, p, addr)
//}
if 0 != inMetric {
// metric web service:
// http://localhost:{in.metric}/debug/metrics
go http.ListenAndServe(fmt.Sprintf(":%d", inMetric), nil)
}
}
// GetOrRegisterCounter get or register counter
/*//
// Counters hold an int64 value that can be incremented and decremented.
type Counter interface {
Clear()
Count() int64
Dec(int64)
Inc(int64)
Snapshot() Counter
}
//*/
func (m *AppMetrics) GetOrRegisterCounter(name string) metrics.Counter {
return m.Registry.GetOrRegister(name, metrics.NewCounter).(metrics.Counter)
}
// GetOrRegisterEwma get or register EWMA
/*//
// EWMAs continuously calculate an exponentially-weighted moving average
// based on an outside source of clock ticks.
type EWMA interface {
Rate() float64
Snapshot() EWMA
Tick()
Update(int64)
}
//*/
func (m *AppMetrics) GetOrRegisterEwma(name string) metrics.EWMA {
return m.Registry.GetOrRegister(name, metrics.NewEWMA).(metrics.EWMA)
}
// GetOrRegisterGauge get or register gauge
/*//
// Gauges hold an int64 value that can be set arbitrarily.
type Gauge interface {
Snapshot() Gauge
Update(int64)
Value() int64
}
//*/
func (m *AppMetrics) GetOrRegisterGauge(name string) metrics.Gauge {
return m.Registry.GetOrRegister(name, metrics.NewGauge).(metrics.Gauge)
}
// GetOrRegisterGaugeFloat64 get or register gauge float64
/*//
// GaugeFloat64s hold a float64 value that can be set arbitrarily.
type GaugeFloat64 interface {
Snapshot() GaugeFloat64
Update(float64)
Value() float64
}
//*/
func (m *AppMetrics) GetOrRegisterGaugeFloat64(name string) metrics.GaugeFloat64 {
return m.Registry.GetOrRegister(name, metrics.NewGaugeFloat64()).(metrics.GaugeFloat64)
}
// // GetOrRegisterHealthcheck get or register health check
// /*//
// // Healthchecks hold an error value describing an arbitrary up/down status.
// type Healthcheck interface {
// Check()
// Error() error
// Healthy()
// Unhealthy(error)
// }
// //*/
// func (m *AppMetrics) GetOrRegisterHealthcheck(name string, check func(h metrics.Healthcheck)) metrics.Healthcheck {
// return m.Registry.GetOrRegister(name, metrics.NewHealthcheck(check)).(metrics.Healthcheck)
// }
// GetOrRegisterHistogram get or register histogram
/*//
// Histograms calculate distribution statistics from a series of int64 values.
type Histogram interface {
Clear()
Count() int64
Max() int64
Mean() float64
Min() int64
Percentile(float64) float64
Percentiles([]float64) []float64
Sample() Sample
Snapshot() Histogram
StdDev() float64
Sum() int64
Update(int64)
Variance() float64
}
//*/
func (m *AppMetrics) GetOrRegisterHistogram(name string, s metrics.Sample) metrics.Histogram {
return m.Registry.GetOrRegister(name, func() metrics.Histogram { return metrics.NewHistogram(s) }).(metrics.Histogram)
}
// GetOrRegisterMeter get or register meter
/*//
// Meters count events to produce exponentially-weighted moving average rates
// at one-, five-, and fifteen-minutes and a mean rate.
type Meter interface {
Count() int64
Mark(int64)
Rate1() float64
Rate5() float64
Rate15() float64
RateMean() float64
Snapshot() Meter
Stop()
}
//*/
func (m *AppMetrics) GetOrRegisterMeter(name string) metrics.Meter {
return m.Registry.GetOrRegister(name, metrics.NewMeter).(metrics.Meter)
}
// GetOrRegisterTimer get or register timer
/*//
// Timers capture the duration and rate of events.
type Timer interface {
Count() int64
Max() int64
Mean() float64
Min() int64
Percentile(float64) float64
Percentiles([]float64) []float64
Rate1() float64
Rate5() float64
Rate15() float64
RateMean() float64
Snapshot() Timer
StdDev() float64
Stop()
Sum() int64
Time(func())
Update(time.Duration)
UpdateSince(time.Time)
Variance() float64
}
//*/
func (m *AppMetrics) GetOrRegisterTimer(name string) metrics.Timer {
return m.Registry.GetOrRegister(name, metrics.NewTimer).(metrics.Timer)
}