diff --git a/doc/zh.md b/doc/zh.md index ede150f..1f362da 100644 --- a/doc/zh.md +++ b/doc/zh.md @@ -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 } ...... diff --git a/holmes.go b/holmes.go index c16b02b..d7441fb 100644 --- a/holmes.go +++ b/holmes.go @@ -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 { @@ -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) } diff --git a/options.go b/options.go index fecda69..4cda8c2 100644 --- a/options.go +++ b/options.go @@ -18,10 +18,11 @@ package holmes import ( - mlog "mosn.io/pkg/log" "sync" "sync/atomic" "time" + + mlog "mosn.io/pkg/log" ) type options struct { @@ -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 @@ -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 diff --git a/readme.md b/readme.md index b81f023..0dedde7 100644 --- a/readme.md +++ b/readme.md @@ -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 } ...... diff --git a/reporters/reporter_test.go b/reporters/reporter_test.go index 89c6ee1..978635d 100644 --- a/reporters/reporter_test.go +++ b/reporters/reporter_test.go @@ -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.")