graphite.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package metrics
  2. import (
  3. "bytes"
  4. "fmt"
  5. "net"
  6. "strings"
  7. "time"
  8. "github.com/grafana/grafana/pkg/log"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. type GraphitePublisher struct {
  12. address string
  13. protocol string
  14. prefix string
  15. prevCounts map[string]int64
  16. }
  17. func CreateGraphitePublisher() (*GraphitePublisher, error) {
  18. graphiteSection, err := setting.Cfg.GetSection("metrics.graphite")
  19. if err != nil {
  20. return nil, nil
  21. }
  22. publisher := &GraphitePublisher{}
  23. publisher.prevCounts = make(map[string]int64)
  24. publisher.protocol = "tcp"
  25. publisher.address = graphiteSection.Key("address").MustString("localhost:2003")
  26. safeInstanceName := strings.Replace(setting.InstanceName, ".", "_", -1)
  27. prefix := graphiteSection.Key("prefix").Value()
  28. if prefix == "" {
  29. prefix = "service.grafana.%(instance_name)s."
  30. }
  31. publisher.prefix = strings.Replace(prefix, "%(instance_name)s", safeInstanceName, -1)
  32. return publisher, nil
  33. }
  34. func (this *GraphitePublisher) Publish(metrics []Metric) {
  35. conn, err := net.DialTimeout(this.protocol, this.address, time.Second*5)
  36. if err != nil {
  37. log.Error(3, "Metrics: GraphitePublisher: Failed to connect to %s!", err)
  38. return
  39. }
  40. buf := bytes.NewBufferString("")
  41. now := time.Now().Unix()
  42. for _, m := range metrics {
  43. metricName := this.prefix + m.Name() + m.StringifyTags()
  44. switch metric := m.(type) {
  45. case Counter:
  46. this.addCount(buf, metricName+".count", metric.Count(), now)
  47. case Timer:
  48. percentiles := metric.Percentiles([]float64{0.25, 0.75, 0.90, 0.99})
  49. this.addCount(buf, metricName+".count", metric.Count(), now)
  50. this.addInt(buf, metricName+".max", metric.Max(), now)
  51. this.addInt(buf, metricName+".min", metric.Min(), now)
  52. this.addFloat(buf, metricName+".mean", metric.Mean(), now)
  53. this.addFloat(buf, metricName+".std", metric.StdDev(), now)
  54. this.addFloat(buf, metricName+".p25", percentiles[0], now)
  55. this.addFloat(buf, metricName+".p75", percentiles[1], now)
  56. this.addFloat(buf, metricName+".p90", percentiles[2], now)
  57. this.addFloat(buf, metricName+".p99", percentiles[3], now)
  58. }
  59. }
  60. log.Trace("Metrics: GraphitePublisher.Publish() \n%s", buf)
  61. _, err = conn.Write(buf.Bytes())
  62. if err != nil {
  63. log.Error(3, "Metrics: GraphitePublisher: Failed to send metrics! %s", err)
  64. }
  65. }
  66. func (this *GraphitePublisher) addInt(buf *bytes.Buffer, metric string, value int64, now int64) {
  67. buf.WriteString(fmt.Sprintf("%s %d %d\n", metric, value, now))
  68. }
  69. func (this *GraphitePublisher) addFloat(buf *bytes.Buffer, metric string, value float64, now int64) {
  70. buf.WriteString(fmt.Sprintf("%s %f %d\n", metric, value, now))
  71. }
  72. func (this *GraphitePublisher) addCount(buf *bytes.Buffer, metric string, value int64, now int64) {
  73. delta := value
  74. if last, ok := this.prevCounts[metric]; ok {
  75. delta = calculateDelta(last, value)
  76. }
  77. this.prevCounts[metric] = value
  78. buf.WriteString(fmt.Sprintf("%s %d %d\n", metric, delta, now))
  79. }