github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/util/metric/registry_test.go (about) 1 // Copyright 2015 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package metric 12 13 import ( 14 "testing" 15 "time" 16 ) 17 18 func (r *Registry) findMetricByName(name string) Iterable { 19 for _, metric := range r.tracked { 20 if metric.GetName() == name { 21 return metric 22 } 23 } 24 return nil 25 } 26 27 // getCounter returns the Counter in this registry with the given name. If a 28 // Counter with this name is not present (including if a non-Counter Iterable is 29 // registered with the name), nil is returned. 30 func (r *Registry) getCounter(name string) *Counter { 31 r.Lock() 32 defer r.Unlock() 33 iterable := r.findMetricByName(name) 34 if iterable == nil { 35 return nil 36 } 37 38 switch t := iterable.(type) { 39 case *Counter: 40 return t 41 default: 42 } 43 return nil 44 } 45 46 // getGauge returns the Gauge in this registry with the given name. If a Gauge 47 // with this name is not present (including if a non-Gauge Iterable is 48 // registered with the name), nil is returned. 49 func (r *Registry) getGauge(name string) *Gauge { 50 r.Lock() 51 defer r.Unlock() 52 iterable := r.findMetricByName(name) 53 if iterable == nil { 54 return nil 55 } 56 gauge, ok := iterable.(*Gauge) 57 if !ok { 58 return nil 59 } 60 return gauge 61 } 62 63 func TestRegistry(t *testing.T) { 64 r := NewRegistry() 65 66 topGauge := NewGauge(Metadata{Name: "top.gauge"}) 67 r.AddMetric(topGauge) 68 69 r.AddMetric(NewGaugeFloat64(Metadata{Name: "top.floatgauge"})) 70 71 topCounter := NewCounter(Metadata{Name: "top.counter"}) 72 r.AddMetric(topCounter) 73 74 r.AddMetric(NewHistogram(Metadata{Name: "top.histogram"}, time.Minute, 1000, 3)) 75 76 r.AddMetric(NewGauge(Metadata{Name: "bottom.gauge"})) 77 ms := &struct { 78 StructGauge *Gauge 79 StructGauge64 *GaugeFloat64 80 StructCounter *Counter 81 StructHistogram *Histogram 82 // A few extra ones: either not exported, or not metric objects. 83 privateStructGauge *Gauge 84 privateStructGauge64 *GaugeFloat64 85 NotAMetric int 86 AlsoNotAMetric string 87 ReallyNotAMetric *Registry 88 }{ 89 StructGauge: NewGauge(Metadata{Name: "struct.gauge"}), 90 StructGauge64: NewGaugeFloat64(Metadata{Name: "struct.gauge64"}), 91 StructCounter: NewCounter(Metadata{Name: "struct.counter"}), 92 StructHistogram: NewHistogram(Metadata{Name: "struct.histogram"}, time.Minute, 1000, 3), 93 privateStructGauge: NewGauge(Metadata{Name: "struct.private-gauge"}), 94 privateStructGauge64: NewGaugeFloat64(Metadata{Name: "struct.private-gauge64"}), 95 NotAMetric: 0, 96 AlsoNotAMetric: "foo", 97 ReallyNotAMetric: NewRegistry(), 98 } 99 r.AddMetricStruct(ms) 100 101 expNames := map[string]struct{}{ 102 "top.histogram": {}, 103 "top.gauge": {}, 104 "top.floatgauge": {}, 105 "top.counter": {}, 106 "bottom.gauge": {}, 107 "struct.gauge": {}, 108 "struct.gauge64": {}, 109 "struct.counter": {}, 110 "struct.histogram": {}, 111 } 112 113 r.Each(func(name string, _ interface{}) { 114 if _, exist := expNames[name]; !exist { 115 t.Errorf("unexpected name: %s", name) 116 } 117 delete(expNames, name) 118 }) 119 if len(expNames) > 0 { 120 t.Fatalf("missed names: %v", expNames) 121 } 122 123 // Test get functions 124 if g := r.getGauge("top.gauge"); g != topGauge { 125 t.Errorf("getGauge returned %v, expected %v", g, topGauge) 126 } 127 if g := r.getGauge("bad"); g != nil { 128 t.Errorf("getGauge returned non-nil %v, expected nil", g) 129 } 130 if g := r.getGauge("top.histogram"); g != nil { 131 t.Errorf("getGauge returned non-nil %v of type %T when requesting non-gauge, expected nil", g, g) 132 } 133 134 if c := r.getCounter("top.counter"); c != topCounter { 135 t.Errorf("getCounter returned %v, expected %v", c, topCounter) 136 } 137 if c := r.getCounter("bad"); c != nil { 138 t.Errorf("getCounter returned non-nil %v, expected nil", c) 139 } 140 if c := r.getCounter("top.histogram"); c != nil { 141 t.Errorf("getCounter returned non-nil %v of type %T when requesting non-counter, expected nil", c, c) 142 } 143 }