-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathdebug.go
More file actions
148 lines (128 loc) · 4.34 KB
/
debug.go
File metadata and controls
148 lines (128 loc) · 4.34 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
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
package debug
import (
"context"
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/spf13/cobra"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/roxctl/central/debug/db"
"github.com/stackrox/rox/roxctl/common"
"github.com/stackrox/rox/roxctl/common/environment"
"github.com/stackrox/rox/roxctl/common/flags"
"github.com/stackrox/rox/roxctl/common/util"
)
var (
levels = getValidLevels()
levelList = strings.Join(levels, " | ")
)
type centralDebugLogLevelCommand struct {
// Properties that are bound to cobra flags.
level string
modules []string
// Properties that are injected or constructed.
env environment.Environment
timeout time.Duration
retryTimeout time.Duration
}
// Command defines the debug command tree
func Command(cliEnvironment environment.Environment) *cobra.Command {
c := &cobra.Command{
Use: "debug",
Short: "Commands for debugging the Central service",
}
c.AddCommand(logLevelCommand(cliEnvironment))
c.AddCommand(dumpCommand(cliEnvironment))
c.AddCommand(downloadDiagnosticsCommand(cliEnvironment))
c.AddCommand(authzTraceCommand(cliEnvironment))
c.AddCommand(db.Command(cliEnvironment))
return c
}
// logLevelCommand allows getting and setting the Log Level for StackRox services.
func logLevelCommand(cliEnvironment environment.Environment) *cobra.Command {
levelCmd := ¢ralDebugLogLevelCommand{env: cliEnvironment}
c := &cobra.Command{
Use: "log",
Short: `Get or set central log level`,
Long: `"log" to get current log level; "log --level=<level>" to set log level.`,
RunE: util.RunENoArgs(func(c *cobra.Command) error {
levelCmd.timeout = flags.Timeout(c)
levelCmd.retryTimeout = flags.RetryTimeout(c)
if levelCmd.level == "" {
return levelCmd.getLogLevel()
}
return levelCmd.setLogLevel()
}),
}
c.Flags().StringVarP(&levelCmd.level, "level", "l", "",
fmt.Sprintf("The log level to set the modules to (%s).", levelList))
c.Flags().StringSliceVarP(&levelCmd.modules, "modules", "m", nil, "The modules to which to apply the command.")
flags.AddTimeout(c)
flags.AddRetryTimeout(c)
return c
}
func (cmd *centralDebugLogLevelCommand) getLogLevel() error {
conn, err := cmd.env.GRPCConnection(common.WithRetryTimeout(cmd.retryTimeout))
if err != nil {
return errors.Wrap(err, "establishing GRPC connection to retrieve log level")
}
defer func() {
_ = conn.Close()
}()
ctx, cancel := context.WithTimeout(context.Background(), cmd.timeout)
defer cancel()
client := v1.NewDebugServiceClient(conn)
logResponse, err := client.GetLogLevel(ctx, &v1.GetLogLevelRequest{Modules: cmd.modules})
if err != nil {
return errors.Wrap(err, "could not get log level from central")
}
cmd.printGetLogLevelResponse(logResponse)
return nil
}
func (cmd *centralDebugLogLevelCommand) printGetLogLevelResponse(r *v1.LogLevelResponse) {
const rowFormat = "%-40s %s"
indent := ""
if r.GetLevel() != "" {
cmd.env.Logger().PrintfLn("Current log level is %s", r.GetLevel())
if len(r.GetModuleLevels()) > 0 {
cmd.env.Logger().PrintfLn("Modules with a different log level:")
indent = " "
}
}
if len(r.GetModuleLevels()) > 0 {
cmd.env.Logger().PrintfLn(indent+rowFormat, "Module", "Level")
cmd.env.Logger().PrintfLn("")
for _, modLvl := range r.GetModuleLevels() {
cmd.env.Logger().PrintfLn(indent+rowFormat, modLvl.GetModule(), modLvl.GetLevel())
}
}
}
func (cmd *centralDebugLogLevelCommand) setLogLevel() error {
conn, err := cmd.env.GRPCConnection(common.WithRetryTimeout(cmd.retryTimeout))
if err != nil {
return errors.Wrap(err, "establishing GRPC connection to set log level")
}
defer func() {
_ = conn.Close()
}()
client := v1.NewDebugServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), cmd.timeout)
defer cancel()
_, err = client.SetLogLevel(ctx, &v1.LogLevelRequest{Level: cmd.level, Modules: cmd.modules})
if err != nil {
return errors.Wrap(err, "could not set log level on central")
}
cmd.env.Logger().PrintfLn("Successfully set log level")
return nil
}
// getValidLevels return level strings in ascending severity order.
func getValidLevels() []string {
sortedLevels := logging.SortedLevels()
labels := make([]string, 0, len(sortedLevels))
for _, lvl := range sortedLevels {
labels = append(labels, logging.LabelForLevelOrInvalid(lvl))
}
return labels
}