Skip to content

feat: cumulative pprof merge for pull mode #1794

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

Merged
merged 9 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/adhoc/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newPull(cfg *config.Adhoc, args []string, st *storage.Storage, logger *logr
}

p := parser.New(logger, st, e)
m := scrape.NewManager(logger, p, defaultMetricsRegistry)
m := scrape.NewManager(logger, p, defaultMetricsRegistry, true)
scrapeCfg := &(*scrapeconfig.DefaultConfig())
scrapeCfg.JobName = "adhoc"
scrapeCfg.EnabledProfiles = []string{"cpu", "mem"}
Expand Down
2 changes: 1 addition & 1 deletion pkg/adhoc/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func newPush(_ *config.Adhoc, args []string, st *storage.Storage, logger *logrus
p := parser.New(logger, st, e)
return push{
args: args,
handler: server.NewIngestHandler(logger, p, func(*ingestion.IngestInput) {}, httputils.NewDefaultHelper(logger)),
handler: server.NewIngestHandler(logger, p, func(*ingestion.IngestInput) {}, httputils.NewDefaultHelper(logger), true),
logger: logger,
}, nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ func newServerService(c *config.Server) (*serverService, error) {
svc.scrapeManager = scrape.NewManager(
svc.logger.WithField("component", "scrape-manager"),
ingester,
defaultMetricsRegistry)
defaultMetricsRegistry,
!svc.config.RemoteWrite.Enabled)

svc.controller, err = server.New(server.Config{
Configuration: svc.config,
Expand Down
57 changes: 53 additions & 4 deletions pkg/convert/pprof/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"github.com/pyroscope-io/pyroscope/pkg/util/cumulativepprof"
"io"
"mime/multipart"
"sync"
Expand All @@ -22,6 +24,8 @@ type RawProfile struct {
// References the next profile in the sequence (cumulative type only).
next *RawProfile

mergers *cumulativepprof.Mergers

m sync.Mutex
// Initializes lazily on Bytes, if not present.
RawData []byte // Represents raw request body as per ingestion API.
Expand Down Expand Up @@ -50,28 +54,73 @@ func (p *RawProfile) ContentType() string {
// two consecutive samples to calculate the diff. If parser is not
// present due to a failure, or sequence violation, the profiles will
// be re-parsed.
func (p *RawProfile) Push(profile []byte, cumulative bool) *RawProfile {
func (p *RawProfile) Push(profile []byte, cumulative, mergeCumulative bool) *RawProfile {
p.m.Lock()
p.Profile = profile
p.RawData = nil
n := &RawProfile{
SampleTypeConfig: p.SampleTypeConfig,
}
if cumulative {
n := &RawProfile{
SampleTypeConfig: p.SampleTypeConfig,
}
// N.B the parser state is only propagated
// after successful Parse call.
n.PreviousProfile = p.Profile
p.next = n
if mergeCumulative {
mergers := p.mergers
if mergers == nil {
mergers = cumulativepprof.NewMergers()
}
err := p.mergeCumulativeLocked(mergers)
if err == nil {
n.mergers = mergers
}
}
}
p.m.Unlock()
return p.next
}

func (p *RawProfile) MergeCumulative(ms *cumulativepprof.Mergers) error {
p.m.Lock()
defer p.m.Unlock()
return p.mergeCumulativeLocked(ms)
}

func (p *RawProfile) mergeCumulativeLocked(ms *cumulativepprof.Mergers) error {
if p.Profile == nil && p.PreviousProfile == nil && p.RawData != nil && p.FormDataContentType != "" {
err := p.loadPprofFromForm()
if err != nil {
return err
}
}
if p.PreviousProfile == nil {
return ErrCumulativeMergeNoPreviousProfile
}
merged, stConfig, err := ms.Merge(p.PreviousProfile, p.Profile, p.SampleTypeConfig)
if err != nil {
return err
}
var mergedProfileBytes bytes.Buffer
err = merged.Write(&mergedProfileBytes)
if err != nil {
return err
}
p.Profile = mergedProfileBytes.Bytes()
p.PreviousProfile = nil
p.SampleTypeConfig = stConfig
p.RawData = nil
Comment on lines +109 to +112
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note: I might be mistaken but this means we will deserialize pprof protobuf bytes again in parser down the data flow, although we already have merged: as far as I understand we can't use it directly because it is github.com/google/pprof/profile.Profile – which is very sad

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a different type.
Maybe we should do the new merge only for remote write case? and let the parser do the merge(diff)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should do the new merge only for remote write case? and let the parser do the merge(diff)

yeah this makes sense to me

return nil
}

const (
formFieldProfile, formFileProfile = "profile", "profile.pprof"
formFieldPreviousProfile, formFilePreviousProfile = "prev_profile", "profile.pprof"
formFieldSampleTypeConfig, formFileSampleTypeConfig = "sample_type_config", "sample_type_config.json"
)
var (
ErrCumulativeMergeNoPreviousProfile = errors.New("no previous profile for cumulative merge")
)

func (p *RawProfile) Bytes() ([]byte, error) {
p.m.Lock()
Expand Down
12 changes: 6 additions & 6 deletions pkg/scrape/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,33 @@ func DefaultConfig() *Config {
},
"mutex": {
Path: "/debug/pprof/mutex",
Params: nil,
Params: url.Values{
"seconds": []string{"10"},
},
SampleTypes: map[string]*profile.SampleTypeConfig{
"contentions": {
DisplayName: "mutex_count",
Units: metadata.LockSamplesUnits,
Cumulative: true,
},
"delay": {
DisplayName: "mutex_duration",
Units: metadata.LockNanosecondsUnits,
Cumulative: true,
},
},
},
"block": {
Path: "/debug/pprof/block",
Params: nil,
Params: url.Values{
"seconds": []string{"10"},
},
SampleTypes: map[string]*profile.SampleTypeConfig{
"contentions": {
DisplayName: "block_count",
Units: metadata.LockSamplesUnits,
Cumulative: true,
},
"delay": {
DisplayName: "block_duration",
Units: metadata.LockNanosecondsUnits,
Cumulative: true,
},
},
},
Expand Down
21 changes: 12 additions & 9 deletions pkg/scrape/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,22 @@ type Manager struct {
targetSets map[string][]*targetgroup.Group

reloadC chan struct{}

disableCumulativeMerge bool
}

// NewManager is the Manager constructor
func NewManager(logger logrus.FieldLogger, p ingestion.Ingester, r prometheus.Registerer) *Manager {
func NewManager(logger logrus.FieldLogger, p ingestion.Ingester, r prometheus.Registerer, disableCumulativeMerge bool) *Manager {
c := make(map[string]*config.Config)
return &Manager{
ingester: p,
logger: logger,
scrapeConfigs: c,
scrapePools: make(map[string]*scrapePool),
stop: make(chan struct{}),
reloadC: make(chan struct{}, 1),
metrics: newMetrics(r),
ingester: p,
logger: logger,
scrapeConfigs: c,
scrapePools: make(map[string]*scrapePool),
stop: make(chan struct{}),
reloadC: make(chan struct{}, 1),
metrics: newMetrics(r),
disableCumulativeMerge: disableCumulativeMerge,
}
}

Expand Down Expand Up @@ -90,7 +93,7 @@ func (m *Manager) reload() {
Errorf("reloading target set")
continue
}
sp, err := newScrapePool(scrapeConfig, m.ingester, m.logger, m.metrics)
sp, err := newScrapePool(scrapeConfig, m.ingester, m.logger, m.metrics, m.disableCumulativeMerge)
if err != nil {
m.logger.WithError(err).
WithField("scrape_pool", setName).
Expand Down
12 changes: 10 additions & 2 deletions pkg/scrape/scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ type scrapePool struct {
// set of hashes.
activeTargets map[uint64]*Target
droppedTargets []*Target

disableCumulativeMerge bool
}

func newScrapePool(cfg *config.Config, p ingestion.Ingester, logger logrus.FieldLogger, m *metrics) (*scrapePool, error) {
func newScrapePool(cfg *config.Config, p ingestion.Ingester, logger logrus.FieldLogger, m *metrics, disableCumulativeMerge bool) (*scrapePool, error) {
m.pools.Inc()
client, err := config.NewClientFromConfig(cfg.HTTPClientConfig, cfg.JobName)
if err != nil {
Expand All @@ -88,6 +90,7 @@ func newScrapePool(cfg *config.Config, p ingestion.Ingester, logger logrus.Field

metrics: m,
poolMetrics: m.poolMetrics(cfg.JobName),
disableCumulativeMerge: disableCumulativeMerge,
}

return &sp, nil
Expand All @@ -105,6 +108,8 @@ func (sp *scrapePool) newScrapeLoop(s *scraper, i, t time.Duration) *scrapeLoop
delta: d,
interval: i,
timeout: t,

disableCumulativeMerge: sp.disableCumulativeMerge,
}
x.ctx, x.cancel = context.WithCancel(sp.ctx)
return &x
Expand Down Expand Up @@ -324,6 +329,8 @@ type scrapeLoop struct {
delta time.Duration
interval time.Duration
timeout time.Duration

disableCumulativeMerge bool
}

func (sl *scrapeLoop) run() {
Expand Down Expand Up @@ -421,7 +428,8 @@ func (sl *scrapeLoop) scrape(startTime, endTime time.Time) error {
}

profile := sl.scraper.profile
sl.scraper.profile = profile.Push(buf.Bytes(), sl.scraper.cumulative)
sl.scraper.profile = profile.Push(buf.Bytes(), sl.scraper.cumulative, !sl.disableCumulativeMerge)

return sl.scraper.ingester.Ingest(ctx, &ingestion.IngestInput{
Profile: profile,
Metadata: ingestion.Metadata{
Expand Down
23 changes: 16 additions & 7 deletions pkg/server/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

"github.com/pyroscope-io/pyroscope/pkg/util/cumulativepprof"

"github.com/pyroscope-io/pyroscope/pkg/convert/speedscope"
"github.com/sirupsen/logrus"

Expand All @@ -28,22 +30,25 @@ type ingestHandler struct {
ingester ingestion.Ingester
onSuccess func(*ingestion.IngestInput)
httpUtils httputils.Utils

disableCumulativeMerge bool
}

func (ctrl *Controller) ingestHandler() http.Handler {
return NewIngestHandler(ctrl.log, ctrl.ingestser, func(pi *ingestion.IngestInput) {
ctrl.StatsInc("ingest")
ctrl.StatsInc("ingest:" + pi.Metadata.SpyName)
ctrl.appStats.Add(hashString(pi.Metadata.Key.AppName()))
}, ctrl.httpUtils)
}, ctrl.httpUtils, !ctrl.config.RemoteWrite.Enabled)
}

func NewIngestHandler(log *logrus.Logger, p ingestion.Ingester, onSuccess func(*ingestion.IngestInput), httpUtils httputils.Utils) http.Handler {
func NewIngestHandler(log *logrus.Logger, p ingestion.Ingester, onSuccess func(*ingestion.IngestInput), httpUtils httputils.Utils, disableCumulativeMerge bool) http.Handler {
return ingestHandler{
log: log,
ingester: p,
onSuccess: onSuccess,
httpUtils: httpUtils,
log: log,
ingester: p,
onSuccess: onSuccess,
httpUtils: httpUtils,
disableCumulativeMerge: disableCumulativeMerge,
}
}

Expand Down Expand Up @@ -159,10 +164,14 @@ func (h ingestHandler) ingestInputFromRequest(r *http.Request) (*ingestion.Inges
}

case strings.Contains(contentType, "multipart/form-data"):
input.Profile = &pprof.RawProfile{
p := &pprof.RawProfile{
FormDataContentType: contentType,
RawData: b,
}
if !h.disableCumulativeMerge {
p.MergeCumulative(cumulativepprof.NewMergers())
}
input.Profile = p
}

if input.Profile == nil {
Expand Down
Loading