-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
118 lines (98 loc) · 2.23 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
)
var configFile = flag.String("c", "", "config file to use")
func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
os.Exit(2)
}
func main() {
flag.Parse()
if *configFile == "" {
usage()
}
config := parseConfig(*configFile)
//get all modules
modules := getModules(config)
//start loop
ticker := time.NewTicker(time.Duration(config.Interval) * time.Second)
quit := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
tickModules(config, &modules)
case <-quit:
ticker.Stop()
return
}
}
}()
//catch sigkill, stop loop, cleanup
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
for sig := range sigChan {
log.Printf("captured %v, stopping metrics collection and exiting", sig)
//run any cleanup steps
close(quit)
tearDownModules(&modules)
os.Exit(0)
}
}()
// run forever
select {}
}
func tickModules(config Config, modules *Modules) {
var start = time.Now()
allMetrics := []*ModuleMetrics{}
// send metric requests to the input modules in a non blocking manner
for i, c := range modules.InputChannels {
module := modules.InputModules[i]
select {
case c <- 1:
default:
log.Printf("The %s input module is queuing requests", module.Name())
}
}
// collect all metrics that are available
collectMetrics := true
for collectMetrics {
select {
case metrics := <-modules.InputResponseChan:
allMetrics = append(allMetrics, metrics)
default:
collectMetrics = false
}
}
// transform metrics
for _, e := range modules.TransformModules {
_, ok := e.(TransformModule)
if !ok {
log.Printf("%s is not an TransformModule", e.Name())
}
}
// send metrics
for _, e := range modules.OutputModules {
module, ok := e.(OutputModule)
if !ok {
log.Printf("%s is not an OutputModule", e.Name())
} else {
module.SendMetrics(allMetrics)
}
}
// check to make sure the metrics collection isn't taking too long
maxTime := float64(config.Interval) * 0.9
tickTime := time.Since(start).Seconds()
if tickTime >= maxTime {
log.Printf("getInputMetrics took %f seconds", tickTime)
}
}