forked from atongen/prom_multi_proc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
187 lines (162 loc) · 4.5 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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"os"
"os/signal"
"path"
"runtime"
"syscall"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// build flags
var (
Version string = "development"
BuildTime string = "unset"
BuildHash string = "unset"
GoVersion string = "unset"
)
// cli flags
var (
socketFlag = flag.String("socket", "/tmp/prom_multi_proc.sock", "Path to unix socket to listen on for incoming metrics")
metricsFlag = flag.String("metrics", "", "Path to json file which contains metric definitions")
addrFlag = flag.String("addr", "0.0.0.0:9299", "Address to listen on for exposing prometheus metrics")
pathFlag = flag.String("path", "/metrics", "Path to use for exposing prometheus metrics")
logFlag = flag.String("log", "", "Path to log file, will write to STDOUT if empty")
versionFlag = flag.Bool("v", false, "Print version information and exit")
)
func init() {
prometheus.MustRegister(metricsTotal)
}
func versionStr() string {
return fmt.Sprintf("%s %s %s %s %s", path.Base(os.Args[0]), Version, BuildTime, BuildHash, GoVersion)
}
func main() {
flag.Parse()
if *versionFlag {
fmt.Println(versionStr())
os.Exit(0)
}
// setup logger, this may be reloaded later with HUP signal
err := SetLogger(*logFlag)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// setup metrics and done channels
metricCh := make(chan Metric)
dataCh := make(chan []byte)
doneCh := make(chan bool)
// begin listening on socket
ln, err := net.Listen("unix", *socketFlag)
if err != nil {
logger.Fatal(err)
}
defer ln.Close()
err = os.Chmod(*socketFlag, 0777)
if err != nil {
logger.Fatal(err)
}
// listen for signals which make us quit
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGKILL)
go func() {
<-sigc
logger.Println("Goodbye!")
ln.Close()
os.Exit(0)
}()
// listen for USR1 signal which makes us reload our metrics definitions
sigu := make(chan os.Signal, 1)
signal.Notify(sigu, syscall.SIGUSR1)
go func() {
for {
<-sigu
logger.Println("USR1 Signal received")
// stop the data processor
doneCh <- true
}
}()
registry := NewRegistry()
go func() {
defer func() {
// recover a panic here to make sure socket gets cleaned up
if r := recover(); r != nil {
logger.Printf("Recovered panic: %s", r)
ln.Close()
os.Exit(1)
}
}()
// this for loop must always either continue, or
// exit the process, in other words, never break;
// otherwise data processing will stop and USR1
// signals will not reload the metrics definition json
for {
logger.Println(versionStr())
logger.Println("Loading metric configuration")
// note beginning names of metrics
names := registry.Names()
// reload metrics definitions file
specs, err := LoadSpecs(*metricsFlag)
if err != nil {
logger.Printf("Error loading configuration: %s", err)
} else {
// only register/unregister if there is no error processing
// the metrics definition json
newNames := []string{}
for _, spec := range specs {
newNames = append(newNames, spec.Name)
if err := registry.Register(spec); err != nil {
logger.Println(err)
} else {
logger.Printf("Registered %s", spec.Name)
}
}
// get names of metrics no longer present and unregister them
unreg := sliceSubStr(names, newNames)
for _, name := range unreg {
if err := registry.Unregister(name); err != nil {
logger.Println(err)
} else {
logger.Printf("Unregistered %s", name)
}
}
}
// begin processing incoming metrics
DataProcessor(registry, metricCh, doneCh)
}
// Ensure this process ends if we ever return from the for loop.
logger.Println("Data processing has ended")
ln.Close()
os.Exit(1)
}()
// listen for HUP signal which makes us reopen our log file descriptors
sigh := make(chan os.Signal, 1)
signal.Notify(sigh, syscall.SIGHUP)
go func() {
for {
<-sigh
logger.Println("Re-opening logs...")
err := SetLogger(*logFlag)
if err != nil {
fmt.Println(err)
ln.Close()
os.Exit(1)
}
}
}()
workers := runtime.NumCPU()
for i := 0; i < workers; i++ {
go DataParser(dataCh, metricCh)
}
go DataReader(ln, dataCh)
// setup prometheus http handlers and begin listening
promHandler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
ErrorLog: logger,
})
http.Handle(*pathFlag, promHandler)
http.ListenAndServe(*addrFlag, nil)
}