/
main.go
60 lines (48 loc) · 1.06 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
package main
import (
"dropsonde-agent/agent"
"dropsonde-agent/emitter"
"flag"
"log"
"os"
"os/signal"
"runtime/pprof"
"syscall"
)
func main() {
log.Printf("Agent starting, use CTL-C to quit\n")
defer log.Printf("Agent stopped\n")
var debug bool
flag.BoolVar(&debug, "debug", false, "Enable debug logging.")
flag.Parse()
if debug {
emitter.DefaultEmitter = emitter.NewLoggingEmitter()
}
stopChan := make(chan struct{})
go func() {
err := agent.Run(stopChan)
if err != nil {
log.Fatalf("failed to run agent: %v", err)
}
}()
killChan := make(chan os.Signal, 2)
signal.Notify(killChan, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case <-RegisterGoRoutineDumpSignalChannel():
DumpGoRoutine()
case <-killChan:
close(stopChan)
return
}
}
}
func DumpGoRoutine() {
goRoutineProfiles := pprof.Lookup("goroutine")
goRoutineProfiles.WriteTo(os.Stdout, 2)
}
func RegisterGoRoutineDumpSignalChannel() chan os.Signal {
threadDumpChan := make(chan os.Signal, 16)
signal.Notify(threadDumpChan, syscall.SIGUSR1)
return threadDumpChan
}