util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright 2016 Circonus, Inc. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package circonusgometrics
  5. import (
  6. "github.com/circonus-labs/circonusllhist"
  7. )
  8. // Reset removes all existing counters and gauges.
  9. func (m *CirconusMetrics) Reset() {
  10. m.cm.Lock()
  11. defer m.cm.Unlock()
  12. m.cfm.Lock()
  13. defer m.cfm.Unlock()
  14. m.gm.Lock()
  15. defer m.gm.Unlock()
  16. m.gfm.Lock()
  17. defer m.gfm.Unlock()
  18. m.hm.Lock()
  19. defer m.hm.Unlock()
  20. m.tm.Lock()
  21. defer m.tm.Unlock()
  22. m.tfm.Lock()
  23. defer m.tfm.Unlock()
  24. m.counters = make(map[string]uint64)
  25. m.counterFuncs = make(map[string]func() uint64)
  26. m.gauges = make(map[string]interface{})
  27. m.gaugeFuncs = make(map[string]func() int64)
  28. m.histograms = make(map[string]*Histogram)
  29. m.text = make(map[string]string)
  30. m.textFuncs = make(map[string]func() string)
  31. }
  32. // snapshot returns a copy of the values of all registered counters and gauges.
  33. func (m *CirconusMetrics) snapshot() (c map[string]uint64, g map[string]interface{}, h map[string]*circonusllhist.Histogram, t map[string]string) {
  34. c = m.snapCounters()
  35. g = m.snapGauges()
  36. h = m.snapHistograms()
  37. t = m.snapText()
  38. return
  39. }
  40. func (m *CirconusMetrics) snapCounters() map[string]uint64 {
  41. m.cm.Lock()
  42. defer m.cm.Unlock()
  43. m.cfm.Lock()
  44. defer m.cfm.Unlock()
  45. c := make(map[string]uint64, len(m.counters)+len(m.counterFuncs))
  46. for n, v := range m.counters {
  47. c[n] = v
  48. }
  49. if m.resetCounters && len(c) > 0 {
  50. m.counters = make(map[string]uint64)
  51. }
  52. for n, f := range m.counterFuncs {
  53. c[n] = f()
  54. }
  55. return c
  56. }
  57. func (m *CirconusMetrics) snapGauges() map[string]interface{} {
  58. m.gm.Lock()
  59. defer m.gm.Unlock()
  60. m.gfm.Lock()
  61. defer m.gfm.Unlock()
  62. g := make(map[string]interface{}, len(m.gauges)+len(m.gaugeFuncs))
  63. for n, v := range m.gauges {
  64. g[n] = v
  65. }
  66. if m.resetGauges && len(g) > 0 {
  67. m.gauges = make(map[string]interface{})
  68. }
  69. for n, f := range m.gaugeFuncs {
  70. g[n] = f()
  71. }
  72. return g
  73. }
  74. func (m *CirconusMetrics) snapHistograms() map[string]*circonusllhist.Histogram {
  75. m.hm.Lock()
  76. defer m.hm.Unlock()
  77. h := make(map[string]*circonusllhist.Histogram, len(m.histograms))
  78. for n, hist := range m.histograms {
  79. hist.rw.Lock()
  80. h[n] = hist.hist.CopyAndReset()
  81. hist.rw.Unlock()
  82. }
  83. if m.resetHistograms && len(h) > 0 {
  84. m.histograms = make(map[string]*Histogram)
  85. }
  86. return h
  87. }
  88. func (m *CirconusMetrics) snapText() map[string]string {
  89. m.tm.Lock()
  90. defer m.tm.Unlock()
  91. m.tfm.Lock()
  92. defer m.tfm.Unlock()
  93. t := make(map[string]string, len(m.text)+len(m.textFuncs))
  94. for n, v := range m.text {
  95. t[n] = v
  96. }
  97. if m.resetText && len(t) > 0 {
  98. m.text = make(map[string]string)
  99. }
  100. for n, f := range m.textFuncs {
  101. t[n] = f()
  102. }
  103. return t
  104. }