github.com/google/cloudprober@v0.11.3/surfacers/common/options/options.go (about) 1 // Copyright 2021 The Cloudprober Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package options defines data structure for common surfacer options. 16 package options 17 18 import ( 19 "fmt" 20 "regexp" 21 22 "github.com/google/cloudprober/logger" 23 "github.com/google/cloudprober/metrics" 24 surfacerpb "github.com/google/cloudprober/surfacers/proto" 25 ) 26 27 type labelFilter struct { 28 key string 29 value string 30 } 31 32 func (lf *labelFilter) matchEventMetrics(em *metrics.EventMetrics) bool { 33 if lf.key != "" { 34 for _, lKey := range em.LabelsKeys() { 35 if lf.key != lKey { 36 continue 37 } 38 if lf.value == "" { 39 return true 40 } 41 return lf.value == em.Label(lKey) 42 } 43 } 44 return false 45 } 46 47 func parseMetricsFilter(configs []*surfacerpb.LabelFilter) ([]*labelFilter, error) { 48 var filters []*labelFilter 49 50 for _, c := range configs { 51 lf := &labelFilter{ 52 key: c.GetKey(), 53 value: c.GetValue(), 54 } 55 56 if lf.value != "" && lf.key == "" { 57 return nil, fmt.Errorf("key is required to match against val (%s)", c.GetValue()) 58 } 59 60 filters = append(filters, lf) 61 } 62 63 return filters, nil 64 } 65 66 // Options encapsulates surfacer options common to all surfacers. 67 type Options struct { 68 MetricsBufferSize int 69 Config *surfacerpb.SurfacerDef 70 Logger *logger.Logger 71 72 allowLabelFilters []*labelFilter 73 ignoreLabelFilters []*labelFilter 74 allowMetricName *regexp.Regexp 75 ignoreMetricName *regexp.Regexp 76 77 AddFailureMetric bool 78 } 79 80 // AllowEventMetrics returns whether a certain EventMetrics should be allowed 81 // or not. 82 // TODO(manugarg): Explore if we can either log or increment some metric when 83 // we ignore an EventMetrics. 84 func (opts *Options) AllowEventMetrics(em *metrics.EventMetrics) bool { 85 if opts == nil { 86 return true 87 } 88 89 // If we match any ignore filter, return false immediately. 90 for _, ignoreF := range opts.ignoreLabelFilters { 91 if ignoreF.matchEventMetrics(em) { 92 return false 93 } 94 } 95 96 // If no allow filters are given, allow everything. 97 if len(opts.allowLabelFilters) == 0 { 98 return true 99 } 100 101 // If allow filters are given, allow only if match them. 102 for _, allowF := range opts.allowLabelFilters { 103 if allowF.matchEventMetrics(em) { 104 return true 105 } 106 } 107 return false 108 } 109 110 // AllowMetric returns whether a certain Metric should be allowed or not. 111 func (opts *Options) AllowMetric(metricName string) bool { 112 if opts == nil { 113 return true 114 } 115 116 if opts.ignoreMetricName != nil && opts.ignoreMetricName.MatchString(metricName) { 117 return false 118 } 119 120 if opts.allowMetricName == nil { 121 return true 122 } 123 124 return opts.allowMetricName.MatchString(metricName) 125 } 126 127 // BuildOptionsFromConfig builds surfacer options using config. 128 func BuildOptionsFromConfig(sdef *surfacerpb.SurfacerDef, l *logger.Logger) (*Options, error) { 129 opts := &Options{ 130 Config: sdef, 131 Logger: l, 132 MetricsBufferSize: int(sdef.GetMetricsBufferSize()), 133 } 134 135 var err error 136 opts.allowLabelFilters, err = parseMetricsFilter(sdef.GetAllowMetricsWithLabel()) 137 if err != nil { 138 return nil, err 139 } 140 141 opts.ignoreLabelFilters, err = parseMetricsFilter(sdef.GetIgnoreMetricsWithLabel()) 142 if err != nil { 143 return nil, err 144 } 145 146 if sdef.GetAllowMetricsWithName() != "" { 147 opts.allowMetricName, err = regexp.Compile(sdef.GetAllowMetricsWithName()) 148 if err != nil { 149 return nil, err 150 } 151 } 152 153 if sdef.GetIgnoreMetricsWithName() != "" { 154 opts.ignoreMetricName, err = regexp.Compile(sdef.GetIgnoreMetricsWithName()) 155 if err != nil { 156 return nil, err 157 } 158 } 159 160 opts.AddFailureMetric = opts.Config.GetAddFailureMetric() 161 defaultFailureMetric := map[surfacerpb.Type]bool{ 162 surfacerpb.Type_STACKDRIVER: true, 163 surfacerpb.Type_CLOUDWATCH: true, 164 } 165 if opts.Config.AddFailureMetric == nil && defaultFailureMetric[opts.Config.GetType()] { 166 opts.AddFailureMetric = true 167 } 168 169 return opts, nil 170 }