github.com/m3db/m3@v1.5.0/src/dbnode/storage/bootstrap/bootstrapper/commitlog/source_instrumentation.go (about)

     1  // Copyright (c) 2021 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package commitlog
    22  
    23  import (
    24  	"time"
    25  
    26  	"github.com/opentracing/opentracing-go"
    27  	opentracinglog "github.com/opentracing/opentracing-go/log"
    28  	"github.com/uber-go/tally"
    29  	"go.uber.org/zap"
    30  
    31  	"github.com/m3db/m3/src/dbnode/tracepoint"
    32  	"github.com/m3db/m3/src/x/clock"
    33  	"github.com/m3db/m3/src/x/context"
    34  	"github.com/m3db/m3/src/x/instrument"
    35  )
    36  
    37  type instrumentationContext struct {
    38  	opts                       Options
    39  	nowFn                      clock.NowFn
    40  	log                        *zap.Logger
    41  	start                      time.Time
    42  	span                       opentracing.Span
    43  	bootstrapSnapshotsDuration tally.Timer
    44  	bootstrapCommitLogDuration tally.Timer
    45  	profiler                   instrument.Profiler
    46  }
    47  
    48  func newInstrumentationContext(
    49  	opts Options,
    50  	nowFn clock.NowFn,
    51  	log *zap.Logger,
    52  	span opentracing.Span,
    53  	scope tally.Scope,
    54  	profiler instrument.Profiler,
    55  ) *instrumentationContext {
    56  	return &instrumentationContext{
    57  		opts:                       opts,
    58  		nowFn:                      nowFn,
    59  		log:                        log,
    60  		span:                       span,
    61  		profiler:                   profiler,
    62  		bootstrapSnapshotsDuration: scope.Timer("snapshots-duration"),
    63  		bootstrapCommitLogDuration: scope.Timer("commitlog-duration"),
    64  	}
    65  }
    66  
    67  const (
    68  	snapshotsProfileName = "commitlog-snapshots"
    69  	readProfileName      = "commitlog-read"
    70  )
    71  
    72  func (i *instrumentationContext) finish() {
    73  	i.span.Finish()
    74  }
    75  
    76  func (i *instrumentationContext) startCPUProfile(name string) {
    77  	err := i.profiler.StartCPUProfile(name)
    78  	if err != nil {
    79  		i.log.Error("unable to start cpu profile", zap.Error(err))
    80  	}
    81  }
    82  
    83  func (i *instrumentationContext) stopCPUProfile() {
    84  	if err := i.profiler.StopCPUProfile(); err != nil {
    85  		i.log.Error("unable to stop cpu profile", zap.Error(err))
    86  	}
    87  }
    88  
    89  func (i *instrumentationContext) writeHeapProfile(name string) {
    90  	err := i.profiler.WriteHeapProfile(name)
    91  	if err != nil {
    92  		i.log.Error("unable to write heap profile", zap.Error(err))
    93  	}
    94  }
    95  
    96  func (i *instrumentationContext) bootstrapSnapshotsStarted() {
    97  	i.log.Info("read snapshots start")
    98  	i.span.LogFields(opentracinglog.String("event", "read_snapshots_start"))
    99  	i.start = i.nowFn()
   100  	i.startCPUProfile(snapshotsProfileName)
   101  	i.writeHeapProfile(snapshotsProfileName)
   102  }
   103  
   104  func (i *instrumentationContext) bootstrapSnapshotsCompleted() {
   105  	duration := i.nowFn().Sub(i.start)
   106  	i.bootstrapSnapshotsDuration.Record(duration)
   107  	i.log.Info("read snapshots done", zap.Duration("took", duration))
   108  	i.span.LogFields(opentracinglog.String("event", "read_snapshots_done"))
   109  	i.stopCPUProfile()
   110  	i.writeHeapProfile(snapshotsProfileName)
   111  }
   112  
   113  func (i *instrumentationContext) readCommitLogStarted() {
   114  	i.log.Info("read commit log start")
   115  	i.span.LogFields(opentracinglog.String("event", "read_commitlog_start"))
   116  	i.start = i.nowFn()
   117  	i.startCPUProfile(readProfileName)
   118  	i.writeHeapProfile(readProfileName)
   119  }
   120  
   121  func (i *instrumentationContext) readCommitLogCompleted() {
   122  	duration := i.nowFn().Sub(i.start)
   123  	i.bootstrapCommitLogDuration.Record(duration)
   124  	i.log.Info("read commit log done", zap.Duration("took", duration))
   125  	i.span.LogFields(opentracinglog.String("event", "read_commitlog_done"))
   126  	i.stopCPUProfile()
   127  	i.writeHeapProfile(readProfileName)
   128  }
   129  
   130  type instrumentation struct {
   131  	opts     Options
   132  	profiler instrument.Profiler
   133  	scope    tally.Scope
   134  	log      *zap.Logger
   135  	nowFn    clock.NowFn
   136  }
   137  
   138  func newInstrumentation(opts Options, scope tally.Scope, log *zap.Logger) *instrumentation {
   139  	return &instrumentation{
   140  		opts:     opts,
   141  		profiler: opts.ResultOptions().InstrumentOptions().Profiler(),
   142  		scope:    scope,
   143  		log:      log,
   144  		nowFn:    opts.ResultOptions().ClockOptions().NowFn(),
   145  	}
   146  }
   147  
   148  func (i *instrumentation) commitLogBootstrapperSourceReadStarted(
   149  	ctx context.Context,
   150  ) *instrumentationContext {
   151  	_, span, _ := ctx.StartSampledTraceSpan(tracepoint.BootstrapperCommitLogSourceRead)
   152  	return newInstrumentationContext(
   153  		i.opts,
   154  		i.nowFn,
   155  		i.log,
   156  		span,
   157  		i.scope,
   158  		i.profiler,
   159  	)
   160  }