Skip to content

Commit

Permalink
兼容新版本接口(inuse_objects和inuse_space的上报处理) (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
fibbery authored Sep 21, 2023
1 parent 63d40cb commit 34d1caf
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
59 changes: 49 additions & 10 deletions reporters/pyroscope_reporter/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
42 changes: 33 additions & 9 deletions reporters/pyroscope_reporter/pyroscope_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package pyroscope_reporter

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"mime/multipart"
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -150,20 +162,32 @@ 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 {
return err
}
return nil
}

func sampleTypeCfg(ptype string) map[string]*SampleType {
switch ptype {
case "heap":
return heapSampleTypes
case "goroutine":
return goroutineSampleTypes
}
return nil
}
10 changes: 5 additions & 5 deletions reporters/pyroscope_reporter/pyroscope_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/gin-gonic/gin"

"mosn.io/holmes"
)

Expand All @@ -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()
Expand All @@ -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.")
Expand Down

0 comments on commit 34d1caf

Please sign in to comment.