github.com/m3db/m3@v1.5.0/src/metrics/rules/rollup_target.go (about) 1 // Copyright (c) 2018 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 rules 22 23 import ( 24 "errors" 25 26 "github.com/m3db/m3/src/metrics/generated/proto/rulepb" 27 "github.com/m3db/m3/src/metrics/pipeline" 28 "github.com/m3db/m3/src/metrics/policy" 29 "github.com/m3db/m3/src/metrics/rules/view" 30 ) 31 32 var ( 33 emptyRollupTarget rollupTarget 34 35 errNilRollupTargetV1Proto = errors.New("nil rollup target v1 proto") 36 errNilRollupTargetV2Proto = errors.New("nil rollup target v2 proto") 37 ) 38 39 // rollupTarget dictates how to roll up metrics. Metrics associated with a rollup 40 // target will be rolled up as dictated by the operations in the pipeline, and stored 41 // under the provided storage policies. 42 type rollupTarget struct { 43 Pipeline pipeline.Pipeline 44 StoragePolicies policy.StoragePolicies 45 ResendEnabled bool 46 } 47 48 // newRollupTargetFromV1Proto creates a new rollup target from v1 proto 49 // for backward compatibility purposes. 50 func newRollupTargetFromV1Proto(pb *rulepb.RollupTarget) (rollupTarget, error) { 51 if pb == nil { 52 return emptyRollupTarget, errNilRollupTargetV1Proto 53 } 54 aggregationID, storagePolicies, err := toAggregationIDAndStoragePolicies(pb.Policies) 55 if err != nil { 56 return emptyRollupTarget, err 57 } 58 59 rollup, err := pipeline.NewRollupOp(pipeline.GroupByRollupType, pb.Name, 60 pb.Tags, aggregationID) 61 if err != nil { 62 return emptyRollupTarget, err 63 } 64 65 rollupOp := pipeline.OpUnion{ 66 Type: pipeline.RollupOpType, 67 Rollup: rollup, 68 } 69 pipeline := pipeline.NewPipeline([]pipeline.OpUnion{rollupOp}) 70 return rollupTarget{ 71 Pipeline: pipeline, 72 StoragePolicies: storagePolicies, 73 }, nil 74 } 75 76 // newRollupTargetFromProto creates a new rollup target from v2 proto. 77 func newRollupTargetFromV2Proto(pb *rulepb.RollupTargetV2) (rollupTarget, error) { 78 if pb == nil { 79 return emptyRollupTarget, errNilRollupTargetV2Proto 80 } 81 pipeline, err := pipeline.NewPipelineFromProto(pb.Pipeline) 82 if err != nil { 83 return emptyRollupTarget, err 84 } 85 storagePolicies, err := policy.NewStoragePoliciesFromProto(pb.StoragePolicies) 86 if err != nil { 87 return emptyRollupTarget, err 88 } 89 return rollupTarget{ 90 Pipeline: pipeline, 91 StoragePolicies: storagePolicies, 92 ResendEnabled: pb.ResendEnabled, 93 }, nil 94 } 95 96 func newRollupTargetFromView(rtv view.RollupTarget) rollupTarget { 97 return rollupTarget{ 98 Pipeline: rtv.Pipeline, 99 StoragePolicies: rtv.StoragePolicies, 100 ResendEnabled: rtv.ResendEnabled, 101 } 102 } 103 104 func (t rollupTarget) rollupTargetView() view.RollupTarget { 105 return view.RollupTarget{ 106 Pipeline: t.Pipeline, 107 StoragePolicies: t.StoragePolicies, 108 ResendEnabled: t.ResendEnabled, 109 } 110 } 111 112 // clone clones a rollup target. 113 func (t *rollupTarget) clone() rollupTarget { 114 return rollupTarget{ 115 Pipeline: t.Pipeline.Clone(), 116 StoragePolicies: t.StoragePolicies.Clone(), 117 ResendEnabled: t.ResendEnabled, 118 } 119 } 120 121 // proto returns the proto representation of a rollup target. 122 func (t *rollupTarget) proto() (*rulepb.RollupTargetV2, error) { 123 pipeline, err := t.Pipeline.Proto() 124 if err != nil { 125 return nil, err 126 } 127 storagePolicies, err := t.StoragePolicies.Proto() 128 if err != nil { 129 return nil, err 130 } 131 return &rulepb.RollupTargetV2{ 132 Pipeline: pipeline, 133 StoragePolicies: storagePolicies, 134 ResendEnabled: t.ResendEnabled, 135 }, nil 136 } 137 138 func newRollupTargetsFromView(targets []view.RollupTarget) []rollupTarget { 139 res := make([]rollupTarget, 0, len(targets)) 140 for _, t := range targets { 141 res = append(res, newRollupTargetFromView(t)) 142 } 143 return res 144 }