/
root.go
72 lines (63 loc) · 1.64 KB
/
root.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
// Copyright © 2018 One Concern
package cmd
import (
"fmt"
"log"
"os"
"runtime/pprof"
"github.com/oneconcern/datamon/pkg/core"
"github.com/oneconcern/datamon/pkg/dlogger"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var logger *zap.Logger
var rootCmd = &cobra.Command{
Use: "metrics",
Short: "Produce metrics for datamon functionaltiy",
Long: `While the tests in the packages used to build the datamon binary are about correctness,
this executable exists to gather performance metrics, memory and cpu usage for example.
`,
TraverseChildren: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if params.root.cpuProfPath != "" {
if _, err := os.Stat(params.root.memProfPath); os.IsNotExist(err) {
if err := os.Mkdir(params.root.memProfPath, 0777); err != nil {
log.Fatal(err)
}
} else {
if err := os.RemoveAll(params.root.memProfPath); err != nil {
log.Fatal(err)
}
}
core.MemProfDir = params.root.memProfPath
f, err := os.Create(params.root.cpuProfPath)
if err != nil {
log.Fatal(err)
}
_ = pprof.StartCPUProfile(f)
}
},
// upstream api note: *PostRun functions aren't called in case of a panic() in Run
PersistentPostRun: func(cmd *cobra.Command, args []string) {
if params.root.cpuProfPath != "" {
pprof.StopCPUProfile()
}
},
}
func Execute() {
var err error
if err = rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
var err error
log.SetFlags(0)
logger, err = dlogger.GetLogger(dlogger.LogLevelDebug)
if err != nil {
log.Fatalln("Failed to set log level:" + err.Error())
}
addCPUProfPath(rootCmd)
addMemProfPath(rootCmd)
}