github.com/m3db/m3@v1.5.0/src/x/instrument/sanitize.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 instrument 22 23 import ( 24 "fmt" 25 "strings" 26 27 "github.com/uber-go/tally" 28 "github.com/uber-go/tally/m3" 29 "github.com/uber-go/tally/prometheus" 30 ) 31 32 // MetricSanitizationType is a type of sanitizer to use for metrics. 33 type MetricSanitizationType int 34 35 const ( 36 // NoMetricSanitization performs no metric sanitization. 37 NoMetricSanitization MetricSanitizationType = iota 38 39 // M3MetricSanitization performs M3 metric sanitization. 40 M3MetricSanitization 41 42 // PrometheusMetricSanitization performs Prometheus metric sanitization. 43 PrometheusMetricSanitization 44 45 // defaultMetricSanitization is the default metric sanitization. 46 defaultMetricSanitization = NoMetricSanitization 47 ) 48 49 var ( 50 validMetricSanitizationTypes = []MetricSanitizationType{ 51 NoMetricSanitization, 52 M3MetricSanitization, 53 PrometheusMetricSanitization, 54 } 55 ) 56 57 func (t MetricSanitizationType) String() string { 58 switch t { 59 case NoMetricSanitization: 60 return "none" 61 case M3MetricSanitization: 62 return "m3" 63 case PrometheusMetricSanitization: 64 return "prometheus" 65 } 66 return "unknown" 67 } 68 69 // UnmarshalYAML unmarshals a MetricSanitizationType into a valid type from string. 70 func (t *MetricSanitizationType) UnmarshalYAML(unmarshal func(interface{}) error) error { 71 var str string 72 if err := unmarshal(&str); err != nil { 73 return err 74 } 75 if str == "" { 76 *t = defaultMetricSanitization 77 return nil 78 } 79 strs := make([]string, 0, len(validMetricSanitizationTypes)) 80 for _, valid := range validMetricSanitizationTypes { 81 if str == valid.String() { 82 *t = valid 83 return nil 84 } 85 strs = append(strs, "'"+valid.String()+"'") 86 } 87 return fmt.Errorf("invalid MetricSanitizationType '%s' valid types are: %s", 88 str, strings.Join(strs, ", ")) 89 } 90 91 // NewOptions returns a new set of sanitization options for the sanitization type. 92 func (t *MetricSanitizationType) NewOptions() *tally.SanitizeOptions { 93 switch *t { 94 case NoMetricSanitization: 95 return nil 96 case M3MetricSanitization: 97 return &m3.DefaultSanitizerOpts 98 case PrometheusMetricSanitization: 99 return &prometheus.DefaultSanitizerOpts 100 } 101 return nil 102 }