-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
68 lines (58 loc) · 1.5 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
package rest
import (
"time"
)
// MetricsClient - satisfies a statsd interface
type MetricsClient interface {
Incr(string, []string, float64) error
Timing(string, time.Duration, []string, float64) error
}
// ServiceMetrics - a metrics client
type ServiceMetrics struct {
Client MetricsClient
Logger Logger
Tags []string
}
// NewServiceMetrics - creates and returns a new Metrics instance
func NewServiceMetrics() *ServiceMetrics {
return &ServiceMetrics{}
}
// UseClient -
func (sm *ServiceMetrics) UseClient(client MetricsClient) *ServiceMetrics {
sm.Client = client
return sm
}
// UseLogger -
func (sm *ServiceMetrics) UseLogger(logger Logger) *ServiceMetrics {
sm.Logger = logger
return sm
}
// UseTags -
func (sm *ServiceMetrics) UseTags(tags []string) *ServiceMetrics {
sm.Tags = tags
return sm
}
// Incr - record an increment by count
func (sm *ServiceMetrics) Incr(stat string, count int64) error {
err := sm.Client.Incr(stat, sm.Tags, float64(count))
if err != nil {
sm.Logger.Error(err)
}
return err
}
// Timing - record the time taken to complete an operation
func (sm *ServiceMetrics) Timing(stat string, delta int64) error {
err := sm.Client.Timing(stat, time.Duration(delta), sm.Tags, 1)
if err != nil {
sm.Logger.Error(err)
}
return err
}
// NewTimer - create a function that will calculate and record the timing when called
func (sm *ServiceMetrics) NewTimer(stat string) func() {
start := time.Now()
return func() {
delta := time.Since(start)
sm.Timing(stat, int64(delta))
}
}