-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsender.go
81 lines (69 loc) · 2.33 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
69
70
71
72
73
74
75
76
77
78
79
80
81
package xstats
import (
"io"
"time"
)
// Sender define an interface to a stats system like statsd or datadog to send
// service's metrics.
type Sender interface {
// Gauge measure the value of a particular thing at a particular time,
// like the amount of fuel in a car’s gas tank or the number of users
// connected to a system.
Gauge(stat string, value float64, tags ...string)
// Count track how many times something happened per second, like
// the number of database requests or page views.
Count(stat string, count float64, tags ...string)
// Histogram track the statistical distribution of a set of values,
// like the duration of a number of database queries or the size of
// files uploaded by users. Each histogram will track the average,
// the minimum, the maximum, the median, the 95th percentile and the count.
Histogram(stat string, value float64, tags ...string)
// Timing mesures the elapsed time
Timing(stat string, value time.Duration, tags ...string)
}
// CloseSender will call Close() on any xstats.Sender that implements io.Closer
func CloseSender(s Sender) error {
if c, ok := s.(io.Closer); ok {
return c.Close()
}
return nil
}
// MultiSender lets you assign more than one sender to xstats in order to
// multicast observeration to different systems.
type MultiSender []Sender
// Gauge implements the xstats.Sender interface
func (s MultiSender) Gauge(stat string, value float64, tags ...string) {
for _, ss := range s {
ss.Gauge(stat, value, tags...)
}
}
// Count implements the xstats.Sender interface
func (s MultiSender) Count(stat string, count float64, tags ...string) {
for _, ss := range s {
ss.Count(stat, count, tags...)
}
}
// Histogram implements the xstats.Sender interface
func (s MultiSender) Histogram(stat string, value float64, tags ...string) {
for _, ss := range s {
ss.Histogram(stat, value, tags...)
}
}
// Timing implements the xstats.Sender interface
func (s MultiSender) Timing(stat string, duration time.Duration, tags ...string) {
for _, ss := range s {
ss.Timing(stat, duration, tags...)
}
}
// Close implements the io.Closer interface
func (s MultiSender) Close() error {
var firstErr error
// attempt to close all senders, return first error encountered
for _, ss := range s {
err := CloseSender(ss)
if err != nil && firstErr == nil {
firstErr = err
}
}
return firstErr
}