-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathhits.go
188 lines (146 loc) · 4.14 KB
/
hits.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
package expmetric
import (
"expvar"
"fmt"
"strings"
"time"
"github.com/kataras/iris/v12"
"github.com/paulbellamy/ratecounter"
)
// HitsPerHour registers an expvar counter which increments
// the number of hits for the last minute.
func HitsPerMinute(options ...Option) iris.Handler {
opts := applyOptions(options)
if opts.MetricName == "" {
opts.MetricName = "hits_per_minute"
}
opts.AvgDiv = 60
return hits(time.Minute, opts)
}
// HitsPerHour registers an expvar counter which increments
// the number of hits for the last second.
func HitsPerSecond(options ...Option) iris.Handler {
opts := applyOptions(options)
if opts.MetricName == "" {
opts.MetricName = "hits_per_second"
}
return hits(time.Second, opts)
}
// HitsPerHour registers an expvar counter which increments
// the number of hits for the last hour.
func HitsPerHour(options ...Option) iris.Handler {
opts := applyOptions(options)
if opts.MetricName == "" {
opts.MetricName = "hits_per_hour"
}
opts.AvgDiv = 24
return hits(time.Hour, opts)
}
// HitsTotal registers an expvar counter which increments
// the number of hits.
func HitsTotal(options ...Option) iris.Handler {
opts := applyOptions(options)
if opts.MetricName == "" {
opts.MetricName = "hits_total"
}
var counter ratecounter.Counter
hitsVar := expvar.NewInt(opts.MetricName)
var hitsAvgVar *expvar.Int
if opts.avgEnabled() {
hitsAvgVar = expvar.NewInt(getMetricNameForAvg(opts.MetricName))
}
return func(ctx iris.Context) {
counter.Incr(1)
value := counter.Value()
hitsVar.Set(value)
if opts.avgEnabled() {
hitsAvgVar.Set(value / opts.AvgDiv)
}
ctx.Next()
}
}
// HitsTotalWithKeyFunc registers a Map expvar counter which increments
// the number of hits inside a map based on the keyFunc.
func HitsTotalWithKeyFunc(keyFunc KeyFunc, options ...Option) iris.Handler {
opts := applyOptions(options)
if opts.MetricName == "" {
panic("iris: expmetric: metric name is empty")
}
hitsVar := expvar.NewMap(opts.MetricName)
return func(ctx iris.Context) {
if key := keyFunc(ctx); key != "" {
hitsVar.Add(key, 1)
}
ctx.Next()
}
}
// HitsTotalPerUserEmail registers a Map expvar counter which increments
// the number of hits per authenticated user's email.
func HitsTotalPerUserEmail(options ...Option) iris.Handler {
keyFunc := func(ctx iris.Context) string {
email, _ := ctx.User().GetEmail()
return email
}
return HitsTotalWithKeyFunc(keyFunc, options...)
}
// HitsTotalPerUserEmail registers a Map expvar counter which increments
// the number of hits per authenticated user's id.
func HitsTotalPerUserID(options ...Option) iris.Handler {
keyFunc := func(ctx iris.Context) string {
email, _ := ctx.User().GetID()
return email
}
return HitsTotalWithKeyFunc(keyFunc, options...)
}
// HitsTotalPerPath registers a Map expvar which
// contains the visited paths and the total count of visits as their value.
func HitsTotalPerPath(options ...Option) iris.Handler {
keyFunc := func(ctx iris.Context) string {
key := ctx.Path()
return key
}
return HitsTotalWithKeyFunc(keyFunc, options...)
}
func getMetricNameForAvg(metricName string) string {
charSplitter := "_"
if strings.Contains(metricName, ".") {
charSplitter = "."
}
return fmt.Sprintf("%s%savg", metricName, charSplitter)
}
func hits(interval time.Duration, opts Options) iris.Handler {
if interval <= 0 {
panic("iris: expmetric: interval zero or less")
}
if opts.MetricName == "" {
panic("iris: expmetric: metric name is empty")
}
if opts.AvgDiv <= 0 {
if interval.Seconds() == 60 {
opts.AvgDiv = 60
} else if interval.Hours() == 24 {
opts.AvgDiv = 24
}
}
hitsVar := expvar.NewInt(opts.MetricName)
var hitsAvgVar *expvar.Int
if opts.avgEnabled() {
hitsAvgVar = expvar.NewInt(getMetricNameForAvg(opts.MetricName))
}
counter := ratecounter.NewRateCounter(interval).WithResolution(opts.Resolution)
counter.OnStop(func(f *ratecounter.RateCounter) {
hitsVar.Set(0)
if opts.avgEnabled() {
hitsAvgVar.Set(0)
}
})
return func(ctx iris.Context) {
counter.Incr(1)
value := counter.Rate()
hitsVar.Set(value)
if opts.avgEnabled() {
hitsAvgVar.Set(value / opts.AvgDiv)
}
ctx.Next()
}
}