Skip to content

Commit

Permalink
implemented default NoOpProfiler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Linas Naginionis committed Feb 2, 2021
1 parent c8c2d5d commit a44993e
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 306 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,64 +73,58 @@ func (i *instrumentationContext) finish() {
i.span.Finish()
}

func (i *instrumentationContext) startCPUProfileIfEnabled(name string) {
if i.pOpts.Enabled() {
err := i.pOpts.Profiler().StartCPUProfile(name)
if err != nil {
i.log.Error("unable to start cpu profile", zap.Error(err))
}
func (i *instrumentationContext) startCPUProfile(name string) {
err := i.pOpts.Profiler().StartCPUProfile(name)
if err != nil {
i.log.Error("unable to start cpu profile", zap.Error(err))
}
}

func (i *instrumentationContext) stopCPUProfileIfEnabled() {
if i.pOpts.Enabled() {
if err := i.pOpts.Profiler().StopCPUProfile(); err != nil {
i.log.Error("unable to stop cpu profile", zap.Error(err))
}
func (i *instrumentationContext) stopCPUProfile() {
if err := i.pOpts.Profiler().StopCPUProfile(); err != nil {
i.log.Error("unable to stop cpu profile", zap.Error(err))
}
}

func (i *instrumentationContext) writeHeapProfileIfEnabled(name string) {
if i.pOpts.Enabled() {
err := i.pOpts.Profiler().WriteHeapProfile(name)
if err != nil {
i.log.Error("unable to write heap profile", zap.Error(err))
}
func (i *instrumentationContext) writeHeapProfile(name string) {
err := i.pOpts.Profiler().WriteHeapProfile(name)
if err != nil {
i.log.Error("unable to write heap profile", zap.Error(err))
}
}

func (i *instrumentationContext) bootstrapSnapshotsStarted() {
i.log.Info("read snapshots start")
i.span.LogFields(opentracinglog.String("event", "read_snapshots_start"))
i.start = i.nowFn()
i.startCPUProfileIfEnabled(snapshotsProfileName)
i.writeHeapProfileIfEnabled(snapshotsProfileName)
i.startCPUProfile(snapshotsProfileName)
i.writeHeapProfile(snapshotsProfileName)
}

func (i *instrumentationContext) bootstrapSnapshotsCompleted() {
duration := i.nowFn().Sub(i.start)
i.bootstrapSnapshotsDuration.Record(duration)
i.log.Info("read snapshots done", zap.Duration("took", duration))
i.span.LogFields(opentracinglog.String("event", "read_snapshots_done"))
i.stopCPUProfileIfEnabled()
i.writeHeapProfileIfEnabled(snapshotsProfileName)
i.stopCPUProfile()
i.writeHeapProfile(snapshotsProfileName)
}

func (i *instrumentationContext) readCommitLogStarted() {
i.log.Info("read commit log start")
i.span.LogFields(opentracinglog.String("event", "read_commitlog_start"))
i.start = i.nowFn()
i.startCPUProfileIfEnabled(readProfileName)
i.writeHeapProfileIfEnabled(readProfileName)
i.startCPUProfile(readProfileName)
i.writeHeapProfile(readProfileName)
}

func (i *instrumentationContext) readCommitLogCompleted() {
duration := i.nowFn().Sub(i.start)
i.bootstrapCommitLogDuration.Record(duration)
i.log.Info("read commit log done", zap.Duration("took", duration))
i.span.LogFields(opentracinglog.String("event", "read_commitlog_done"))
i.stopCPUProfileIfEnabled()
i.writeHeapProfileIfEnabled(readProfileName)
i.stopCPUProfile()
i.writeHeapProfile(readProfileName)
}

type instrumentation struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,32 @@ func (i *instrumentationContext) finish() {
i.span.Finish()
}

func (i *instrumentationContext) startCPUProfileIfEnabled(name string) {
if i.pOpts.Enabled() {
err := i.pOpts.Profiler().StartCPUProfile(name)
if err != nil {
i.log.Error("unable to start cpu profile", zap.Error(err))
}
func (i *instrumentationContext) startCPUProfile(name string) {
err := i.pOpts.Profiler().StartCPUProfile(name)
if err != nil {
i.log.Error("unable to start cpu profile", zap.Error(err))
}
}

func (i *instrumentationContext) stopCPUProfileIfEnabled() {
if i.pOpts.Enabled() {
if err := i.pOpts.Profiler().StopCPUProfile(); err != nil {
i.log.Error("unable to stop cpu profile", zap.Error(err))
}
func (i *instrumentationContext) stopCPUProfile() {
if err := i.pOpts.Profiler().StopCPUProfile(); err != nil {
i.log.Error("unable to stop cpu profile", zap.Error(err))
}
}

func (i *instrumentationContext) writeHeapProfileIfEnabled(name string) {
if i.pOpts.Enabled() {
err := i.pOpts.Profiler().WriteHeapProfile(name)
if err != nil {
i.log.Error("unable to write heap profile", zap.Error(err))
}
func (i *instrumentationContext) writeHeapProfile(name string) {
err := i.pOpts.Profiler().WriteHeapProfile(name)
if err != nil {
i.log.Error("unable to write heap profile", zap.Error(err))
}
}

func (i *instrumentationContext) bootstrapDataStarted() {
i.log.Info("bootstrapping time series data start", i.logFields...)
i.span.LogFields(opentracinglog.String("event", "bootstrap_data_start"))
i.start = i.nowFn()
i.startCPUProfileIfEnabled(dataProfile)
i.writeHeapProfileIfEnabled(dataProfile)
i.startCPUProfile(dataProfile)
i.writeHeapProfile(dataProfile)
}

func (i *instrumentationContext) bootstrapDataCompleted() {
Expand All @@ -119,25 +113,25 @@ func (i *instrumentationContext) bootstrapDataCompleted() {
i.log.Info("bootstrapping time series data success",
append(i.logFields, zap.Duration("took", duration))...)
i.span.LogFields(opentracinglog.String("event", "bootstrap_data_done"))
i.stopCPUProfileIfEnabled()
i.writeHeapProfileIfEnabled(dataProfile)
i.stopCPUProfile()
i.writeHeapProfile(dataProfile)
}

func (i *instrumentationContext) bootstrapIndexStarted() {
i.log.Info("bootstrapping index metadata start")
i.span.LogFields(opentracinglog.String("event", "bootstrap_index_start"))
i.start = i.nowFn()
i.startCPUProfileIfEnabled(indexProfile)
i.writeHeapProfileIfEnabled(indexProfile)
i.startCPUProfile(indexProfile)
i.writeHeapProfile(indexProfile)
}

func (i *instrumentationContext) bootstrapIndexCompleted() {
duration := i.nowFn().Sub(i.start)
i.bootstrapIndexDuration.Record(duration)
i.log.Info("bootstrapping index metadata success", zap.Duration("took", duration))
i.span.LogFields(opentracinglog.String("event", "bootstrap_index_done"))
i.stopCPUProfileIfEnabled()
i.writeHeapProfileIfEnabled(indexProfile)
i.stopCPUProfile()
i.writeHeapProfile(indexProfile)
}

type instrumentation struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,64 +71,58 @@ func (i *instrumentationContext) finish() {
i.span.Finish()
}

func (i *instrumentationContext) startCPUProfileIfEnabled(name string) {
if i.pOpts.Enabled() {
err := i.pOpts.Profiler().StartCPUProfile(name)
if err != nil {
i.log.Error("unable to start cpu profile", zap.Error(err))
}
func (i *instrumentationContext) startCPUProfile(name string) {
err := i.pOpts.Profiler().StartCPUProfile(name)
if err != nil {
i.log.Error("unable to start cpu profile", zap.Error(err))
}
}

func (i *instrumentationContext) stopCPUProfileIfEnabled() {
if i.pOpts.Enabled() {
if err := i.pOpts.Profiler().StopCPUProfile(); err != nil {
i.log.Error("unable to stop cpu profile", zap.Error(err))
}
func (i *instrumentationContext) stopCPUProfile() {
if err := i.pOpts.Profiler().StopCPUProfile(); err != nil {
i.log.Error("unable to stop cpu profile", zap.Error(err))
}
}

func (i *instrumentationContext) writeHeapProfileIfEnabled(name string) {
if i.pOpts.Enabled() {
err := i.pOpts.Profiler().WriteHeapProfile(name)
if err != nil {
i.log.Error("unable to write heap profile", zap.Error(err))
}
func (i *instrumentationContext) writeHeapProfile(name string) {
err := i.pOpts.Profiler().WriteHeapProfile(name)
if err != nil {
i.log.Error("unable to write heap profile", zap.Error(err))
}
}

func (i *instrumentationContext) bootstrapDataStarted() {
i.log.Info("bootstrapping time series data start")
i.span.LogFields(opentracinglog.String("event", "bootstrap_data_start"))
i.start = i.nowFn()
i.startCPUProfileIfEnabled(dataProfile)
i.writeHeapProfileIfEnabled(dataProfile)
i.startCPUProfile(dataProfile)
i.writeHeapProfile(dataProfile)
}

func (i *instrumentationContext) bootstrapDataCompleted() {
duration := i.nowFn().Sub(i.start)
i.bootstrapDataDuration.Record(duration)
i.log.Info("bootstrapping time series data success", zap.Duration("took", duration))
i.span.LogFields(opentracinglog.String("event", "bootstrap_data_done"))
i.stopCPUProfileIfEnabled()
i.writeHeapProfileIfEnabled(dataProfile)
i.stopCPUProfile()
i.writeHeapProfile(dataProfile)
}

func (i *instrumentationContext) bootstrapIndexStarted() {
i.log.Info("bootstrapping index metadata start")
i.span.LogFields(opentracinglog.String("event", "bootstrap_index_start"))
i.start = i.nowFn()
i.startCPUProfileIfEnabled(indexProfile)
i.writeHeapProfileIfEnabled(indexProfile)
i.startCPUProfile(indexProfile)
i.writeHeapProfile(indexProfile)
}

func (i *instrumentationContext) bootstrapIndexCompleted() {
duration := i.nowFn().Sub(i.start)
i.bootstrapIndexDuration.Record(duration)
i.log.Info("bootstrapping index metadata success", zap.Duration("took", duration))
i.span.LogFields(opentracinglog.String("event", "bootstrap_index_done"))
i.stopCPUProfileIfEnabled()
i.writeHeapProfileIfEnabled(indexProfile)
i.stopCPUProfile()
i.writeHeapProfile(indexProfile)
}

type instrumentationReadShardsContext struct {
Expand Down
22 changes: 2 additions & 20 deletions src/dbnode/storage/profiler/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,15 @@

package profiler

import "fmt"

type options struct {
enabled bool
profiler Profiler
}

// NewOptions creates new profiler options.
func NewOptions() Options {
return &options{}
}

func (o *options) Validate() error {
if o.enabled && o.profiler == nil {
return fmt.Errorf("profiler is enabled but is not set")
return &options{
profiler: NewNoOpProfiler(),
}
return nil
}

func (o *options) Enabled() bool {
return o.enabled
}

func (o *options) SetEnabled(value bool) Options {
opts := *o
opts.enabled = value
return &opts
}

func (o *options) Profiler() Profiler {
Expand Down
Loading

0 comments on commit a44993e

Please sign in to comment.