graphite.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package nebula
  2. // This file is a trimmed, inlined copy of the graphite exporter from
  3. // github.com/cyberdelia/go-metrics-graphite, retaining only the Config type and
  4. // the Once entrypoint that Nebula uses. The upstream package has been
  5. // unmaintained for 10+ years, so it was vendored here to drop the dependency.
  6. // See https://github.com/slackhq/nebula/issues/1831.
  7. //
  8. // Copyright 2015 Timothée Peignier. All rights reserved.
  9. //
  10. // Redistribution and use in source and binary forms, with or without
  11. // modification, are permitted provided that the following conditions are met:
  12. //
  13. // 1. Redistributions of source code must retain the above copyright notice,
  14. // this list of conditions and the following disclaimer.
  15. //
  16. // 2. Redistributions in binary form must reproduce the above copyright notice,
  17. // this list of conditions and the following disclaimer in the documentation
  18. // and/or other materials provided with the distribution.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. import (
  31. "bufio"
  32. "fmt"
  33. "net"
  34. "strconv"
  35. "strings"
  36. "time"
  37. "github.com/rcrowley/go-metrics"
  38. )
  39. // graphiteConfigExport provides a container with configuration parameters for
  40. // the Graphite exporter.
  41. type graphiteConfigExport struct {
  42. Addr *net.TCPAddr // Network address to connect to
  43. Registry metrics.Registry // Registry to be exported
  44. FlushInterval time.Duration // Flush interval
  45. DurationUnit time.Duration // Time conversion unit for durations
  46. Prefix string // Prefix to be prepended to metric names
  47. Percentiles []float64 // Percentiles to export from timers and histograms
  48. }
  49. // graphiteOnce performs a single submission to Graphite, returning a non-nil
  50. // error on failed connections.
  51. func graphiteOnce(c graphiteConfigExport) error {
  52. now := time.Now().Unix()
  53. du := float64(c.DurationUnit)
  54. flushSeconds := float64(c.FlushInterval) / float64(time.Second)
  55. conn, err := net.DialTCP("tcp", nil, c.Addr)
  56. if err != nil {
  57. return err
  58. }
  59. defer conn.Close()
  60. w := bufio.NewWriter(conn)
  61. c.Registry.Each(func(name string, i any) {
  62. switch metric := i.(type) {
  63. case metrics.Counter:
  64. count := metric.Count()
  65. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, count, now)
  66. fmt.Fprintf(w, "%s.%s.count_ps %.2f %d\n", c.Prefix, name, float64(count)/flushSeconds, now)
  67. case metrics.Gauge:
  68. fmt.Fprintf(w, "%s.%s.value %d %d\n", c.Prefix, name, metric.Value(), now)
  69. case metrics.GaugeFloat64:
  70. fmt.Fprintf(w, "%s.%s.value %f %d\n", c.Prefix, name, metric.Value(), now)
  71. case metrics.Histogram:
  72. h := metric.Snapshot()
  73. ps := h.Percentiles(c.Percentiles)
  74. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, h.Count(), now)
  75. fmt.Fprintf(w, "%s.%s.min %d %d\n", c.Prefix, name, h.Min(), now)
  76. fmt.Fprintf(w, "%s.%s.max %d %d\n", c.Prefix, name, h.Max(), now)
  77. fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, h.Mean(), now)
  78. fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", c.Prefix, name, h.StdDev(), now)
  79. for psIdx, psKey := range c.Percentiles {
  80. key := strings.Replace(strconv.FormatFloat(psKey*100.0, 'f', -1, 64), ".", "", 1)
  81. fmt.Fprintf(w, "%s.%s.%s-percentile %.2f %d\n", c.Prefix, name, key, ps[psIdx], now)
  82. }
  83. case metrics.Meter:
  84. m := metric.Snapshot()
  85. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, m.Count(), now)
  86. fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", c.Prefix, name, m.Rate1(), now)
  87. fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", c.Prefix, name, m.Rate5(), now)
  88. fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", c.Prefix, name, m.Rate15(), now)
  89. fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, m.RateMean(), now)
  90. case metrics.Timer:
  91. t := metric.Snapshot()
  92. ps := t.Percentiles(c.Percentiles)
  93. count := t.Count()
  94. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, count, now)
  95. fmt.Fprintf(w, "%s.%s.count_ps %.2f %d\n", c.Prefix, name, float64(count)/flushSeconds, now)
  96. fmt.Fprintf(w, "%s.%s.min %d %d\n", c.Prefix, name, t.Min()/int64(du), now)
  97. fmt.Fprintf(w, "%s.%s.max %d %d\n", c.Prefix, name, t.Max()/int64(du), now)
  98. fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, t.Mean()/du, now)
  99. fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", c.Prefix, name, t.StdDev()/du, now)
  100. for psIdx, psKey := range c.Percentiles {
  101. key := strings.Replace(strconv.FormatFloat(psKey*100.0, 'f', -1, 64), ".", "", 1)
  102. fmt.Fprintf(w, "%s.%s.%s-percentile %.2f %d\n", c.Prefix, name, key, ps[psIdx]/du, now)
  103. }
  104. fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", c.Prefix, name, t.Rate1(), now)
  105. fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", c.Prefix, name, t.Rate5(), now)
  106. fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", c.Prefix, name, t.Rate15(), now)
  107. fmt.Fprintf(w, "%s.%s.mean-rate %.2f %d\n", c.Prefix, name, t.RateMean(), now)
  108. }
  109. w.Flush()
  110. })
  111. return nil
  112. }