librato.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package librato
  2. import (
  3. "fmt"
  4. "github.com/rcrowley/go-metrics"
  5. "github.com/samuel/go-librato/librato"
  6. "log"
  7. "math"
  8. "time"
  9. )
  10. type LibratoReporter struct {
  11. Email, Token string
  12. Source string
  13. Interval time.Duration
  14. Registry metrics.Registry
  15. Percentiles []float64
  16. }
  17. func (self *LibratoReporter) Run() {
  18. ticker := time.Tick(self.Interval * time.Millisecond)
  19. metricsApi := &librato.Metrics{self.Email, self.Token}
  20. for now := range ticker {
  21. var metrics *librato.MetricsFormat
  22. var err error
  23. if metrics, err = self.BuildRequest(now, self.Registry); err != nil {
  24. log.Printf("ERROR constructing librato request body %s", err)
  25. }
  26. if err := metricsApi.SendMetrics(metrics); err != nil {
  27. log.Printf("ERROR sending metrics to librato %s", err)
  28. }
  29. }
  30. }
  31. // calculate sum of squares from data provided by metrics.Histogram
  32. // see http://en.wikipedia.org/wiki/Standard_deviation#Rapid_calculation_methods
  33. func sumSquares(m metrics.Histogram) float64 {
  34. count := float64(m.Count())
  35. sum := m.Mean() * float64(m.Count())
  36. sumSquared := math.Pow(float64(sum), 2)
  37. sumSquares := math.Pow(count*m.StdDev(), 2) + sumSquared/float64(m.Count())
  38. if math.IsNaN(sumSquares) {
  39. return 0.0
  40. }
  41. return sumSquared
  42. }
  43. func sumSquaresTimer(m metrics.Timer) float64 {
  44. count := float64(m.Count())
  45. sum := m.Mean() * float64(m.Count())
  46. sumSquared := math.Pow(float64(sum), 2)
  47. sumSquares := math.Pow(count*m.StdDev(), 2) + sumSquared/float64(m.Count())
  48. if math.IsNaN(sumSquares) {
  49. return 0.0
  50. }
  51. return sumSquares
  52. }
  53. func (self *LibratoReporter) BuildRequest(now time.Time, r metrics.Registry) (snapshot *librato.MetricsFormat, err error) {
  54. snapshot = &librato.MetricsFormat{}
  55. snapshot.MeasureTime = now.Unix()
  56. snapshot.Source = self.Source
  57. snapshot.Gauges = make([]interface{}, 0)
  58. snapshot.Counters = make([]librato.Metric, 0)
  59. histogramGaugeCount := 1 + len(self.Percentiles)
  60. r.Each(func(name string, metric interface{}) {
  61. switch m := metric.(type) {
  62. case metrics.Counter:
  63. libratoName := fmt.Sprintf("%s.%s", name, "count")
  64. snapshot.Counters = append(snapshot.Counters, librato.Metric{Name: libratoName, Value: float64(m.Count())})
  65. case metrics.Gauge:
  66. snapshot.Gauges = append(snapshot.Gauges, librato.Metric{Name: name, Value: float64(m.Value())})
  67. case metrics.Histogram:
  68. if m.Count() > 0 {
  69. libratoName := fmt.Sprintf("%s.%s", name, "hist")
  70. gauges := make([]interface{}, histogramGaugeCount, histogramGaugeCount)
  71. gauges[0] = librato.Gauge{
  72. Name: libratoName,
  73. Count: uint64(m.Count()),
  74. Sum: m.Mean() * float64(m.Count()),
  75. Max: float64(m.Max()),
  76. Min: float64(m.Min()),
  77. SumSquares: sumSquares(m),
  78. }
  79. for i, p := range self.Percentiles {
  80. gauges[i+1] = librato.Metric{Name: fmt.Sprintf("%s.%.2f", libratoName, p), Value: m.Percentile(p)}
  81. }
  82. snapshot.Gauges = append(snapshot.Gauges, gauges...)
  83. }
  84. case metrics.Meter:
  85. snapshot.Counters = append(snapshot.Counters, librato.Metric{Name: name, Value: float64(m.Count())})
  86. snapshot.Gauges = append(snapshot.Gauges,
  87. librato.Metric{
  88. Name: fmt.Sprintf("%s.%s", name, "1min"),
  89. Value: m.Rate1(),
  90. },
  91. librato.Metric{
  92. Name: fmt.Sprintf("%s.%s", name, "5min"),
  93. Value: m.Rate5(),
  94. },
  95. librato.Metric{
  96. Name: fmt.Sprintf("%s.%s", name, "15min"),
  97. Value: m.Rate15(),
  98. },
  99. )
  100. case metrics.Timer:
  101. if m.Count() > 0 {
  102. libratoName := fmt.Sprintf("%s.%s", name, "timer")
  103. gauges := make([]interface{}, histogramGaugeCount, histogramGaugeCount)
  104. gauges[0] = librato.Gauge{
  105. Name: libratoName,
  106. Count: uint64(m.Count()),
  107. Sum: m.Mean() * float64(m.Count()),
  108. Max: float64(m.Max()),
  109. Min: float64(m.Min()),
  110. SumSquares: sumSquaresTimer(m),
  111. }
  112. for i, p := range self.Percentiles {
  113. gauges[i+1] = librato.Metric{Name: fmt.Sprintf("%s.%2.0f", libratoName, p*100), Value: m.Percentile(p)}
  114. }
  115. snapshot.Gauges = append(snapshot.Gauges, gauges...)
  116. snapshot.Gauges = append(snapshot.Gauges,
  117. librato.Metric{
  118. Name: fmt.Sprintf("%s.%s", name, "1min"),
  119. Value: m.Rate1(),
  120. },
  121. librato.Metric{
  122. Name: fmt.Sprintf("%s.%s", name, "5min"),
  123. Value: m.Rate5(),
  124. },
  125. librato.Metric{
  126. Name: fmt.Sprintf("%s.%s", name, "15min"),
  127. Value: m.Rate15(),
  128. },
  129. )
  130. }
  131. }
  132. })
  133. return
  134. }