This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sender.go
68 lines (52 loc) · 1.51 KB
/
sender.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 stats
import (
"fmt"
"time"
"github.com/modprox/mp/pkg/since"
"github.com/cactus/go-statsd-client/statsd"
)
type Service string
func (s Service) String() string {
return string(s)
}
const (
Proxy Service = "modprox-proxy"
Registry Service = "modprox-registry"
)
//go:generate mockery3 -interface Sender -package statstest
// A Sender is used to emit statsd type metrics.
type Sender interface {
Count(metric string, i int)
Gauge(metric string, n int)
GaugeMS(metric string, t time.Time)
}
// New creates a new Sender which will send metrics to the receiver described
// by the cfg.Agent configuration. All metrics will be emittited under the
// application named by Service s.
func New(s Service, cfg Statsd) (Sender, error) {
address := fmt.Sprintf("%s:%d", cfg.Agent.Address, cfg.Agent.Port)
emitter, err := statsd.NewClient(address, s.String())
return &sender{
emitter: emitter,
}, err
}
type discard struct{}
func (d *discard) Count(string, int) {}
func (d *discard) Gauge(string, int) {}
func (d *discard) GaugeMS(string, time.Time) {}
func Discard() Sender {
return &discard{}
}
type sender struct {
emitter statsd.Statter
}
func (s *sender) Count(metric string, n int) {
_ = s.emitter.Inc(metric, 1, 1)
}
func (s *sender) Gauge(metric string, n int) {
_ = s.emitter.Gauge(metric, int64(n), 1)
}
// GaugeMS gauges the amount of time that has elapsed since t in milliseconds.
func (s *sender) GaugeMS(metric string, t time.Time) {
_ = s.emitter.Gauge(metric, since.MS(t), 1)
}