Skip to content

Commit

Permalink
Merge #36922
Browse files Browse the repository at this point in the history
36922: goroutinedumper: Gzip files r=tbg a=giorgosp

Fixes #36846

Release note: None

Co-authored-by: George Papadrosou <[email protected]>
  • Loading branch information
craig[bot] and giorgosp committed Apr 19, 2019
2 parents f67d5ec + b497e67 commit a38c05b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion pkg/cli/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ func runDebugZip(cmd *cobra.Command, args []string) error {
return z.createError("/goroutines", err)
}
for _, file := range goroutinesResp.Files {
name := prefix + "/goroutines/" + file.Name + ".txt"
// NB: the files have a .txt.gz suffix already.
name := prefix + "/goroutines/" + file.Name
if err := z.createRawOrError(name, file.Contents, err); err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions pkg/server/goroutinedumper/goroutinedumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package goroutinedumper

import (
"compress/gzip"
"context"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -175,14 +176,22 @@ func gc(ctx context.Context, dir string, sizeLimit int64) {
}

func takeGoroutineDump(dir string, filename string) error {
filename = filename + ".txt.gz"
path := filepath.Join(dir, filename)
f, err := os.Create(path)
if err != nil {
return errors.Wrapf(err, "error creating file %s for goroutine dump", path)
}
defer f.Close()
if err = pprof.Lookup("goroutine").WriteTo(f, 2); err != nil {
w := gzip.NewWriter(f)
if err = pprof.Lookup("goroutine").WriteTo(w, 2); err != nil {
return errors.Wrapf(err, "error writing goroutine dump to %s", path)
}
return nil
// Flush and write the gzip header. It doesn't close the underlying writer.
if err := w.Close(); err != nil {
return errors.Wrapf(err, "error closing gzip writer for %s", path)
}
// Return f.Close() too so that we don't miss a potential error if everything
// else succeeded.
return f.Close()
}
4 changes: 2 additions & 2 deletions pkg/server/goroutinedumper/goroutinedumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ func TestTakeGoroutineDump(t *testing.T) {
t.Run("fails because dump already exists as a directory", func(t *testing.T) {
tempDir, dirCleanupFn := testutils.TempDir(t)
defer dirCleanupFn()
filename := "goroutine_dump"
path := filepath.Join(tempDir, filename)
path := filepath.Join(tempDir, "goroutine_dump.txt.gz")
err := os.Mkdir(path, 0755)
assert.NoError(t, err, "failed to make dump directory %s", path)

filename := "goroutine_dump"
err = takeGoroutineDump(tempDir, filename)
assert.Error(t, err)
assert.Contains(
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func TestStatusGetFiles(t *testing.T) {
t.Run("goroutines", func(t *testing.T) {
const testFilesNo = 3
for i := 0; i < testFilesNo; i++ {
testFile := filepath.Join(storeSpec.Path, "logs", goroutinesDir, fmt.Sprintf("goroutine_dump%d.txt", i))
testFile := filepath.Join(storeSpec.Path, "logs", goroutinesDir, fmt.Sprintf("goroutine_dump%d.txt.gz", i))
if err := ioutil.WriteFile(testFile, []byte(fmt.Sprintf("Goroutine dump %d", i)), 0644); err != nil {
t.Fatal(err)
}
Expand All @@ -328,7 +328,7 @@ func TestStatusGetFiles(t *testing.T) {
}

for i, file := range response.Files {
expectedFileName := fmt.Sprintf("goroutine_dump%d.txt", i)
expectedFileName := fmt.Sprintf("goroutine_dump%d.txt.gz", i)
if file.Name != expectedFileName {
t.Fatalf("expected file name %s, found %s", expectedFileName, file.Name)
}
Expand Down

0 comments on commit a38c05b

Please sign in to comment.