github.com/m3db/m3@v1.5.0/src/aggregator/aggregation/options.go (about)

     1  // Copyright (c) 2017 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 aggregation
    22  
    23  import (
    24  	"github.com/m3db/m3/src/metrics/aggregation"
    25  	"github.com/m3db/m3/src/x/instrument"
    26  
    27  	"github.com/uber-go/tally"
    28  )
    29  
    30  var (
    31  	defaultHasExpensiveAggregations = false
    32  )
    33  
    34  // Options is the options for aggregations.
    35  type Options struct {
    36  	// Metrics is as set of aggregation metrics.
    37  	Metrics Metrics
    38  	// HasExpensiveAggregations means expensive (multiplication/division)
    39  	// aggregation types are enabled.
    40  	HasExpensiveAggregations bool
    41  }
    42  
    43  // Metrics is a set of metrics that can be used by elements.
    44  type Metrics struct {
    45  	Counter CounterMetrics
    46  	Gauge   GaugeMetrics
    47  }
    48  
    49  // CounterMetrics is a set of counter metrics can be used by all counters.
    50  type CounterMetrics struct {
    51  	valuesOutOfOrder tally.Counter
    52  }
    53  
    54  // GaugeMetrics is a set of gauge metrics can be used by all gauges.
    55  type GaugeMetrics struct {
    56  	valuesOutOfOrder tally.Counter
    57  }
    58  
    59  // NewMetrics is a set of aggregation metrics.
    60  func NewMetrics(scope tally.Scope) Metrics {
    61  	scope = scope.SubScope("aggregation")
    62  	return Metrics{
    63  		Counter: newCounterMetrics(scope.SubScope("counters")),
    64  		Gauge:   newGaugeMetrics(scope.SubScope("gauges")),
    65  	}
    66  }
    67  
    68  func newCounterMetrics(scope tally.Scope) CounterMetrics {
    69  	return CounterMetrics{
    70  		valuesOutOfOrder: scope.Counter("values-out-of-order"),
    71  	}
    72  }
    73  
    74  // IncValuesOutOfOrder increments value or if not initialized is a no-op.
    75  func (m CounterMetrics) IncValuesOutOfOrder() {
    76  	if m.valuesOutOfOrder != nil {
    77  		m.valuesOutOfOrder.Inc(1)
    78  	}
    79  }
    80  
    81  func newGaugeMetrics(scope tally.Scope) GaugeMetrics {
    82  	return GaugeMetrics{
    83  		valuesOutOfOrder: scope.Counter("values-out-of-order"),
    84  	}
    85  }
    86  
    87  // IncValuesOutOfOrder increments value or if not initialized is a no-op.
    88  func (m GaugeMetrics) IncValuesOutOfOrder() {
    89  	if m.valuesOutOfOrder != nil {
    90  		m.valuesOutOfOrder.Inc(1)
    91  	}
    92  }
    93  
    94  // NewOptions creates a new aggregation options.
    95  func NewOptions(instrumentOpts instrument.Options) Options {
    96  	return Options{
    97  		HasExpensiveAggregations: defaultHasExpensiveAggregations,
    98  		Metrics:                  NewMetrics(instrumentOpts.MetricsScope()),
    99  	}
   100  }
   101  
   102  // ResetSetData resets the aggregation options.
   103  func (o *Options) ResetSetData(aggTypes aggregation.Types) {
   104  	o.HasExpensiveAggregations = isExpensive(aggTypes)
   105  }