github.com/XiaoMi/Gaea@v1.2.5/stats/duration.go (about) 1 /* 2 Copyright 2018 The Vitess Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package stats 18 19 import ( 20 "strconv" 21 "time" 22 23 "github.com/XiaoMi/Gaea/util/sync2" 24 ) 25 26 // CounterDuration exports a time.Duration as counter. 27 type CounterDuration struct { 28 i sync2.AtomicDuration 29 help string 30 } 31 32 // NewCounterDuration returns a new CounterDuration. 33 func NewCounterDuration(name, help string) *CounterDuration { 34 cd := &CounterDuration{ 35 help: help, 36 } 37 publish(name, cd) 38 return cd 39 } 40 41 // Help implements the Variable interface. 42 func (cd CounterDuration) Help() string { 43 return cd.help 44 } 45 46 // String is the implementation of expvar.var. 47 func (cd CounterDuration) String() string { 48 return strconv.FormatInt(int64(cd.i.Get()), 10) 49 } 50 51 // Add adds the provided value to the CounterDuration. 52 func (cd *CounterDuration) Add(delta time.Duration) { 53 cd.i.Add(delta) 54 } 55 56 // Get returns the value. 57 func (cd *CounterDuration) Get() time.Duration { 58 return cd.i.Get() 59 } 60 61 // GaugeDuration exports a time.Duration as gauge. 62 // In addition to CounterDuration, it also has Set() which allows overriding 63 // the current value. 64 type GaugeDuration struct { 65 CounterDuration 66 } 67 68 // NewGaugeDuration returns a new GaugeDuration. 69 func NewGaugeDuration(name, help string) *GaugeDuration { 70 gd := &GaugeDuration{ 71 CounterDuration: CounterDuration{ 72 help: help, 73 }, 74 } 75 publish(name, gd) 76 return gd 77 } 78 79 // Set sets the value. 80 func (gd *GaugeDuration) Set(value time.Duration) { 81 gd.i.Set(value) 82 } 83 84 // CounterDurationFunc allows to provide the value via a custom function. 85 type CounterDurationFunc struct { 86 F func() time.Duration 87 help string 88 } 89 90 // NewCounterDurationFunc creates a new CounterDurationFunc instance and 91 // publishes it if name is set. 92 func NewCounterDurationFunc(name string, help string, f func() time.Duration) *CounterDurationFunc { 93 cf := &CounterDurationFunc{ 94 F: f, 95 help: help, 96 } 97 98 if name != "" { 99 publish(name, cf) 100 } 101 return cf 102 } 103 104 // Help implements the Variable interface. 105 func (cf CounterDurationFunc) Help() string { 106 return cf.help 107 } 108 109 // String is the implementation of expvar.var. 110 func (cf CounterDurationFunc) String() string { 111 return strconv.FormatInt(int64(cf.F()), 10) 112 } 113 114 // GaugeDurationFunc allows to provide the value via a custom function. 115 type GaugeDurationFunc struct { 116 CounterDurationFunc 117 } 118 119 // NewGaugeDurationFunc creates a new GaugeDurationFunc instance and 120 // publishes it if name is set. 121 func NewGaugeDurationFunc(name string, help string, f func() time.Duration) *GaugeDurationFunc { 122 gf := &GaugeDurationFunc{ 123 CounterDurationFunc: CounterDurationFunc{ 124 F: f, 125 help: help, 126 }, 127 } 128 129 if name != "" { 130 publish(name, gf) 131 } 132 return gf 133 }