graphite.go 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package receiver
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/grafana/grafana/pkg/log"
  6. "net"
  7. "time"
  8. )
  9. type GraphiteSender struct {
  10. Host string
  11. Port string
  12. Protocol string
  13. Prefix string
  14. }
  15. func (this *GraphiteSender) Send(metrics map[string]interface{}) error {
  16. log.Debug("GraphiteSender: Sending metrics to graphite")
  17. address := fmt.Sprintf("%s:%s", this.Host, this.Port)
  18. conn, err := net.DialTimeout(this.Protocol, address, time.Second*5)
  19. if err != nil {
  20. return fmt.Errorf("Graphite Sender: Failed to connec to %s!", err)
  21. }
  22. buf := bytes.NewBufferString("")
  23. now := time.Now().Unix()
  24. for key, value := range metrics {
  25. metricName := this.Prefix + key
  26. line := fmt.Sprintf("%s %d %d\n", metricName, value, now)
  27. log.Debug("SendMetric: sending %s", line)
  28. buf.WriteString(line)
  29. }
  30. _, err = conn.Write(buf.Bytes())
  31. if err != nil {
  32. return fmt.Errorf("Graphite Sender: Failed to send metrics!", err)
  33. }
  34. return nil
  35. }