forked from pingcap/br
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.go
208 lines (189 loc) · 5.74 KB
/
cmd.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2020 PingCAP, Inc. Licensed under Apache-2.0.
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
tidbutils "github.com/pingcap/tidb-tools/pkg/utils"
"github.com/pingcap/tidb/util/logutil"
"github.com/spf13/cobra"
"github.com/pingcap/br/pkg/gluetidb"
"github.com/pingcap/br/pkg/redact"
"github.com/pingcap/br/pkg/summary"
"github.com/pingcap/br/pkg/task"
"github.com/pingcap/br/pkg/utils"
"github.com/pingcap/br/pkg/version/build"
)
var (
initOnce = sync.Once{}
defaultContext context.Context
hasLogFile uint64
tidbGlue = gluetidb.New()
envLogToTermKey = "BR_LOG_TO_TERM"
filterOutSysAndMemTables = []string{
"*.*",
fmt.Sprintf("!%s.*", utils.TemporaryDBName("*")),
"!mysql.*",
"!sys.*",
"!INFORMATION_SCHEMA.*",
"!PERFORMANCE_SCHEMA.*",
"!METRICS_SCHEMA.*",
"!INSPECTION_SCHEMA.*",
}
acceptAllTables = []string{
"*.*",
}
)
const (
// FlagLogLevel is the name of log-level flag.
FlagLogLevel = "log-level"
// FlagLogFile is the name of log-file flag.
FlagLogFile = "log-file"
// FlagLogFormat is the name of log-format flag.
FlagLogFormat = "log-format"
// FlagStatusAddr is the name of status-addr flag.
FlagStatusAddr = "status-addr"
// FlagSlowLogFile is the name of slow-log-file flag.
FlagSlowLogFile = "slow-log-file"
// FlagRedactLog is whether to redact sensitive information in log, already deprecated by FlagRedactInfoLog
FlagRedactLog = "redact-log"
// FlagRedactInfoLog is whether to redact sensitive information in log.
FlagRedactInfoLog = "redact-info-log"
flagVersion = "version"
flagVersionShort = "V"
)
func timestampLogFileName() string {
return filepath.Join(os.TempDir(), time.Now().Format("br.log.2006-01-02T15.04.05Z0700"))
}
// AddFlags adds flags to the given cmd.
func AddFlags(cmd *cobra.Command) {
cmd.Version = build.Info()
cmd.Flags().BoolP(flagVersion, flagVersionShort, false, "Display version information about BR")
cmd.SetVersionTemplate("{{printf \"%s\" .Version}}\n")
cmd.PersistentFlags().StringP(FlagLogLevel, "L", "info",
"Set the log level")
cmd.PersistentFlags().String(FlagLogFile, timestampLogFileName(),
"Set the log file path. If not set, logs will output to temp file")
cmd.PersistentFlags().String(FlagLogFormat, "text",
"Set the log format")
cmd.PersistentFlags().Bool(FlagRedactLog, false,
"Set whether to redact sensitive info in log, already deprecated by --redact-info-log")
cmd.PersistentFlags().Bool(FlagRedactInfoLog, false,
"Set whether to redact sensitive info in log")
cmd.PersistentFlags().String(FlagStatusAddr, "",
"Set the HTTP listening address for the status report service. Set to empty string to disable")
task.DefineCommonFlags(cmd.PersistentFlags())
cmd.PersistentFlags().StringP(FlagSlowLogFile, "", "",
"Set the slow log file path. If not set, discard slow logs")
_ = cmd.PersistentFlags().MarkHidden(FlagSlowLogFile)
_ = cmd.PersistentFlags().MarkHidden(FlagRedactLog)
}
// Init initializes BR cli.
func Init(cmd *cobra.Command) (err error) {
initOnce.Do(func() {
slowLogFilename, e := cmd.Flags().GetString(FlagSlowLogFile)
if e != nil {
err = e
return
}
tidbLogCfg := logutil.LogConfig{}
if len(slowLogFilename) != 0 {
tidbLogCfg.SlowQueryFile = slowLogFilename
// Just for special grpc log file,
// otherwise the info will be print in stdout...
tidbLogCfg.File.Filename = timestampLogFileName()
} else {
// Disable annoying TiDB Log.
// TODO: some error logs outputs randomly, we need to fix them in TiDB.
tidbLogCfg.Level = "fatal"
}
e = logutil.InitLogger(&tidbLogCfg)
if e != nil {
err = e
return
}
// Initialize the logger.
conf := new(log.Config)
conf.Level, err = cmd.Flags().GetString(FlagLogLevel)
if err != nil {
return
}
conf.File.Filename, err = cmd.Flags().GetString(FlagLogFile)
if err != nil {
return
}
conf.Format, err = cmd.Flags().GetString(FlagLogFormat)
if err != nil {
return
}
_, outputLogToTerm := os.LookupEnv(envLogToTermKey)
if outputLogToTerm {
// Log to term if env `BR_LOG_TO_TERM` is set.
conf.File.Filename = ""
}
if len(conf.File.Filename) != 0 {
atomic.StoreUint64(&hasLogFile, 1)
summary.InitCollector(true)
// cmd.PrintErr prints to stderr, but PrintErrf prints to stdout.
cmd.PrintErr(fmt.Sprintf("Detail BR log in %s \n", conf.File.Filename))
}
lg, p, e := log.InitLogger(conf)
if e != nil {
err = e
return
}
log.ReplaceGlobals(lg, p)
redactLog, e := cmd.Flags().GetBool(FlagRedactLog)
if e != nil {
err = e
return
}
redactInfoLog, e := cmd.Flags().GetBool(FlagRedactInfoLog)
if e != nil {
err = e
return
}
redact.InitRedact(redactLog || redactInfoLog)
err = startPProf(cmd)
})
return errors.Trace(err)
}
func startPProf(cmd *cobra.Command) error {
// Initialize the pprof server.
statusAddr, err := cmd.Flags().GetString(FlagStatusAddr)
if err != nil {
return errors.Trace(err)
}
ca, cert, key, err := task.ParseTLSTripleFromFlags(cmd.Flags())
if err != nil {
return errors.Trace(err)
}
// Host isn't used here.
tls, err := tidbutils.NewTLS(ca, cert, key, "localhost", nil)
if err != nil {
return errors.Trace(err)
}
if statusAddr != "" {
return utils.StartPProfListener(statusAddr, tls)
}
utils.StartDynamicPProfListener(tls)
return nil
}
// HasLogFile returns whether we set a log file.
func HasLogFile() bool {
return atomic.LoadUint64(&hasLogFile) != uint64(0)
}
// SetDefaultContext sets the default context for command line usage.
func SetDefaultContext(ctx context.Context) {
defaultContext = ctx
}
// GetDefaultContext returns the default context for command line usage.
func GetDefaultContext() context.Context {
return defaultContext
}