github.com/m3db/m3@v1.5.0/src/dbnode/storage/bootstrap/bootstrapper/uninitialized/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 uninitialized
    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  )
    35  
    36  type instrumentationContext struct {
    37  	nowFn                 clock.NowFn
    38  	log                   *zap.Logger
    39  	start                 time.Time
    40  	span                  opentracing.Span
    41  	bootstrapReadDuration tally.Timer
    42  }
    43  
    44  func newInstrumentationContext(
    45  	nowFn clock.NowFn,
    46  	log *zap.Logger,
    47  	span opentracing.Span,
    48  	scope tally.Scope,
    49  ) *instrumentationContext {
    50  	ctx := &instrumentationContext{
    51  		nowFn:                 nowFn,
    52  		log:                   log,
    53  		span:                  span,
    54  		bootstrapReadDuration: scope.Timer("read-duration"),
    55  	}
    56  	ctx.readStarted()
    57  	return ctx
    58  }
    59  
    60  func (i *instrumentationContext) finish() {
    61  	i.readCompleted()
    62  	i.span.Finish()
    63  }
    64  
    65  func (i *instrumentationContext) readStarted() {
    66  	i.log.Info("read uninitialized start")
    67  	i.span.LogFields(opentracinglog.String("event", "read_uninitialized_start"))
    68  	i.start = i.nowFn()
    69  }
    70  
    71  func (i *instrumentationContext) readCompleted() {
    72  	duration := i.nowFn().Sub(i.start)
    73  	i.bootstrapReadDuration.Record(duration)
    74  	i.log.Info("read uninitialized done", zap.Duration("took", duration))
    75  	i.span.LogFields(opentracinglog.String("event", "read_uninitialized_done"))
    76  }
    77  
    78  type instrumentation struct {
    79  	scope tally.Scope
    80  	log   *zap.Logger
    81  	nowFn clock.NowFn
    82  }
    83  
    84  func newInstrumentation(opts Options) *instrumentation {
    85  	var (
    86  		scope = opts.InstrumentOptions().MetricsScope().SubScope("uninitialized-bootstrapper")
    87  		iopts = opts.InstrumentOptions().SetMetricsScope(scope)
    88  	)
    89  	opts = opts.SetInstrumentOptions(iopts)
    90  
    91  	return &instrumentation{
    92  		scope: scope,
    93  		log:   iopts.Logger().With(zap.String("bootstrapper", "uninitialized")),
    94  		nowFn: opts.ResultOptions().ClockOptions().NowFn(),
    95  	}
    96  }
    97  
    98  func (i *instrumentation) uninitializedBootstrapperSourceReadStarted(
    99  	ctx context.Context,
   100  ) *instrumentationContext {
   101  	_, span, _ := ctx.StartSampledTraceSpan(tracepoint.BootstrapperUninitializedSourceRead)
   102  	return newInstrumentationContext(
   103  		i.nowFn,
   104  		i.log,
   105  		span,
   106  		i.scope,
   107  	)
   108  }