Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:supports dump goroutine to logger and fix typos #91

Merged
merged 11 commits into from Mar 31, 2022
2 changes: 1 addition & 1 deletion doc/zh.md
Expand Up @@ -193,7 +193,7 @@ h.Stop()

```go
type ReporterImpl struct{}
func (r *ReporterImple) Report(pType string, buf []byte, reason string, eventID string) error{
func (r *ReporterImpl) Report(pType string, buf []byte, reason string, eventID string) error{
// do something
}
......
Expand Down
14 changes: 14 additions & 0 deletions holmes.go
Expand Up @@ -589,6 +589,15 @@ func (h *Holmes) cpuProfile(curCPUUsage int, c typeOption) bool {
time.Sleep(defaultCPUSamplingTime)
pprof.StopCPUProfile()

if h.opts.DumpToLogger {
bfCpy, err := ioutil.ReadFile(binFileName)
if err != nil {
h.Errorf("encounter error when dumping profile to logger, failed to read cpu profile file: %v", err)
return true
}
h.Infof("[Holmes] CPU profile:: \n" + string(bfCpy))
}

if opts := h.opts.GetReporterOpts(); opts.active == 1 {
bfCpy, err := ioutil.ReadFile(binFileName)
if err != nil {
Expand Down Expand Up @@ -722,6 +731,11 @@ func (h *Holmes) writeProfileDataToFile(data bytes.Buffer, dumpType configureTyp
h.Errorf("failed to write profile to file(%v), err: %s", fileName, err.Error())
return
}

if h.opts.DumpOptions.DumpToLogger {
h.Infof(fmt.Sprintf("[Holmes] %v profile: \n", check2name[dumpType]) + data.String())
}

h.Infof("[Holmes] pprof %v profile write to file %v successfully", check2name[dumpType], fileName)
}

Expand Down
12 changes: 11 additions & 1 deletion options.go
Expand Up @@ -18,10 +18,11 @@
package holmes

import (
mlog "mosn.io/pkg/log"
"sync"
"sync/atomic"
"time"

mlog "mosn.io/pkg/log"
)

type options struct {
Expand Down Expand Up @@ -94,6 +95,8 @@ type DumpOptions struct {
DumpProfileType dumpProfileType
// only dump top 10 if set to false, otherwise dump all, only effective when in_text = true
DumpFullStack bool
// dump profile to logger. It will make huge log output if enable DumpToLogger option. issues/90
DumpToLogger bool
}

// ShrinkThrOptions contains the configuration about shrink thread
Expand Down Expand Up @@ -287,6 +290,13 @@ func WithGoroutineDump(min int, diff int, abs int, max int, coolDown time.Durati
})
}

func WithDumpToLogger(new bool) Option {
return optionFunc(func(opts *options) (err error) {
opts.DumpToLogger = new
return
})
}

type typeOption struct {
Enable bool
// mem/cpu/gcheap trigger minimum in percent, goroutine/thread trigger minimum in number
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -168,7 +168,7 @@ You can use `Reporter` to implement the following features:

```go
type ReporterImpl struct{}
func (r *ReporterImple) Report(pType string, buf []byte, reason string, eventID string) error{
func (r *ReporterImpl) Report(pType string, buf []byte, reason string, eventID string) error{
// do something
}
......
Expand Down
1 change: 1 addition & 0 deletions reporters/reporter_test.go
Expand Up @@ -123,6 +123,7 @@ func TestReporterReopen(t *testing.T) {
holmes.WithGoroutineDump(5, 10, 20, 90, time.Second),
holmes.WithCPUDump(0, 2, 80, time.Second),
holmes.WithCollectInterval("5s"),
holmes.WithDumpToLogger(true),
)
if err != nil {
log.Fatalf("fail to set opts on running time.")
Expand Down