Skip to content

Commit

Permalink
profiler: Pass registry to constructor
Browse files Browse the repository at this point in the history
Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
  • Loading branch information
Sylfrena committed Feb 16, 2022
1 parent 54d8842 commit 0815626
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
4 changes: 2 additions & 2 deletions cmd/parca-agent/main.go
Expand Up @@ -158,7 +158,7 @@ func main() {
}

externalLabels := getExternalLabels(flags.ExternalLabel, flags.Node)
tm := target.NewManager(logger, externalLabels, ksymCache, listener, dc, flags.ProfilingDuration, flags.TempDir)
tm := target.NewManager(logger, reg, externalLabels, ksymCache, listener, dc, flags.ProfilingDuration, flags.TempDir)

mux.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{}))
mux.HandleFunc("/debug/pprof/", pprof.Index)
Expand Down Expand Up @@ -367,7 +367,7 @@ func main() {
ctx, cancel := context.WithCancel(ctx)
g.Add(func() error {
level.Debug(logger).Log("msg", "starting target manager")
return tm.Run(ctx, m.SyncCh(), reg)
return tm.Run(ctx, m.SyncCh())
}, func(error) {
cancel()
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/profiler/profiler.go
Expand Up @@ -59,6 +59,7 @@ const (

type CgroupProfiler struct {
logger log.Logger
missingStacks prometheus.Counter
ksymCache *ksym.KsymCache
target model.LabelSet
profilingDuration time.Duration
Expand All @@ -70,20 +71,19 @@ type CgroupProfiler struct {

mtx *sync.RWMutex
lastProfileTakenAt time.Time
missingStacks prometheus.Counter
lastError error

perfCache *perf.PerfCache
}

func NewCgroupProfiler(
logger log.Logger,
reg prometheus.Registerer,
ksymCache *ksym.KsymCache,
writeClient profilestorepb.ProfileStoreServiceClient,
debugInfoClient debuginfo.Client,
target model.LabelSet,
profilingDuration time.Duration,
reg prometheus.Registerer,
tmp string,
) *CgroupProfiler {
return &CgroupProfiler{
Expand Down
13 changes: 8 additions & 5 deletions pkg/target/manager.go
Expand Up @@ -32,6 +32,7 @@ type Manager struct {
mtx *sync.RWMutex
profilerPools map[string]*ProfilerPool
logger log.Logger
reg prometheus.Registerer
externalLabels model.LabelSet
ksymCache *ksym.KsymCache
writeClient profilestorepb.ProfileStoreServiceClient
Expand All @@ -42,6 +43,7 @@ type Manager struct {

func NewManager(
logger log.Logger,
reg prometheus.Registerer,
externalLabels model.LabelSet,
ksymCache *ksym.KsymCache,
writeClient profilestorepb.ProfileStoreServiceClient,
Expand All @@ -52,6 +54,7 @@ func NewManager(
mtx: &sync.RWMutex{},
profilerPools: map[string]*ProfilerPool{},
logger: logger,
reg: reg,
externalLabels: externalLabels,
ksymCache: ksymCache,
writeClient: writeClient,
Expand All @@ -63,21 +66,21 @@ func NewManager(
return m
}

func (m *Manager) Run(ctx context.Context, update <-chan map[string][]*Group, reg prometheus.Registerer) error {
func (m *Manager) Run(ctx context.Context, update <-chan map[string][]*Group) error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case targetSets := <-update:
err := m.reconcileTargets(ctx, targetSets, reg)
err := m.reconcileTargets(ctx, targetSets)
if err != nil {
return err
}
}
}
}

func (m *Manager) reconcileTargets(ctx context.Context, targetSets map[string][]*Group, reg prometheus.Registerer) error {
func (m *Manager) reconcileTargets(ctx context.Context, targetSets map[string][]*Group) error {
m.mtx.Lock()
defer m.mtx.Unlock()

Expand All @@ -87,11 +90,11 @@ func (m *Manager) reconcileTargets(ctx context.Context, targetSets map[string][]

pp, found := m.profilerPools[name]
if !found {
pp = NewProfilerPool(ctx, m.externalLabels, m.logger, m.ksymCache, m.writeClient, m.debugInfoClient, m.profilingDuration, m.tmp)
pp = NewProfilerPool(ctx, m.externalLabels, m.logger, m.reg, m.ksymCache, m.writeClient, m.debugInfoClient, m.profilingDuration, m.tmp)
m.profilerPools[name] = pp
}

pp.Sync(targetSet, reg)
pp.Sync(targetSet)
}
return nil
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/target/profiler_pool.go
Expand Up @@ -49,6 +49,7 @@ type ProfilerPool struct {
activeProfilers map[uint64]Profiler
externalLabels model.LabelSet
logger log.Logger
reg prometheus.Registerer
ksymCache *ksym.KsymCache
writeClient profilestorepb.ProfileStoreServiceClient
debugInfoClient debuginfo.Client
Expand All @@ -60,6 +61,7 @@ func NewProfilerPool(
ctx context.Context,
externalLabels model.LabelSet,
logger log.Logger,
reg prometheus.Registerer,
ksymCache *ksym.KsymCache,
writeClient profilestorepb.ProfileStoreServiceClient,
debugInfoClient debuginfo.Client,
Expand All @@ -72,6 +74,7 @@ func NewProfilerPool(
activeProfilers: map[uint64]Profiler{},
externalLabels: externalLabels,
logger: logger,
reg: reg,
ksymCache: ksymCache,
writeClient: writeClient,
debugInfoClient: debugInfoClient,
Expand All @@ -93,7 +96,7 @@ func (pp *ProfilerPool) Profilers() []Profiler {
return res
}

func (pp *ProfilerPool) Sync(tg []*Group, reg prometheus.Registerer) {
func (pp *ProfilerPool) Sync(tg []*Group) {
pp.mtx.Lock()
defer pp.mtx.Unlock()

Expand Down Expand Up @@ -128,12 +131,12 @@ func (pp *ProfilerPool) Sync(tg []*Group, reg prometheus.Registerer) {
if _, found := pp.activeTargets[h]; !found {
newProfiler := profiler.NewCgroupProfiler(
pp.logger,
pp.reg,
pp.ksymCache,
pp.writeClient,
pp.debugInfoClient,
newTarget.labelSet,
pp.profilingDuration,
reg,
pp.tmp,
)

Expand Down

0 comments on commit 0815626

Please sign in to comment.