Skip to content

Commit

Permalink
Add log gathering to debug command. (#10609)
Browse files Browse the repository at this point in the history
  • Loading branch information
ncabatoff committed Dec 22, 2020
1 parent f4db2dd commit e2620b9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
3 changes: 3 additions & 0 deletions changelog/10609.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
command/debug: Now collects logs (at level `trace`) as a periodic output.
```
39 changes: 37 additions & 2 deletions command/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (c *DebugCommand) Flags() *FlagSets {
Usage: "Target to capture, defaulting to all if none specified. " +
"This can be specified multiple times to capture multiple targets. " +
"Available targets are: config, host, metrics, pprof, " +
"replication-status, server-status.",
"replication-status, server-status, log.",
})

return set
Expand Down Expand Up @@ -477,7 +477,7 @@ func (c *DebugCommand) preflight(rawArgs []string) (string, error) {
}

func (c *DebugCommand) defaultTargets() []string {
return []string{"config", "host", "metrics", "pprof", "replication-status", "server-status"}
return []string{"config", "host", "metrics", "pprof", "replication-status", "server-status", "log"}
}

func (c *DebugCommand) captureStaticTargets() error {
Expand Down Expand Up @@ -513,6 +513,7 @@ func (c *DebugCommand) capturePollingTargets() error {
var g run.Group

ctx, cancelFunc := context.WithTimeout(context.Background(), c.flagDuration+debugDurationGrace)
defer cancelFunc()

// This run group watches for interrupt or duration
g.Add(func() error {
Expand Down Expand Up @@ -576,6 +577,15 @@ func (c *DebugCommand) capturePollingTargets() error {
})
}

if strutil.StrListContains(c.flagTargets, "log") {
g.Add(func() error {
_ = c.writeLogs(ctx)
return nil
}, func(error) {
cancelFunc()
})
}

// We shouldn't bump across errors since none is returned by the interrupts,
// but we error check for sanity here.
if err := g.Run(); err != nil {
Expand Down Expand Up @@ -981,3 +991,28 @@ func (c *DebugCommand) captureError(target string, err error) {
})
c.errLock.Unlock()
}

func (c *DebugCommand) writeLogs(ctx context.Context) error {
out, err := os.Create(filepath.Join(c.flagOutput, "vault.log"))
if err != nil {
return err
}
defer out.Close()

logCh, err := c.cachedClient.Sys().Monitor(ctx, "trace")
if err != nil {
return err
}

for {
select {
case log := <-logCh:
_, err = out.WriteString(log)
if err != nil {
return err
}
case <-ctx.Done():
return nil
}
}
}
1 change: 1 addition & 0 deletions command/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ func TestDebugCommand_PartialPermissions(t *testing.T) {
case fh.Name == filepath.Join(basePath, "index.json"):
case fh.Name == filepath.Join(basePath, "replication_status.json"):
case fh.Name == filepath.Join(basePath, "server_status.json"):
case fh.Name == filepath.Join(basePath, "vault.log"):
default:
return fmt.Errorf("unexpected file: %s", fh.Name)
}
Expand Down

0 comments on commit e2620b9

Please sign in to comment.