github.com/m3db/m3@v1.5.0/src/metrics/aggregation/type_config.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 "fmt" 25 26 "github.com/m3db/m3/src/x/instrument" 27 "github.com/m3db/m3/src/x/pool" 28 ) 29 30 // TypesConfiguration contains configuration for aggregation types. 31 type TypesConfiguration struct { 32 // Default aggregation types for counter metrics. 33 DefaultCounterAggregationTypes *Types `yaml:"defaultCounterAggregationTypes"` 34 35 // Default aggregation types for timer metrics. 36 DefaultTimerAggregationTypes *Types `yaml:"defaultTimerAggregationTypes"` 37 38 // Default aggregation types for gauge metrics. 39 DefaultGaugeAggregationTypes *Types `yaml:"defaultGaugeAggregationTypes"` 40 41 // CounterTransformFnType configures the type string transformation function for counters. 42 CounterTransformFnType *TransformFnType `yaml:"counterTransformFnType"` 43 44 // TimerTransformFnType configures the type string transformation function for timers. 45 TimerTransformFnType *TransformFnType `yaml:"timerTransformFnType"` 46 47 // GaugeTransformFnType configures the type string transformation function for gauges. 48 GaugeTransformFnType *TransformFnType `yaml:"gaugeTransformFnType"` 49 50 // Pool of aggregation types. 51 AggregationTypesPool pool.ObjectPoolConfiguration `yaml:"aggregationTypesPool"` 52 53 // Pool of quantile slices. 54 QuantilesPool pool.BucketizedPoolConfiguration `yaml:"quantilesPool"` 55 } 56 57 // NewOptions creates a new Option. 58 func (c TypesConfiguration) NewOptions(instrumentOpts instrument.Options) (TypesOptions, error) { 59 opts := NewTypesOptions() 60 if c.DefaultCounterAggregationTypes != nil { 61 opts = opts.SetDefaultCounterAggregationTypes(*c.DefaultCounterAggregationTypes) 62 } 63 if c.DefaultGaugeAggregationTypes != nil { 64 opts = opts.SetDefaultGaugeAggregationTypes(*c.DefaultGaugeAggregationTypes) 65 } 66 if c.DefaultTimerAggregationTypes != nil { 67 opts = opts.SetDefaultTimerAggregationTypes(*c.DefaultTimerAggregationTypes) 68 } 69 if c.CounterTransformFnType != nil { 70 fn, err := c.CounterTransformFnType.TransformFn() 71 if err != nil { 72 return nil, err 73 } 74 opts = opts.SetCounterTypeStringTransformFn(fn) 75 } 76 if c.TimerTransformFnType != nil { 77 fn, err := c.TimerTransformFnType.TransformFn() 78 if err != nil { 79 return nil, err 80 } 81 opts = opts.SetTimerTypeStringTransformFn(fn) 82 } 83 if c.GaugeTransformFnType != nil { 84 fn, err := c.GaugeTransformFnType.TransformFn() 85 if err != nil { 86 return nil, err 87 } 88 opts = opts.SetGaugeTypeStringTransformFn(fn) 89 } 90 91 // Set aggregation types pool. 92 scope := instrumentOpts.MetricsScope() 93 iOpts := instrumentOpts.SetMetricsScope(scope.SubScope("aggregation-types-pool")) 94 aggTypesPoolOpts := c.AggregationTypesPool.NewObjectPoolOptions(iOpts) 95 aggTypesPool := NewTypesPool(aggTypesPoolOpts) 96 opts = opts.SetTypesPool(aggTypesPool) 97 aggTypesPool.Init(func() Types { 98 return make(Types, 0, len(ValidTypes)) 99 }) 100 101 // Set quantiles pool. 102 iOpts = instrumentOpts.SetMetricsScope(scope.SubScope("quantile-pool")) 103 quantilesPool := pool.NewFloatsPool( 104 c.QuantilesPool.NewBuckets(), 105 c.QuantilesPool.NewObjectPoolOptions(iOpts), 106 ) 107 opts = opts.SetQuantilesPool(quantilesPool) 108 quantilesPool.Init() 109 110 return opts, nil 111 } 112 113 // TransformFnType specifies the type of the aggregation 114 // transform function. 115 type TransformFnType string 116 117 var ( 118 // NoopTransformType is the type for noop transform function. 119 NoopTransformType TransformFnType = "noop" 120 // EmptyTransformType is the type for an empty transform function. 121 EmptyTransformType TransformFnType = "empty" 122 // SuffixTransformType is the type for suffix transform function. 123 SuffixTransformType TransformFnType = "suffix" 124 125 validTypes = []TransformFnType{ 126 NoopTransformType, 127 EmptyTransformType, 128 SuffixTransformType, 129 } 130 ) 131 132 // UnmarshalYAML uses the unmarshal function provided as an argument to set 133 // the current TransformFnType. 134 func (t *TransformFnType) UnmarshalYAML(unmarshal func(interface{}) error) error { 135 var str string 136 if err := unmarshal(&str); err != nil { 137 return err 138 } 139 var validStrings []string 140 for _, validType := range validTypes { 141 validString := string(validType) 142 if validString == str { 143 *t = validType 144 return nil 145 } 146 validStrings = append(validStrings, validString) 147 } 148 149 return fmt.Errorf("invalid transform type %s, valid types are: %v", str, validStrings) 150 } 151 152 // TransformFn returns the transform function. 153 func (t TransformFnType) TransformFn() (TypeStringTransformFn, error) { 154 switch t { 155 case NoopTransformType: 156 return NoOpTransform, nil 157 case EmptyTransformType: 158 return EmptyTransform, nil 159 case SuffixTransformType: 160 return SuffixTransform, nil 161 default: 162 return nil, fmt.Errorf("invalid type string transform function type: %s", string(t)) 163 } 164 }