Skip to content

Commit e2620b9

Browse files
authored
Add log gathering to debug command. (#10609)
1 parent f4db2dd commit e2620b9

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

changelog/10609.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
command/debug: Now collects logs (at level `trace`) as a periodic output.
3+
```

command/debug.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func (c *DebugCommand) Flags() *FlagSets {
169169
Usage: "Target to capture, defaulting to all if none specified. " +
170170
"This can be specified multiple times to capture multiple targets. " +
171171
"Available targets are: config, host, metrics, pprof, " +
172-
"replication-status, server-status.",
172+
"replication-status, server-status, log.",
173173
})
174174

175175
return set
@@ -477,7 +477,7 @@ func (c *DebugCommand) preflight(rawArgs []string) (string, error) {
477477
}
478478

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

483483
func (c *DebugCommand) captureStaticTargets() error {
@@ -513,6 +513,7 @@ func (c *DebugCommand) capturePollingTargets() error {
513513
var g run.Group
514514

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

517518
// This run group watches for interrupt or duration
518519
g.Add(func() error {
@@ -576,6 +577,15 @@ func (c *DebugCommand) capturePollingTargets() error {
576577
})
577578
}
578579

580+
if strutil.StrListContains(c.flagTargets, "log") {
581+
g.Add(func() error {
582+
_ = c.writeLogs(ctx)
583+
return nil
584+
}, func(error) {
585+
cancelFunc()
586+
})
587+
}
588+
579589
// We shouldn't bump across errors since none is returned by the interrupts,
580590
// but we error check for sanity here.
581591
if err := g.Run(); err != nil {
@@ -981,3 +991,28 @@ func (c *DebugCommand) captureError(target string, err error) {
981991
})
982992
c.errLock.Unlock()
983993
}
994+
995+
func (c *DebugCommand) writeLogs(ctx context.Context) error {
996+
out, err := os.Create(filepath.Join(c.flagOutput, "vault.log"))
997+
if err != nil {
998+
return err
999+
}
1000+
defer out.Close()
1001+
1002+
logCh, err := c.cachedClient.Sys().Monitor(ctx, "trace")
1003+
if err != nil {
1004+
return err
1005+
}
1006+
1007+
for {
1008+
select {
1009+
case log := <-logCh:
1010+
_, err = out.WriteString(log)
1011+
if err != nil {
1012+
return err
1013+
}
1014+
case <-ctx.Done():
1015+
return nil
1016+
}
1017+
}
1018+
}

command/debug_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ func TestDebugCommand_PartialPermissions(t *testing.T) {
685685
case fh.Name == filepath.Join(basePath, "index.json"):
686686
case fh.Name == filepath.Join(basePath, "replication_status.json"):
687687
case fh.Name == filepath.Join(basePath, "server_status.json"):
688+
case fh.Name == filepath.Join(basePath, "vault.log"):
688689
default:
689690
return fmt.Errorf("unexpected file: %s", fh.Name)
690691
}

0 commit comments

Comments
 (0)