Repository navigation
Expand file tree
/
Copy pathapphelp.go
More file actions
95 lines (82 loc) · 1.63 KB
/
Copy pathapphelp.go
File metadata and controls
95 lines (82 loc) · 1.63 KB
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
package main
import (
"flag"
"gopp"
"log"
"os"
"os/signal"
"runtime/pprof"
"runtime/trace"
"syscall"
_ "net/http/pprof"
"github.com/google/gops/agent"
)
const (
ltracep = "trace: "
ldebugp = "debug: "
linfop = "info: "
lwarningp = "warning: "
lerrorp = "error: "
lalertp = "alert: "
)
// replacement: dynamic set cpu pprof and trace with gops
var cpuprofile string // = filepath.Base(os.Args[0])
var tracefile string //= "trace.out"
func init() {
flag.StringVar(&cpuprofile, "pprof", cpuprofile, "enable CPU pprof, supply file name.")
flag.StringVar(&tracefile, "trace", cpuprofile, "trace, supply file name.")
}
func SetupProfile() {
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
}
}
func StopProfile() {
if cpuprofile != "" {
log.Println("save profile", cpuprofile)
pprof.StopCPUProfile()
}
}
func SetupTrace() {
if tracefile != "" {
f, err := os.Create("trace.out")
gopp.ErrPrint(err)
trace.Start(f)
log.Println("traceing to trace.out...")
}
}
func StopTrace() {
if tracefile != "" {
trace.Stop()
}
}
// should block
func SetupSignal(exitFunc func()) {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
for {
select {
case s := <-c:
switch s {
case syscall.SIGINT:
log.Println("exiting...", s)
// os.Exit(0) // will not run defer
if exitFunc != nil {
exitFunc()
}
return // will run defer
default:
log.Println("unprocessed signal:", s)
}
}
}
}
func SetupGops() {
if err := agent.Listen(agent.Options{}); err != nil {
log.Fatal(err)
}
}