github.com/m3db/m3@v1.5.0/src/x/instrument/options.go (about)

     1  // Copyright (c) 2016 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 instrument
    22  
    23  import (
    24  	"os"
    25  	"time"
    26  
    27  	"github.com/opentracing/opentracing-go"
    28  	"github.com/uber-go/tally"
    29  	"go.uber.org/zap"
    30  	"go.uber.org/zap/zapcore"
    31  )
    32  
    33  const (
    34  	defaultSamplingRate      = 1.0
    35  	defaultReportingInterval = time.Second
    36  )
    37  
    38  var defaultProfiler = NewNoOpProfiler()
    39  
    40  type options struct {
    41  	zap             *zap.Logger
    42  	scope           tally.Scope
    43  	tracer          opentracing.Tracer
    44  	samplingRate    float64
    45  	timerOptions    TimerOptions
    46  	reportInterval  time.Duration
    47  	customBuildTags map[string]string
    48  	profiler        Profiler
    49  }
    50  
    51  // NewOptions creates new instrument options.
    52  func NewOptions() Options {
    53  	zapCore := zapcore.NewCore(
    54  		zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), os.Stdout, zap.InfoLevel)
    55  	zapLogger := zap.New(zapCore)
    56  	return &options{
    57  		zap:             zapLogger,
    58  		scope:           tally.NoopScope,
    59  		samplingRate:    defaultSamplingRate,
    60  		reportInterval:  defaultReportingInterval,
    61  		customBuildTags: map[string]string{},
    62  		profiler:        defaultProfiler,
    63  	}
    64  }
    65  
    66  func (o *options) SetLogger(value *zap.Logger) Options {
    67  	opts := *o
    68  	opts.zap = value
    69  	return &opts
    70  }
    71  
    72  func (o *options) Logger() *zap.Logger {
    73  	return o.zap
    74  }
    75  
    76  func (o *options) SetMetricsScope(value tally.Scope) Options {
    77  	opts := *o
    78  	opts.scope = value
    79  	return &opts
    80  }
    81  
    82  func (o *options) MetricsScope() tally.Scope {
    83  	return o.scope
    84  }
    85  
    86  func (o *options) Tracer() opentracing.Tracer {
    87  	return o.tracer
    88  }
    89  
    90  func (o *options) SetTracer(tracer opentracing.Tracer) Options {
    91  	opts := *o
    92  	opts.tracer = tracer
    93  	return &opts
    94  }
    95  
    96  func (o *options) SetTimerOptions(value TimerOptions) Options {
    97  	opts := *o
    98  	opts.timerOptions = value
    99  	return &opts
   100  }
   101  
   102  func (o *options) TimerOptions() TimerOptions {
   103  	return o.timerOptions
   104  }
   105  
   106  func (o *options) SetReportInterval(value time.Duration) Options {
   107  	opts := *o
   108  	opts.reportInterval = value
   109  	return &opts
   110  }
   111  
   112  func (o *options) ReportInterval() time.Duration {
   113  	return o.reportInterval
   114  }
   115  
   116  func (o *options) SetCustomBuildTags(tags map[string]string) Options {
   117  	opts := *o
   118  	opts.customBuildTags = tags
   119  	return &opts
   120  }
   121  
   122  func (o *options) CustomBuildTags() map[string]string {
   123  	return o.customBuildTags
   124  }
   125  
   126  func (o *options) SetProfiler(value Profiler) Options {
   127  	opts := *o
   128  	opts.profiler = value
   129  
   130  	return &opts
   131  }
   132  
   133  func (o *options) Profiler() Profiler {
   134  	return o.profiler
   135  }