[MM-54290] Add CPU profile to support package (#24477)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
153ebc31d7
Коммит
845ea2a984
@@ -10,6 +10,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hashicorp/go-multierror"
|
"github.com/hashicorp/go-multierror"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -21,6 +22,10 @@ import (
|
|||||||
"github.com/mattermost/mattermost/server/v8/config"
|
"github.com/mattermost/mattermost/server/v8/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
cpuProfileDuration = 5 * time.Second
|
||||||
|
)
|
||||||
|
|
||||||
func (a *App) GenerateSupportPacket(c *request.Context) []model.FileData {
|
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
|
// 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
|
var warnings []string
|
||||||
@@ -35,6 +40,7 @@ func (a *App) GenerateSupportPacket(c *request.Context) []model.FileData {
|
|||||||
"config": a.createSanitizedConfigFile,
|
"config": a.createSanitizedConfigFile,
|
||||||
"mattermost log": a.getMattermostLog,
|
"mattermost log": a.getMattermostLog,
|
||||||
"notification log": a.getNotificationsLog,
|
"notification log": a.getNotificationsLog,
|
||||||
|
"cpu profile": a.createCPUProfile,
|
||||||
"heap profile": a.createHeapProfile,
|
"heap profile": a.createHeapProfile,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -294,6 +300,25 @@ func (a *App) createSanitizedConfigFile(_ *request.Context) (*model.FileData, er
|
|||||||
return fileData, nil
|
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) {
|
func (a *App) createHeapProfile(*request.Context) (*model.FileData, error) {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
|
|
||||||
|
|||||||
@@ -102,7 +102,15 @@ func TestGenerateSupportPacket(t *testing.T) {
|
|||||||
|
|
||||||
fileDatas := th.App.GenerateSupportPacket(ctx)
|
fileDatas := th.App.GenerateSupportPacket(ctx)
|
||||||
var rFileNames []string
|
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 {
|
for _, fileData := range fileDatas {
|
||||||
require.NotNil(t, fileData)
|
require.NotNil(t, fileData)
|
||||||
assert.Positive(t, len(fileData.Body))
|
assert.Positive(t, len(fileData.Body))
|
||||||
@@ -117,7 +125,14 @@ func TestGenerateSupportPacket(t *testing.T) {
|
|||||||
err = os.Remove("mattermost.log")
|
err = os.Remove("mattermost.log")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
fileDatas = th.App.GenerateSupportPacket(ctx)
|
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
|
rFileNames = nil
|
||||||
for _, fileData := range fileDatas {
|
for _, fileData := range fileDatas {
|
||||||
require.NotNil(t, fileData)
|
require.NotNil(t, fileData)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user