duration.go 3.2 KB

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