From ef7bec6d2abbd8a809dbf29913afdf7d91701f70 Mon Sep 17 00:00:00 2001 From: fibbery Date: Sat, 16 Sep 2023 02:07:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3(inuse=5Fobjects=E5=92=8Cinuse=5Fspace?= =?UTF-8?q?=E7=9A=84=E4=B8=8A=E6=8A=A5=E5=A4=84=E7=90=86)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- reporters/pyroscope_reporter/client_config.go | 59 +++++++++++++++---- .../pyroscope_reporter/pyroscope_client.go | 42 ++++++++++--- .../pyroscope_client_test.go | 10 ++-- 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/reporters/pyroscope_reporter/client_config.go b/reporters/pyroscope_reporter/client_config.go index 453a0b6..bf11c31 100644 --- a/reporters/pyroscope_reporter/client_config.go +++ b/reporters/pyroscope_reporter/client_config.go @@ -49,17 +49,56 @@ const ( ReservedTagKeyName = "__name__" ) +var ( + heapSampleTypes = map[string]*SampleType{ + "alloc_objects": { + Units: "objects", + Cumulative: false, + }, + "alloc_space": { + Units: "bytes", + Cumulative: false, + }, + "inuse_space": { + Units: "bytes", + Aggregation: "average", + Cumulative: false, + }, + "inuse_objects": { + Units: "objects", + Aggregation: "average", + Cumulative: false, + }, + } + goroutineSampleTypes = map[string]*SampleType{ + "goroutine": { + DisplayName: "goroutines", + Units: "goroutines", + Aggregation: "average", + }, + } +) + +type SampleType struct { + Units string `json:"units,omitempty"` + Aggregation string `json:"aggregation,omitempty"` + DisplayName string `json:"display-name,omitempty"` + Sampled bool `json:"sampled,omitempty"` + Cumulative bool `json:"cumulative,omitempty"` +} + type UploadJob struct { - Name string - StartTime time.Time - EndTime time.Time - SpyName string - SampleRate uint32 - Units string - AggregationType string - Format UploadFormat - Profile []byte - PrevProfile []byte + Name string + StartTime time.Time + EndTime time.Time + SpyName string + SampleRate uint32 + Units string + AggregationType string + Format UploadFormat + Profile []byte + PrevProfile []byte + SampleTypeConfig map[string]*SampleType } type RemoteConfig struct { diff --git a/reporters/pyroscope_reporter/pyroscope_client.go b/reporters/pyroscope_reporter/pyroscope_client.go index a1639c6..50da2ab 100644 --- a/reporters/pyroscope_reporter/pyroscope_client.go +++ b/reporters/pyroscope_reporter/pyroscope_client.go @@ -19,6 +19,7 @@ package pyroscope_reporter import ( "bytes" + "encoding/json" "fmt" "io/ioutil" "mime/multipart" @@ -91,6 +92,17 @@ func (r *PyroscopeReporter) uploadProfile(j *UploadJob) error { return err } } + if j.SampleTypeConfig != nil { + fw, err = writer.CreateFormFile("sample_type_config", "sample_type_config.json") + if err != nil { + return err + } + b, err := json.Marshal(j.SampleTypeConfig) + if err != nil { + return err + } + fw.Write(b) + } writer.Close() // nolint: errcheck q := u.Query() @@ -150,16 +162,18 @@ func (r *PyroscopeReporter) Report(ptype string, filename string, reason holmes. endTime := sampleTime.Truncate(DefaultUploadRate) startTime := endTime.Add(-DefaultUploadRate) _, _, _, _, _ = ptype, filename, reason, eventID, scene + stc := sampleTypeCfg(ptype) j := &UploadJob{ - Name: r.AppName, - StartTime: startTime, - EndTime: endTime, - SpyName: "gospy", - SampleRate: 100, - Units: "samples", - AggregationType: "sum", - Format: Pprof, - Profile: pprofBytes, + Name: r.AppName, + StartTime: startTime, + EndTime: endTime, + SpyName: "gospy", + SampleRate: 100, + Units: "samples", + AggregationType: "sum", + Format: Pprof, + Profile: pprofBytes, + SampleTypeConfig: stc, } if err := r.uploadProfile(j); err != nil { @@ -167,3 +181,13 @@ func (r *PyroscopeReporter) Report(ptype string, filename string, reason holmes. } return nil } + +func sampleTypeCfg(ptype string) map[string]*SampleType { + switch ptype { + case "heap": + return heapSampleTypes + case "goroutine": + return goroutineSampleTypes + } + return nil +} diff --git a/reporters/pyroscope_reporter/pyroscope_client_test.go b/reporters/pyroscope_reporter/pyroscope_client_test.go index ca69600..8050f53 100644 --- a/reporters/pyroscope_reporter/pyroscope_client_test.go +++ b/reporters/pyroscope_reporter/pyroscope_client_test.go @@ -7,6 +7,7 @@ import ( "time" "github.com/gin-gonic/gin" + "mosn.io/holmes" ) @@ -16,11 +17,9 @@ func TestMain(m *testing.M) { log.Println("holmes initialing") h, _ = holmes.New( holmes.WithCollectInterval("1s"), - holmes.WithDumpPath("./"), - holmes.WithTextDump(), ) log.Println("holmes initial success") - h.EnableCPUDump().Start() + h.EnableMemDump().EnableGoroutineDump().EnableCPUDump().Start() time.Sleep(11 * time.Second) log.Println("on running") newMockServer() @@ -47,9 +46,10 @@ func TestPyroscopeClient(t *testing.T) { err = h.Set( holmes.WithProfileReporter(pReporter), - holmes.WithGoroutineDump(5, 10, 20, 90, time.Second), + holmes.WithGoroutineDump(0, 0, 1, 2, time.Second), holmes.WithCPUDump(0, 2, 80, time.Second), - holmes.WithCollectInterval("5s"), + holmes.WithMemDump(0, 1, 1, time.Second), + holmes.WithCollectInterval("1s"), ) if err != nil { log.Fatalf("fail to set opts on running time.")