/
statcap.go
157 lines (128 loc) · 3.39 KB
/
statcap.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
package main
import (
"encoding/json"
"flag"
"log"
"os"
"os/signal"
"strings"
"time"
"github.com/dustin/gomemcached/client"
"github.com/dustin/statcap/mapconv"
"github.com/dustin/statcap/statstore"
)
var sleepTime = flag.Duration("sleep", 5*time.Second,
"Sleep time between samples")
var server *string = flag.String("server", "localhost:11211",
"memcached server to connect to")
var outPath *string = flag.String("out", "http://localhost:5984/stats",
"http://couch.db/path or a /file/path")
var protoFile *string = flag.String("proto", "",
"Proto document, into which timings stats will be added")
var additionalStats *string = flag.String("stats", "timings,kvtimings",
"stats to fetch beyond toplevel; comma separated")
// That from which we get stats
type fetcher interface {
StatsMap(which string) (map[string]string, error)
Close()
}
func store(db statstore.Storer, ts time.Time, m map[string]interface{}) error {
_, _, err := db.Insert(statstore.NewItem(m, ts))
if err != nil {
log.Printf("Error inserting data: %v", err)
}
return err
}
// Get stats, converting as many values to numbers as possible.
// ...unless there's no connection, in which case we'll return empty stats.
func getNumericStats(client fetcher, which string) (rv map[string]interface{}) {
if client == nil {
rv = make(map[string]interface{})
} else {
rv = mapconv.Numerify(client.StatsMap(which))
}
return
}
func connect() *memcached.Client {
client, err := memcached.Connect("tcp", *server)
if err != nil {
log.Printf("Error connecting to %s: %v", *server, err)
return nil
}
return client
}
func fetchOnce(client fetcher,
proto map[string]interface{}) (fetcher, int, map[string]interface{}) {
allstats := map[string]interface{}{}
for k, v := range proto {
allstats[k] = v
}
allstats["ts"] = time.Now()
all := getNumericStats(client, "")
captured := len(all)
allstats["all"] = all
if *additionalStats != "" {
additional := strings.Split(*additionalStats, ",")
for _, name := range additional {
st := getNumericStats(client, name)
captured += len(st)
if len(st) > 0 {
allstats[name] = st
}
}
}
return client, captured, allstats
}
func gatherStats(client fetcher, db statstore.Storer,
proto map[string]interface{}) {
running := true
sigch := make(chan os.Signal, 10)
signal.Notify(sigch, os.Interrupt)
ticker := time.NewTicker(*sleepTime)
for running {
var captured int
var allstats map[string]interface{}
client, captured, allstats = fetchOnce(client, proto)
if captured > 0 {
log.Printf("Captured %d stats", captured)
go store(db, time.Now(), allstats)
} else {
if client != nil {
client.Close()
}
client = connect()
}
select {
case <-ticker.C:
// Normal "sleep"
case sig := <-sigch:
running = false
log.Printf("Got %v, shutting down.", sig)
}
}
}
func main() {
flag.Parse()
out, err := statstore.GetStorer(*outPath)
if err != nil {
log.Fatalf("Error creating storer: %v", err)
}
defer out.Close()
client := connect()
if client == nil {
log.Fatalf("Error making first connection to couch")
}
defer client.Close()
proto := map[string]interface{}{}
if *protoFile != "" {
f, err := os.Open(*protoFile)
if err != nil {
log.Fatalf("Error opening proto file: %v", err)
}
err = json.NewDecoder(f).Decode(&proto)
if err != nil {
log.Fatalf("Error parsing proto: %v", err)
}
}
gatherStats(client, out, proto)
}