-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (106 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
119
package main
import (
"context"
"fmt"
"github.com/alecthomas/kong"
"github.com/spacemonkeygo/monkit/v3"
"go.uber.org/zap"
"log"
"net"
"os"
"reflect"
"runtime"
"runtime/pprof"
"storj.io/common/storj"
dbg "storj.io/private/debug"
"strings"
)
func main() {
zapLog, err := zap.NewDevelopment()
if err != nil {
panic(err)
}
if os.Getenv("STBB_PPROF") != "" {
var output *os.File
output, err := os.Create(os.Getenv("STBB_PPROF"))
if err != nil {
panic(err)
}
defer func() {
output.Close()
}()
err = pprof.StartCPUProfile(output)
if err != nil {
panic(err)
}
defer pprof.StopCPUProfile()
}
if os.Getenv("STBB_MONKIT") != "" {
filter := strings.ToLower(os.Getenv("STBB_MONKIT"))
defer func() {
monkit.Default.Stats(func(key monkit.SeriesKey, field string, val float64) {
if filter == "true" || strings.Contains(strings.ToLower(key.String()), filter) {
fmt.Println(key, field, val)
}
})
}()
}
if os.Getenv("STBB_PPROF_ALLOCS") != "" {
var output *os.File
output, err := os.Create(os.Getenv("STBB_PPROF_ALLOCS"))
if err != nil {
panic(err)
}
defer func() {
output.Close()
}()
defer func() {
err = pprof.Lookup("allocs").WriteTo(output, 0)
if err != nil {
panic(err)
}
}()
}
if os.Getenv("STBB_DEBUG") != "" {
fmt.Println("stating debug server")
listener, err := net.Listen("tcp", os.Getenv("STBB_DEBUG"))
if err != nil {
panic(err)
}
dbgServer := dbg.NewServer(zapLog, listener, monkit.Default, dbg.Config{})
go func() {
err := dbgServer.Run(context.Background())
if err != nil {
fmt.Println(err)
}
}()
defer dbgServer.Close()
}
var cli Checksum
ctx := kong.Parse(&cli,
kong.TypeMapper(reflect.TypeOf(storj.NodeURL{}), kong.MapperFunc(func(ctx *kong.DecodeContext, target reflect.Value) error {
s := ctx.Scan.Pop().Value.(string)
url, err := storj.ParseNodeURL(s)
if err != nil {
return err
}
target.Set(reflect.ValueOf(url))
return nil
})),
)
kong.Bind(ctx)
err = ctx.Run(ctx)
if err != nil {
log.Fatalf("%+v", err)
}
}
func readStack() []byte {
buf := make([]byte, 1024)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return buf[:n]
}
buf = make([]byte, 2*len(buf))
}
}