opentsdb.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package metrics
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "net"
  7. "os"
  8. "strings"
  9. "time"
  10. )
  11. var shortHostName string = ""
  12. // OpenTSDBConfig provides a container with configuration parameters for
  13. // the OpenTSDB exporter
  14. type OpenTSDBConfig struct {
  15. Addr *net.TCPAddr // Network address to connect to
  16. Registry Registry // Registry to be exported
  17. FlushInterval time.Duration // Flush interval
  18. DurationUnit time.Duration // Time conversion unit for durations
  19. Prefix string // Prefix to be prepended to metric names
  20. }
  21. // OpenTSDB is a blocking exporter function which reports metrics in r
  22. // to a TSDB server located at addr, flushing them every d duration
  23. // and prepending metric names with prefix.
  24. func OpenTSDB(r Registry, d time.Duration, prefix string, addr *net.TCPAddr) {
  25. OpenTSDBWithConfig(OpenTSDBConfig{
  26. Addr: addr,
  27. Registry: r,
  28. FlushInterval: d,
  29. DurationUnit: time.Nanosecond,
  30. Prefix: prefix,
  31. })
  32. }
  33. // OpenTSDBWithConfig is a blocking exporter function just like OpenTSDB,
  34. // but it takes a OpenTSDBConfig instead.
  35. func OpenTSDBWithConfig(c OpenTSDBConfig) {
  36. for _ = range time.Tick(c.FlushInterval) {
  37. if err := openTSDB(&c); nil != err {
  38. log.Println(err)
  39. }
  40. }
  41. }
  42. func getShortHostname() string {
  43. if shortHostName == "" {
  44. host, _ := os.Hostname()
  45. if index := strings.Index(host, "."); index > 0 {
  46. shortHostName = host[:index]
  47. } else {
  48. shortHostName = host
  49. }
  50. }
  51. return shortHostName
  52. }
  53. func openTSDB(c *OpenTSDBConfig) error {
  54. shortHostname := getShortHostname()
  55. now := time.Now().Unix()
  56. du := float64(c.DurationUnit)
  57. conn, err := net.DialTCP("tcp", nil, c.Addr)
  58. if nil != err {
  59. return err
  60. }
  61. defer conn.Close()
  62. w := bufio.NewWriter(conn)
  63. c.Registry.Each(func(name string, i interface{}) {
  64. switch metric := i.(type) {
  65. case Counter:
  66. fmt.Fprintf(w, "put %s.%s.count %d %d host=%s\n", c.Prefix, name, now, metric.Count(), shortHostname)
  67. case Gauge:
  68. fmt.Fprintf(w, "put %s.%s.value %d %d host=%s\n", c.Prefix, name, now, metric.Value(), shortHostname)
  69. case GaugeFloat64:
  70. fmt.Fprintf(w, "put %s.%s.value %d %f host=%s\n", c.Prefix, name, now, metric.Value(), shortHostname)
  71. case Histogram:
  72. h := metric.Snapshot()
  73. ps := h.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  74. fmt.Fprintf(w, "put %s.%s.count %d %d host=%s\n", c.Prefix, name, now, h.Count(), shortHostname)
  75. fmt.Fprintf(w, "put %s.%s.min %d %d host=%s\n", c.Prefix, name, now, h.Min(), shortHostname)
  76. fmt.Fprintf(w, "put %s.%s.max %d %d host=%s\n", c.Prefix, name, now, h.Max(), shortHostname)
  77. fmt.Fprintf(w, "put %s.%s.mean %d %.2f host=%s\n", c.Prefix, name, now, h.Mean(), shortHostname)
  78. fmt.Fprintf(w, "put %s.%s.std-dev %d %.2f host=%s\n", c.Prefix, name, now, h.StdDev(), shortHostname)
  79. fmt.Fprintf(w, "put %s.%s.50-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[0], shortHostname)
  80. fmt.Fprintf(w, "put %s.%s.75-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[1], shortHostname)
  81. fmt.Fprintf(w, "put %s.%s.95-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[2], shortHostname)
  82. fmt.Fprintf(w, "put %s.%s.99-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[3], shortHostname)
  83. fmt.Fprintf(w, "put %s.%s.999-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[4], shortHostname)
  84. case Meter:
  85. m := metric.Snapshot()
  86. fmt.Fprintf(w, "put %s.%s.count %d %d host=%s\n", c.Prefix, name, now, m.Count(), shortHostname)
  87. fmt.Fprintf(w, "put %s.%s.one-minute %d %.2f host=%s\n", c.Prefix, name, now, m.Rate1(), shortHostname)
  88. fmt.Fprintf(w, "put %s.%s.five-minute %d %.2f host=%s\n", c.Prefix, name, now, m.Rate5(), shortHostname)
  89. fmt.Fprintf(w, "put %s.%s.fifteen-minute %d %.2f host=%s\n", c.Prefix, name, now, m.Rate15(), shortHostname)
  90. fmt.Fprintf(w, "put %s.%s.mean %d %.2f host=%s\n", c.Prefix, name, now, m.RateMean(), shortHostname)
  91. case Timer:
  92. t := metric.Snapshot()
  93. ps := t.Percentiles([]float64{0.5, 0.75, 0.95, 0.99, 0.999})
  94. fmt.Fprintf(w, "put %s.%s.count %d %d host=%s\n", c.Prefix, name, now, t.Count(), shortHostname)
  95. fmt.Fprintf(w, "put %s.%s.min %d %d host=%s\n", c.Prefix, name, now, t.Min()/int64(du), shortHostname)
  96. fmt.Fprintf(w, "put %s.%s.max %d %d host=%s\n", c.Prefix, name, now, t.Max()/int64(du), shortHostname)
  97. fmt.Fprintf(w, "put %s.%s.mean %d %.2f host=%s\n", c.Prefix, name, now, t.Mean()/du, shortHostname)
  98. fmt.Fprintf(w, "put %s.%s.std-dev %d %.2f host=%s\n", c.Prefix, name, now, t.StdDev()/du, shortHostname)
  99. fmt.Fprintf(w, "put %s.%s.50-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[0]/du, shortHostname)
  100. fmt.Fprintf(w, "put %s.%s.75-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[1]/du, shortHostname)
  101. fmt.Fprintf(w, "put %s.%s.95-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[2]/du, shortHostname)
  102. fmt.Fprintf(w, "put %s.%s.99-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[3]/du, shortHostname)
  103. fmt.Fprintf(w, "put %s.%s.999-percentile %d %.2f host=%s\n", c.Prefix, name, now, ps[4]/du, shortHostname)
  104. fmt.Fprintf(w, "put %s.%s.one-minute %d %.2f host=%s\n", c.Prefix, name, now, t.Rate1(), shortHostname)
  105. fmt.Fprintf(w, "put %s.%s.five-minute %d %.2f host=%s\n", c.Prefix, name, now, t.Rate5(), shortHostname)
  106. fmt.Fprintf(w, "put %s.%s.fifteen-minute %d %.2f host=%s\n", c.Prefix, name, now, t.Rate15(), shortHostname)
  107. fmt.Fprintf(w, "put %s.%s.mean-rate %d %.2f host=%s\n", c.Prefix, name, now, t.RateMean(), shortHostname)
  108. }
  109. w.Flush()
  110. })
  111. return nil
  112. }