forked from TheThingsArchive/ttn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
69 lines (58 loc) · 1.5 KB
/
stats.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
// Copyright © 2016 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
// Package stats supports the collection of metrics from a running component.
package stats
import (
"time"
"github.com/rcrowley/go-metrics"
)
// Enabled activates stats collection
var Enabled = true
// Initialize the stats
func Initialize() {
go func() {
c := time.Tick(1 * time.Minute)
for _ = range c {
Registry.(Ticker).Tick()
}
}()
}
// MarkMeter registers an event
func MarkMeter(name string) {
if Enabled {
metrics.GetOrRegisterMeter(name, Registry).Mark(1)
Registry.(Ticker).Renew(name)
}
}
// UpdateHistogram registers a new value for a histogram
func UpdateHistogram(name string, value int64) {
if Enabled {
metrics.GetOrRegisterHistogram(name, Registry, metrics.NewUniformSample(1000)).Update(value)
Registry.(Ticker).Renew(name)
}
}
// IncCounter increments a counter by 1
func IncCounter(name string) {
if Enabled {
metrics.GetOrRegisterCounter(name, Registry).Inc(1)
Registry.(Ticker).Renew(name)
}
}
// DecCounter decrements a counter by 1
func DecCounter(name string) {
if Enabled {
metrics.GetOrRegisterCounter(name, Registry).Dec(1)
Registry.(Ticker).Renew(name)
}
}
// SetString ...
func SetString(name, tag, value string) {
if Enabled {
GetOrRegisterString(name, Registry).Set(tag, value)
Registry.(Ticker).Renew(name)
}
}
// SetTimeout ...
func SetTimeout(name string, ttl uint) {
Registry.(Ticker).SetTTL(name, ttl)
}