histogram.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. "fmt"
  7. "sync"
  8. "github.com/circonus-labs/circonusllhist"
  9. )
  10. // Histogram measures the distribution of a stream of values.
  11. type Histogram struct {
  12. name string
  13. hist *circonusllhist.Histogram
  14. rw sync.RWMutex
  15. }
  16. // Timing adds a value to a histogram
  17. func (m *CirconusMetrics) Timing(metric string, val float64) {
  18. m.SetHistogramValue(metric, val)
  19. }
  20. // RecordValue adds a value to a histogram
  21. func (m *CirconusMetrics) RecordValue(metric string, val float64) {
  22. m.SetHistogramValue(metric, val)
  23. }
  24. // RecordCountForValue adds count n for value to a histogram
  25. func (m *CirconusMetrics) RecordCountForValue(metric string, val float64, n int64) {
  26. hist := m.NewHistogram(metric)
  27. m.hm.Lock()
  28. hist.rw.Lock()
  29. hist.hist.RecordValues(val, n)
  30. hist.rw.Unlock()
  31. m.hm.Unlock()
  32. }
  33. // SetHistogramValue adds a value to a histogram
  34. func (m *CirconusMetrics) SetHistogramValue(metric string, val float64) {
  35. hist := m.NewHistogram(metric)
  36. m.hm.Lock()
  37. hist.rw.Lock()
  38. hist.hist.RecordValue(val)
  39. hist.rw.Unlock()
  40. m.hm.Unlock()
  41. }
  42. // GetHistogramTest returns the current value for a gauge. (note: it is a function specifically for "testing", disable automatic submission during testing.)
  43. func (m *CirconusMetrics) GetHistogramTest(metric string) ([]string, error) {
  44. m.hm.Lock()
  45. defer m.hm.Unlock()
  46. if hist, ok := m.histograms[metric]; ok {
  47. return hist.hist.DecStrings(), nil
  48. }
  49. return []string{""}, fmt.Errorf("Histogram metric '%s' not found", metric)
  50. }
  51. // RemoveHistogram removes a histogram
  52. func (m *CirconusMetrics) RemoveHistogram(metric string) {
  53. m.hm.Lock()
  54. delete(m.histograms, metric)
  55. m.hm.Unlock()
  56. }
  57. // NewHistogram returns a histogram instance.
  58. func (m *CirconusMetrics) NewHistogram(metric string) *Histogram {
  59. m.hm.Lock()
  60. defer m.hm.Unlock()
  61. if hist, ok := m.histograms[metric]; ok {
  62. return hist
  63. }
  64. hist := &Histogram{
  65. name: metric,
  66. hist: circonusllhist.New(),
  67. }
  68. m.histograms[metric] = hist
  69. return hist
  70. }
  71. // Name returns the name from a histogram instance
  72. func (h *Histogram) Name() string {
  73. return h.name
  74. }
  75. // RecordValue records the given value to a histogram instance
  76. func (h *Histogram) RecordValue(v float64) {
  77. h.rw.Lock()
  78. h.hist.RecordValue(v)
  79. h.rw.Unlock()
  80. }