Skip to content

Commit

Permalink
feat: support dump profile to logger and fix typos (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun10ng committed Mar 31, 2022
1 parent d0bbb2f commit ca4d9d3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
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

0 comments on commit ca4d9d3

Please sign in to comment.