1
0

graphite.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "context"
  33. "fmt"
  34. "net"
  35. "strconv"
  36. "strings"
  37. "time"
  38. "github.com/rcrowley/go-metrics"
  39. )
  40. // graphiteConfigExport provides a container with configuration parameters for
  41. // the Graphite exporter.
  42. type graphiteConfigExport struct {
  43. Addr *net.TCPAddr // Network address to connect to
  44. Registry metrics.Registry // Registry to be exported
  45. FlushInterval time.Duration // Flush interval
  46. DurationUnit time.Duration // Time conversion unit for durations
  47. Prefix string // Prefix to be prepended to metric names
  48. Percentiles []float64 // Percentiles to export from timers and histograms
  49. }
  50. // graphiteTimeout bounds one submission, the connect and the write together. An export is a few KB, a host that can't
  51. // take that in this long isn't going to.
  52. const graphiteTimeout = 30 * time.Second
  53. // graphiteOnce performs a single submission to Graphite, returning a non-nil
  54. // error if it could not connect or write all of it within timeout. It gives up
  55. // as soon as ctx is done.
  56. func graphiteOnce(ctx context.Context, c graphiteConfigExport, timeout time.Duration) error {
  57. now := time.Now().Unix()
  58. du := float64(c.DurationUnit)
  59. flushSeconds := float64(c.FlushInterval) / float64(time.Second)
  60. deadline := time.Now().Add(timeout)
  61. d := net.Dialer{Deadline: deadline}
  62. conn, err := d.DialContext(ctx, "tcp", c.Addr.String())
  63. if err != nil {
  64. return err
  65. }
  66. defer conn.Close()
  67. if err := conn.SetDeadline(deadline); err != nil {
  68. return err
  69. }
  70. stop := context.AfterFunc(ctx, func() { _ = conn.Close() })
  71. defer stop()
  72. w := bufio.NewWriter(conn)
  73. c.Registry.Each(func(name string, i any) {
  74. switch metric := i.(type) {
  75. case metrics.Counter:
  76. count := metric.Count()
  77. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, count, now)
  78. fmt.Fprintf(w, "%s.%s.count_ps %.2f %d\n", c.Prefix, name, float64(count)/flushSeconds, now)
  79. case metrics.Gauge:
  80. fmt.Fprintf(w, "%s.%s.value %d %d\n", c.Prefix, name, metric.Value(), now)
  81. case metrics.GaugeFloat64:
  82. fmt.Fprintf(w, "%s.%s.value %f %d\n", c.Prefix, name, metric.Value(), now)
  83. case metrics.Histogram:
  84. h := metric.Snapshot()
  85. ps := h.Percentiles(c.Percentiles)
  86. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, h.Count(), now)
  87. fmt.Fprintf(w, "%s.%s.min %d %d\n", c.Prefix, name, h.Min(), now)
  88. fmt.Fprintf(w, "%s.%s.max %d %d\n", c.Prefix, name, h.Max(), now)
  89. fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, h.Mean(), now)
  90. fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", c.Prefix, name, h.StdDev(), now)
  91. for psIdx, psKey := range c.Percentiles {
  92. key := strings.Replace(strconv.FormatFloat(psKey*100.0, 'f', -1, 64), ".", "", 1)
  93. fmt.Fprintf(w, "%s.%s.%s-percentile %.2f %d\n", c.Prefix, name, key, ps[psIdx], now)
  94. }
  95. case metrics.Meter:
  96. m := metric.Snapshot()
  97. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, m.Count(), now)
  98. fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", c.Prefix, name, m.Rate1(), now)
  99. fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", c.Prefix, name, m.Rate5(), now)
  100. fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", c.Prefix, name, m.Rate15(), now)
  101. fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, m.RateMean(), now)
  102. case metrics.Timer:
  103. t := metric.Snapshot()
  104. ps := t.Percentiles(c.Percentiles)
  105. count := t.Count()
  106. fmt.Fprintf(w, "%s.%s.count %d %d\n", c.Prefix, name, count, now)
  107. fmt.Fprintf(w, "%s.%s.count_ps %.2f %d\n", c.Prefix, name, float64(count)/flushSeconds, now)
  108. fmt.Fprintf(w, "%s.%s.min %d %d\n", c.Prefix, name, t.Min()/int64(du), now)
  109. fmt.Fprintf(w, "%s.%s.max %d %d\n", c.Prefix, name, t.Max()/int64(du), now)
  110. fmt.Fprintf(w, "%s.%s.mean %.2f %d\n", c.Prefix, name, t.Mean()/du, now)
  111. fmt.Fprintf(w, "%s.%s.std-dev %.2f %d\n", c.Prefix, name, t.StdDev()/du, now)
  112. for psIdx, psKey := range c.Percentiles {
  113. key := strings.Replace(strconv.FormatFloat(psKey*100.0, 'f', -1, 64), ".", "", 1)
  114. fmt.Fprintf(w, "%s.%s.%s-percentile %.2f %d\n", c.Prefix, name, key, ps[psIdx]/du, now)
  115. }
  116. fmt.Fprintf(w, "%s.%s.one-minute %.2f %d\n", c.Prefix, name, t.Rate1(), now)
  117. fmt.Fprintf(w, "%s.%s.five-minute %.2f %d\n", c.Prefix, name, t.Rate5(), now)
  118. fmt.Fprintf(w, "%s.%s.fifteen-minute %.2f %d\n", c.Prefix, name, t.Rate15(), now)
  119. fmt.Fprintf(w, "%s.%s.mean-rate %.2f %d\n", c.Prefix, name, t.RateMean(), now)
  120. }
  121. })
  122. // A failed write sticks, so this reports the first one
  123. return w.Flush()
  124. }