gauge.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // A Gauge is an instantaneous measurement of a value.
  6. //
  7. // Use a gauge to track metrics which increase and decrease (e.g., amount of
  8. // free memory).
  9. import (
  10. "fmt"
  11. )
  12. // Gauge sets a gauge to a value
  13. func (m *CirconusMetrics) Gauge(metric string, val interface{}) {
  14. m.SetGauge(metric, val)
  15. }
  16. // SetGauge sets a gauge to a value
  17. func (m *CirconusMetrics) SetGauge(metric string, val interface{}) {
  18. m.gm.Lock()
  19. defer m.gm.Unlock()
  20. m.gauges[metric] = val
  21. }
  22. // AddGauge adds value to existing gauge
  23. func (m *CirconusMetrics) AddGauge(metric string, val interface{}) {
  24. m.gm.Lock()
  25. defer m.gm.Unlock()
  26. v, ok := m.gauges[metric]
  27. if !ok {
  28. m.gauges[metric] = val
  29. return
  30. }
  31. switch val.(type) {
  32. default:
  33. // ignore it, unsupported type
  34. case int:
  35. m.gauges[metric] = v.(int) + val.(int)
  36. case int8:
  37. m.gauges[metric] = v.(int8) + val.(int8)
  38. case int16:
  39. m.gauges[metric] = v.(int16) + val.(int16)
  40. case int32:
  41. m.gauges[metric] = v.(int32) + val.(int32)
  42. case int64:
  43. m.gauges[metric] = v.(int64) + val.(int64)
  44. case uint:
  45. m.gauges[metric] = v.(uint) + val.(uint)
  46. case uint8:
  47. m.gauges[metric] = v.(uint8) + val.(uint8)
  48. case uint16:
  49. m.gauges[metric] = v.(uint16) + val.(uint16)
  50. case uint32:
  51. m.gauges[metric] = v.(uint32) + val.(uint32)
  52. case uint64:
  53. m.gauges[metric] = v.(uint64) + val.(uint64)
  54. case float32:
  55. m.gauges[metric] = v.(float32) + val.(float32)
  56. case float64:
  57. m.gauges[metric] = v.(float64) + val.(float64)
  58. }
  59. }
  60. // RemoveGauge removes a gauge
  61. func (m *CirconusMetrics) RemoveGauge(metric string) {
  62. m.gm.Lock()
  63. defer m.gm.Unlock()
  64. delete(m.gauges, metric)
  65. }
  66. // GetGaugeTest returns the current value for a gauge. (note: it is a function specifically for "testing", disable automatic submission during testing.)
  67. func (m *CirconusMetrics) GetGaugeTest(metric string) (interface{}, error) {
  68. m.gm.Lock()
  69. defer m.gm.Unlock()
  70. if val, ok := m.gauges[metric]; ok {
  71. return val, nil
  72. }
  73. return nil, fmt.Errorf("Gauge metric '%s' not found", metric)
  74. }
  75. // SetGaugeFunc sets a gauge to a function [called at flush interval]
  76. func (m *CirconusMetrics) SetGaugeFunc(metric string, fn func() int64) {
  77. m.gfm.Lock()
  78. defer m.gfm.Unlock()
  79. m.gaugeFuncs[metric] = fn
  80. }
  81. // RemoveGaugeFunc removes a gauge function
  82. func (m *CirconusMetrics) RemoveGaugeFunc(metric string) {
  83. m.gfm.Lock()
  84. defer m.gfm.Unlock()
  85. delete(m.gaugeFuncs, metric)
  86. }
  87. // getGaugeType returns accurate resmon type for underlying type of gauge value
  88. func (m *CirconusMetrics) getGaugeType(v interface{}) string {
  89. mt := "n"
  90. switch v.(type) {
  91. case int:
  92. mt = "i"
  93. case int8:
  94. mt = "i"
  95. case int16:
  96. mt = "i"
  97. case int32:
  98. mt = "i"
  99. case uint:
  100. mt = "I"
  101. case uint8:
  102. mt = "I"
  103. case uint16:
  104. mt = "I"
  105. case uint32:
  106. mt = "I"
  107. case int64:
  108. mt = "l"
  109. case uint64:
  110. mt = "L"
  111. }
  112. return mt
  113. }