vitess.io/vitess@v0.16.2/go/stats/duration.go (about) 1 /* 2 Copyright 2019 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 "vitess.io/vitess/go/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 // Get returns the value. 110 func (cf CounterDurationFunc) Get() int64 { 111 return int64(cf.F()) 112 } 113 114 // String is the implementation of expvar.var. 115 func (cf CounterDurationFunc) String() string { 116 return strconv.FormatInt(int64(cf.F()), 10) 117 } 118 119 // GaugeDurationFunc allows to provide the value via a custom function. 120 type GaugeDurationFunc struct { 121 CounterDurationFunc 122 } 123 124 // NewGaugeDurationFunc creates a new GaugeDurationFunc instance and 125 // publishes it if name is set. 126 func NewGaugeDurationFunc(name string, help string, f func() time.Duration) *GaugeDurationFunc { 127 gf := &GaugeDurationFunc{ 128 CounterDurationFunc: CounterDurationFunc{ 129 F: f, 130 help: help, 131 }, 132 } 133 134 if name != "" { 135 publish(name, gf) 136 } 137 return gf 138 }