github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/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 // MarshalYAML marshals a MetricSanitizationType. 70 func (t *MetricSanitizationType) MarshalYAML() (interface{}, error) { 71 return t.String(), nil 72 } 73 74 // UnmarshalYAML unmarshals a MetricSanitizationType into a valid type from string. 75 func (t *MetricSanitizationType) UnmarshalYAML(unmarshal func(interface{}) error) error { 76 var str string 77 if err := unmarshal(&str); err != nil { 78 return err 79 } 80 if str == "" { 81 *t = defaultMetricSanitization 82 return nil 83 } 84 strs := make([]string, 0, len(validMetricSanitizationTypes)) 85 for _, valid := range validMetricSanitizationTypes { 86 if str == valid.String() { 87 *t = valid 88 return nil 89 } 90 strs = append(strs, "'"+valid.String()+"'") 91 } 92 return fmt.Errorf("invalid MetricSanitizationType '%s' valid types are: %s", 93 str, strings.Join(strs, ", ")) 94 } 95 96 // NewOptions returns a new set of sanitization options for the sanitization type. 97 func (t *MetricSanitizationType) NewOptions() *tally.SanitizeOptions { 98 switch *t { 99 case NoMetricSanitization: 100 return nil 101 case M3MetricSanitization: 102 return &m3.DefaultSanitizerOpts 103 case PrometheusMetricSanitization: 104 return &prometheus.DefaultSanitizerOpts 105 } 106 return nil 107 }