forked from heroku/log-shuttle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram_stats.go
More file actions
215 lines (184 loc) · 5.15 KB
/
program_stats.go
File metadata and controls
215 lines (184 loc) · 5.15 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"fmt"
"net"
"os"
"sort"
"strings"
"sync"
"time"
"github.com/bmizerany/perks/quantile"
"github.com/heroku/slog"
)
func Exists(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func cleanUpSocket(path string) error {
if Exists(path) {
return os.Remove(path)
}
return nil
}
type NamedValue struct {
name string
value float64
}
func NewNamedValue(name string, value float64) NamedValue {
return NamedValue{name: name, value: value}
}
type Snapshotter interface {
Snapshot(bool) map[string]interface{}
}
// Emits Values via the standard logger at a given interval
func EmitStats(snapper Snapshotter, interval time.Duration, source string) {
if interval > 0 {
ticker := time.Tick(interval)
for _ = range ticker {
snapshot := snapper.Snapshot(true)
if source != "" {
snapshot["log_shuttle_stats_source"] = source
}
Logger.Println(slog.Context(snapshot))
}
}
}
type ProgramStats struct {
Lost, Drops *Counter
stats map[string]*quantile.Stream
Input chan NamedValue
lastPoll time.Time
exposeStats bool
network, address string
sync.Mutex
}
// Returns a new ProgramStats instance aggregating stats from the input channel
// You will need to Listen() seperately if you need / want to export stats
// polling
func NewProgramStats(listen string, buffer int) *ProgramStats {
var network, address string
if len(listen) == 0 {
network = ""
address = ""
} else {
netDeets := strings.Split(listen, ",")
switch len(netDeets) {
case 2:
network = netDeets[0]
address = netDeets[1]
default:
ErrLogger.Fatalf("Invalid -stats-addr (%s). Must be of form <net>,<addr> (e.g. unix,/tmp/ff or tcp,:8080)\n", listen)
}
}
ps := ProgramStats{
Input: make(chan NamedValue, buffer),
Lost: NewCounter(0),
Drops: NewCounter(0),
lastPoll: time.Now(),
network: network,
address: address,
exposeStats: network != "",
stats: make(map[string]*quantile.Stream),
}
go ps.aggregateValues()
return &ps
}
// Listen for stats requests if we should
func (stats *ProgramStats) Listen() {
if stats.exposeStats {
unixSocket := stats.network == "unix"
if unixSocket {
err := cleanUpSocket(stats.address)
if err != nil {
ErrLogger.Fatalf("Unable to remove old stats socket (%s): %s\n", stats.address, err)
}
}
listener, err := net.Listen(stats.network, stats.address)
if err != nil {
ErrLogger.Fatalf("Unable to listen on %s,%s: %s\n", stats.network, stats.address, err)
}
go func() {
stats.accept(listener)
stats.cleanup(listener)
}()
}
}
// Cleanup after ourselves
// TODO(edwardam): Chances are that we won't get here because we'll exit before
// this
func (stats *ProgramStats) cleanup(listener net.Listener) {
if listener != nil {
listener.Close()
}
if stats.network == "unix" {
if Exists(stats.address) {
err := os.Remove(stats.address)
if err != nil {
ErrLogger.Printf("Unable to remove socket (%s): %s\n", stats.address, err)
}
}
}
}
// Aggregate the values by name as them come in via the input channel
func (stats *ProgramStats) aggregateValues() {
for namedValue := range stats.Input {
stats.Mutex.Lock()
sample, ok := stats.stats[namedValue.name]
// Zero value not good enough, so initialize
if !ok {
sample = quantile.NewTargeted(0.50, 0.95, 0.99)
}
sample.Insert(namedValue.value)
stats.stats[namedValue.name] = sample
stats.Mutex.Unlock()
}
}
// Accept connections and handle them
func (stats *ProgramStats) accept(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
ErrLogger.Printf("Error accepting connection: %s\n", err)
break
}
go stats.handleConnection(conn)
}
}
// we create a buffer (output) in order to sort the output
func (stats *ProgramStats) handleConnection(conn net.Conn) {
defer conn.Close()
snapshot := stats.Snapshot(false)
output := make([]string, 0, len(snapshot))
for key, value := range snapshot {
output = append(output, fmt.Sprintf("%s: %v\n", key, value))
}
sort.Strings(output)
for i := range output {
_, err := conn.Write([]byte(output[i]))
if err != nil {
ErrLogger.Printf("Error writting stats out: %s\n", err)
}
}
}
// Produces a point in time snapshot of the quantiles/other stats
// If reset is true, then call Reset() on each of the quantiles
func (stats *ProgramStats) Snapshot(reset bool) map[string]interface{} {
snapshot := make(map[string]interface{})
// We don't need locks for these values
snapshot["alltime.drops.count"] = stats.Drops.AllTime()
snapshot["alltime.lost.count"] = stats.Lost.AllTime()
stats.Mutex.Lock()
defer stats.Mutex.Unlock()
snapshot["last.stats.connection.since.seconds"] = time.Now().Sub(stats.lastPoll).Seconds()
for name, stream := range stats.stats {
base := name + "."
snapshot[base+"count"] = stream.Count()
snapshot[base+"p50.seconds"] = time.Duration(stream.Query(0.50) * float64(time.Second))
snapshot[base+"p95.seconds"] = time.Duration(stream.Query(0.95) * float64(time.Second))
snapshot[base+"p99.seconds"] = time.Duration(stream.Query(0.99) * float64(time.Second))
if reset {
stream.Reset()
}
}
return snapshot
}