From 845ea2a984334b110cfa1b1582fcab8286d8b61b Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Mon, 25 Sep 2023 11:00:33 +0200 Subject: [PATCH] [MM-54290] Add CPU profile to support package (#24477) --- server/channels/app/support_packet.go | 25 ++++++++++++++++++++++ server/channels/app/support_packet_test.go | 19 ++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/server/channels/app/support_packet.go b/server/channels/app/support_packet.go index 899bfe1c35..aa3b9b4653 100644 --- a/server/channels/app/support_packet.go +++ b/server/channels/app/support_packet.go @@ -10,6 +10,7 @@ import ( "runtime" "runtime/pprof" "strings" + "time" "github.com/hashicorp/go-multierror" "github.com/pkg/errors" @@ -21,6 +22,10 @@ import ( "github.com/mattermost/mattermost/server/v8/config" ) +const ( + cpuProfileDuration = 5 * time.Second +) + func (a *App) GenerateSupportPacket(c *request.Context) []model.FileData { // If any errors we come across within this function, we will log it in a warning.txt file so that we know why certain files did not get produced if any var warnings []string @@ -35,6 +40,7 @@ func (a *App) GenerateSupportPacket(c *request.Context) []model.FileData { "config": a.createSanitizedConfigFile, "mattermost log": a.getMattermostLog, "notification log": a.getNotificationsLog, + "cpu profile": a.createCPUProfile, "heap profile": a.createHeapProfile, } @@ -294,6 +300,25 @@ func (a *App) createSanitizedConfigFile(_ *request.Context) (*model.FileData, er return fileData, nil } +func (a *App) createCPUProfile(_ *request.Context) (*model.FileData, error) { + var b bytes.Buffer + + err := pprof.StartCPUProfile(&b) + if err != nil { + return nil, errors.Wrap(err, "failed to start CPU profile") + } + + time.Sleep(cpuProfileDuration) + + pprof.StopCPUProfile() + + fileData := &model.FileData{ + Filename: "cpu.prof", + Body: b.Bytes(), + } + return fileData, nil +} + func (a *App) createHeapProfile(*request.Context) (*model.FileData, error) { var b bytes.Buffer diff --git a/server/channels/app/support_packet_test.go b/server/channels/app/support_packet_test.go index 44b43bf8b9..a4f371cb7e 100644 --- a/server/channels/app/support_packet_test.go +++ b/server/channels/app/support_packet_test.go @@ -102,7 +102,15 @@ func TestGenerateSupportPacket(t *testing.T) { fileDatas := th.App.GenerateSupportPacket(ctx) var rFileNames []string - testFiles := []string{"support_packet.yaml", "plugins.json", "sanitized_config.json", "mattermost.log", "notifications.log", "heap.prof"} + testFiles := []string{ + "support_packet.yaml", + "plugins.json", + "sanitized_config.json", + "mattermost.log", + "notifications.log", + "cpu.prof", + "heap.prof", + } for _, fileData := range fileDatas { require.NotNil(t, fileData) assert.Positive(t, len(fileData.Body)) @@ -117,7 +125,14 @@ func TestGenerateSupportPacket(t *testing.T) { err = os.Remove("mattermost.log") require.NoError(t, err) fileDatas = th.App.GenerateSupportPacket(ctx) - testFiles = []string{"support_packet.yaml", "plugins.json", "sanitized_config.json", "heap.prof", "warning.txt"} + testFiles = []string{ + "support_packet.yaml", + "plugins.json", + "sanitized_config.json", + "cpu.prof", + "heap.prof", + "warning.txt", + } rFileNames = nil for _, fileData := range fileDatas { require.NotNil(t, fileData)