Skip to content

Commit

Permalink
command/debug: add index document
Browse files Browse the repository at this point in the history
  • Loading branch information
pearkes committed Oct 10, 2018
1 parent 2fa6301 commit ae2e1be
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
47 changes: 45 additions & 2 deletions command/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ const (
// to ensure that all information can be collected in time
debugMinDuration = 10 * time.Second

// The extension for archive files
// debugArchiveExtension is the extension for archive files
debugArchiveExtension = ".tar.gz"

// debugProtocolVersion is the version of the package that is
// generated. If this format changes interface, this version
// can be incremented so clients can selectively support packages
debugProtocolVersion = 1
)

func New(ui cli.Ui, shutdownCh <-chan struct{}) *cmd {
Expand Down Expand Up @@ -77,6 +82,23 @@ type cmd struct {
// validateTiming can be used to skip validation of interval, duration. This
// is primarily useful for testing
validateTiming bool

index *debugIndex
}

// debugIndex is used to manage the summary of all data recorded
// during the debug, to be written to json at the end of the run
// and stored at the root. Each attribute corresponds to a file or files.
type debugIndex struct {
// Version of the debug package
Version int
// Version of the target Consul agent
AgentVersion string

Interval string
Duration string

Targets []string
}

func (c *cmd) init() {
Expand Down Expand Up @@ -150,6 +172,15 @@ func (c *cmd) Run(args []string) int {
c.UI.Info(fmt.Sprintf(" Output: '%s'", archiveName))
c.UI.Info(fmt.Sprintf(" Capture: '%s'", strings.Join(c.capture, ", ")))

// Record some information for the index at the root of the archive
index := &debugIndex{
Version: debugProtocolVersion,
AgentVersion: version,
Interval: c.interval.String(),
Duration: c.duration.String(),
Targets: c.capture,
}

// Add the extra grace period to ensure
// all intervals will be captured within the time allotted
c.duration = c.duration + debugDurationGrace
Expand All @@ -161,14 +192,26 @@ func (c *cmd) Run(args []string) int {
}

// Capture dynamic information from the target agent, blocking for duration
// TODO(pearkes): figure out a cleaner way to do this
if c.configuredTarget("metrics") || c.configuredTarget("logs") || c.configuredTarget("pprof") {
err = c.captureDynamic()
if err != nil {
c.UI.Error(fmt.Sprintf("Error encountered during collection: %v", err))
}
}

// Write the index document
idxMarshalled, err := json.MarshalIndent(index, "", "\t")
if err != nil {
c.UI.Error(fmt.Sprintf("Error marshalling index document: %v", err))
return 1
}

err = ioutil.WriteFile(fmt.Sprintf("%s/index.json", c.output), idxMarshalled, 0644)
if err != nil {
c.UI.Error(fmt.Sprintf("Error creating index document: %v", err))
return 1
}

// Archive the data if configured to
if c.archive {
err = c.createArchive()
Expand Down
2 changes: 1 addition & 1 deletion command/debug/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestDebugCommand_Archive(t *testing.T) {
}

// should only contain this one capture target
if h.Name != "debug/agent.json" {
if h.Name != "debug/agent.json" && h.Name != "debug/index.json" {
t.Fatalf("archive contents do not match: %s", h.Name)
}
}
Expand Down

0 comments on commit ae2e1be

Please sign in to comment.