-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
216 lines (195 loc) · 5.14 KB
/
main.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"io"
"log"
"os"
"os/signal"
"time"
"github.com/rapidloop/sop/api"
"github.com/rapidloop/sop/input"
"github.com/rapidloop/sop/model"
"github.com/rapidloop/sop/output"
"github.com/rapidloop/sop/sopdb"
"github.com/rapidloop/sop/sopdb/rdb"
)
var (
VERSION = "1.0-beta.1" // overridden by build script
config model.Config
)
func main() {
if len(os.Args) != 2 {
help(os.Stderr)
os.Exit(1)
}
switch os.Args[1] {
case "-h", "--help":
help(os.Stdout)
return
case "--version":
version()
return
case "-p":
model.PrintExampleConfig()
return
default:
if err := config.Load(os.Args[1]); err != nil {
fmt.Fprintf(os.Stderr, "sop: failed to load config: %v\n", err)
os.Exit(1)
}
}
os.Exit(realmain())
}
func help(w io.Writer) {
fmt.Fprintf(w, `Usage: sop path/to/config
sop is a multi-purpose metrics manipulation tool * https://github.com/rapidloop/sop
sop version %v * (c) RapidLoop, Inc. 2017 * Apache 2.0 License
sop path/to/config start sop with the specified config
sop -p print detailed example configuration to stdout
sop -h, sop --help show this help
sop --version show version info
To get started, try:
$ sop -p > sop.cfg
$ vim sop.cfg
$ sop sop.cfg
`, VERSION)
}
func version() {
fmt.Println(VERSION)
}
//------------------------------------------------------------------------------
func realmain() int {
// setup log
log.SetOutput(os.Stderr)
log.SetFlags(log.Ldate | log.Lmicroseconds | log.Lshortfile)
log.Printf("sop starting: version=%s, pid=%d", VERSION, os.Getpid())
defer log.Print("sop shutdown complete")
// start outputs
outputs := make([]output.Output, 0, len(config.Output))
for _, outcfg := range config.Output {
out, err := output.NewOutput(outcfg)
if err != nil {
log.Printf("failed to create output (type %s): %v", outcfg.Type, err)
log.Printf("configuration was: %+v", outcfg)
return 1
}
if err = out.Start(); err != nil {
log.Printf("failed to start output %s: %v", out.Info(), err)
return 1
}
log.Printf("started output: %s", out.Info())
outputs = append(outputs, out)
}
defer func() {
for _, out := range outputs {
if err := out.Stop(); err != nil {
log.Printf("failed to stop output %s: %v", out.Info(), err)
} else {
log.Printf("stopped output: %s", out.Info())
}
}
}()
// open db
tdb := time.Now()
db, err := rdb.NewDB(config.General.DataPath)
if err != nil {
log.Printf("failed to open database at %q: %v", config.General.DataPath,
err)
return 1
}
elapsed := time.Since(tdb)
log.Printf("started database: path=%s (took %v)", config.General.DataPath,
elapsed)
defer func() {
if err := db.Close(); err != nil {
log.Printf("failed to stop database: %v", err)
} else {
log.Print("stopped database")
}
}()
// create storer
storer := sopdb.NewStorer(db, config.General, outputs)
storer.Start()
log.Printf("started storer: ttl=%v, downsample=%v", storer.TTL(),
storer.Downsample())
defer func() {
storer.Stop()
log.Print("stopped storer")
}()
// create reaper
if config.General.RetentionDays <= 0 || config.General.RetentionGCHours <= 0 {
log.Print("retention/gc not set, old data will not be scrubbed")
} else {
reaper := sopdb.NewReaper(db, config.General)
reaper.Start()
log.Printf("started reaper: retain=%v, gc=%v", reaper.Retain(),
reaper.Interval())
defer func() {
reaper.Stop()
log.Print("stopped reaper")
}()
}
// start inputs
inputs := make([]input.Input, 0, len(config.Input))
for _, inpcfg := range config.Input {
inp, err := input.NewInput(inpcfg, storer)
if err != nil {
log.Printf("failed to create input (type %s): %v", inpcfg.Type, err)
log.Printf("configuration was: %+v", inpcfg)
return 1
}
if err = inp.Start(); err != nil {
log.Printf("failed to start input %s: %v", inp.Info(), err)
return 1
}
log.Printf("started input: %s", inp.Info())
inputs = append(inputs, inp)
}
defer func() {
for _, inp := range inputs {
if err := inp.Stop(); err != nil {
log.Printf("failed to stop input %s: %v", inp.Info(), err)
} else {
log.Printf("stopped input: %s", inp.Info())
}
}
}()
// start APIs
apis := make([]api.API, 0, len(config.API))
for _, apicfg := range config.API {
oneAPI, err := api.NewAPI(apicfg, config.General, db)
if err != nil {
log.Printf("failed to create api (type %s): %v", apicfg.Type, err)
log.Printf("configuration was: %+v", apicfg)
return 1
}
if err = oneAPI.Start(); err != nil {
log.Printf("failed to start api %s: %v", oneAPI.Info(), err)
return 1
}
log.Printf("started api: %s", oneAPI.Info())
apis = append(apis, oneAPI)
}
defer func() {
for _, oneAPI := range apis {
if err := oneAPI.Stop(); err != nil {
log.Printf("failed to stop api %s: %v", oneAPI.Info(), err)
} else {
log.Printf("stopped api: %s", oneAPI.Info())
}
}
}()
// ready
log.Print("sop open for business")
// wait for signal
log.Printf("exiting on signal: %v", waitForInterrupt())
return 0
}
func waitForInterrupt() (s os.Signal) {
ch := make(chan os.Signal, 5)
signal.Notify(ch, os.Interrupt)
s = <-ch
signal.Stop(ch)
close(ch)
return
}