/
reporter.go
191 lines (167 loc) · 4.23 KB
/
reporter.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
189
190
191
package main
import (
"bufio"
"errors"
"gopkg.in/tomb.v2"
"net"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/DataDog/datadog-go/statsd"
log "github.com/cihub/seelog"
)
type Client struct {
client *statsd.Client
ip net.IP
port int32
sleep int32
flows *FlowMap
tags []string
lookup map[string]string
t tomb.Tomb
}
const (
statsdBufflen = 5
statsdSleep = 30
)
func memorySize() (uint64, error) {
f, err := os.Open("/proc/meminfo")
if err != nil {
return 0, err
}
s := bufio.NewScanner(f)
if !s.Scan() {
return 0, errors.New("/proc/meminfo parse error")
}
l := s.Text()
fs := strings.Fields(l)
if len(fs) != 3 || fs[2] != "kB" {
return 0, errors.New("/proc/meminfo parse error")
}
kb, err := strconv.ParseUint(fs[1], 10, 64)
if err != nil {
return 0, err
}
//return bytes
return kb * 1024, nil
}
func NewClient(ip net.IP, port int32, sleep int32, flows *FlowMap, lookup map[string]string, tags []string) (*Client, error) {
cli, err := statsd.NewBuffered(net.JoinHostPort(ip.String(), strconv.Itoa(int(port))), statsdBufflen)
if err != nil {
cli = nil
log.Errorf("Error instantiating stats Statter: %v", err)
return nil, err
}
r := &Client{
client: cli,
port: port,
sleep: sleep,
flows: flows,
tags: tags,
lookup: lookup,
}
r.t.Go(r.Report)
return r, nil
}
func (r *Client) Stop() error {
r.t.Kill(nil)
return r.t.Wait()
}
func (r *Client) submit(key, metric string, value float64, tags []string, asHistogram bool) error {
var err error
if asHistogram {
err = r.client.Histogram(metric, value, tags, 1)
} else {
err = r.client.Gauge(metric, value, tags, 1)
}
if err != nil {
log.Infof("There was an issue reporting metric: [%s] %s = %v - error: %v", key, metric, value, err)
return err
} else {
log.Infof("Reported successfully! Metric: [%s] %s = %v - tags: %v", key, metric, value, tags)
}
return nil
}
func (r *Client) Report() error {
defer r.client.Close()
log.Infof("Started reporting.")
memsize, err := memorySize()
if err != nil {
log.Warnf("Error getting memory size. Relying on OOM to keep process in check. Err: %v", err)
}
ticker := time.NewTicker(time.Duration(r.sleep) * time.Second)
done := false
var memstats runtime.MemStats
var pct float64
for !done {
select {
case key := <-r.flows.Expire:
r.flows.Delete(key)
log.Infof("Flow expired: [%s]", key)
case <-ticker.C:
flush := false
now := time.Now().Unix()
runtime.ReadMemStats(&memstats)
if memsize > 0 {
pct = float64(memstats.Alloc) / float64(memsize)
} else {
pct = 0
}
if pct >= FORCE_FLUSH_PCT { //memory out of control
flush = true
log.Warnf("Forcing flush - memory consumption above maximum allowed system usage: %v %%", pct*100)
}
r.flows.Lock()
for k := range r.flows.Map {
flow, e := r.flows.GetUnsafe(k)
flow.Lock()
if e && flow.Sampled > 0 {
success := true
value := float64(flow.SRTT) * float64(time.Nanosecond) / float64(time.Millisecond)
value_jitter := float64(flow.Jitter) * float64(time.Nanosecond) / float64(time.Millisecond)
value_last := float64(flow.Last) * float64(time.Nanosecond) / float64(time.Millisecond)
srcHost, ok := r.lookup[flow.Src.String()]
if !ok {
srcHost = flow.Src.String()
}
dstHost, ok := r.lookup[flow.Dst.String()]
if !ok {
dstHost = flow.Dst.String()
}
tags := []string{"src:" + srcHost, "dst:" + dstHost}
tags = append(tags, r.tags...)
metric := "system.net.tcp.rtt.avg"
err := r.submit(k, metric, value, tags, false)
if err != nil {
success = false
}
metric = "system.net.tcp.rtt.jitter"
err = r.submit(k, metric, value_jitter, tags, false)
if err != nil {
success = false
}
metric = "system.net.tcp.rtt"
err = r.submit(k, metric, value_last, tags, false)
if err != nil {
success = false
}
if success {
log.Debugf("Reported successfully on: %v", k)
}
}
if flush || (now-flow.LastFlush) > FLUSH_IVAL {
log.Debugf("Flushing book-keeping for long-lived flow: %v", k)
flow.Flush()
}
flow.Unlock()
}
r.flows.Unlock()
case <-r.t.Dying():
log.Infof("Done reporting.")
done = true
}
}
return nil
}