github.com/m3db/m3@v1.5.0/src/metrics/metric/types.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 metric 22 23 import ( 24 "fmt" 25 "strings" 26 27 "github.com/m3db/m3/src/metrics/generated/proto/metricpb" 28 ) 29 30 // Type is a metric type. 31 type Type int 32 33 // A list of supported metric types. 34 const ( 35 UnknownType Type = iota 36 CounterType 37 TimerType 38 GaugeType 39 ) 40 41 // ValidTypes is a list of valid metric types. 42 var ValidTypes = []Type{ 43 CounterType, 44 TimerType, 45 GaugeType, 46 } 47 48 var ( 49 M3CounterValue = []byte("counter") 50 M3GaugeValue = []byte("gauge") 51 M3TimerValue = []byte("timer") 52 53 PromUnknownValue = []byte("unknown") 54 PromCounterValue = []byte("counter") 55 PromGaugeValue = []byte("gauge") 56 PromHistogramValue = []byte("histogram") 57 PromGaugeHistogramValue = []byte("gauge_histogram") 58 PromSummaryValue = []byte("summary") 59 PromInfoValue = []byte("info") 60 PromStateSetValue = []byte("state_set") 61 PromQuantileName = []byte("quantile") 62 63 M3MetricsPrefix = []byte("__m3") 64 M3MetricsPrefixString = string(M3MetricsPrefix) 65 66 M3TypeTag = []byte(M3MetricsPrefixString + "_type__") 67 M3MetricsGraphiteAggregation = []byte(M3MetricsPrefixString + "_graphite_aggregation__") 68 M3MetricsGraphitePrefix = []byte(M3MetricsPrefixString + "_graphite_prefix__") 69 M3MetricsDropTimestamp = []byte(M3MetricsPrefixString + "_drop_timestamp__") 70 M3PromTypeTag = []byte(M3MetricsPrefixString + "_prom_type__") 71 M3MetricsPromSummary = []byte(M3MetricsPrefixString + "_prom_summary__") 72 ) 73 74 func (t Type) String() string { 75 switch t { 76 case UnknownType: 77 return "unknown" 78 case CounterType: 79 return "counter" 80 case TimerType: 81 return "timer" 82 case GaugeType: 83 return "gauge" 84 default: 85 return fmt.Sprintf("unknown type: %d", t) 86 } 87 } 88 89 // ToProto converts the metric type to a protobuf message in place. 90 func (t Type) ToProto(pb *metricpb.MetricType) error { 91 switch t { 92 case UnknownType: 93 *pb = metricpb.MetricType_UNKNOWN 94 case CounterType: 95 *pb = metricpb.MetricType_COUNTER 96 case TimerType: 97 *pb = metricpb.MetricType_TIMER 98 case GaugeType: 99 *pb = metricpb.MetricType_GAUGE 100 default: 101 return fmt.Errorf("unknown metric type: %v", t) 102 } 103 return nil 104 } 105 106 // FromProto converts the protobuf message to a metric type. 107 func (t *Type) FromProto(pb metricpb.MetricType) error { 108 switch pb { 109 case metricpb.MetricType_UNKNOWN: 110 *t = UnknownType 111 case metricpb.MetricType_COUNTER: 112 *t = CounterType 113 case metricpb.MetricType_TIMER: 114 *t = TimerType 115 case metricpb.MetricType_GAUGE: 116 *t = GaugeType 117 default: 118 return fmt.Errorf("unknown metric type in proto: %v", pb) 119 } 120 return nil 121 } 122 123 // ParseType parses a type string and returns the type. 124 func ParseType(typeStr string) (Type, error) { 125 validTypeStrs := make([]string, 0, len(ValidTypes)) 126 for _, valid := range ValidTypes { 127 if typeStr == valid.String() { 128 return valid, nil 129 } 130 validTypeStrs = append(validTypeStrs, valid.String()) 131 } 132 return UnknownType, fmt.Errorf("invalid metric type '%s', valid types are: %s", 133 typeStr, strings.Join(validTypeStrs, ", ")) 134 } 135 136 // MustParseType parses a type string and panics if the input in invalid. 137 func MustParseType(typeStr string) Type { 138 t, err := ParseType(typeStr) 139 if err != nil { 140 panic(err.Error()) 141 } 142 return t 143 } 144 145 // UnmarshalYAML unmarshals YAML object into a metric type. 146 func (t *Type) UnmarshalYAML(unmarshal func(interface{}) error) error { 147 var str string 148 if err := unmarshal(&str); err != nil { 149 return err 150 } 151 152 mt, err := ParseType(str) 153 if err != nil { 154 return err 155 } 156 157 *t = mt 158 return nil 159 }