client.go 777 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package statsd
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. type Client struct {
  7. w io.Writer
  8. prefix string
  9. }
  10. func NewClient(w io.Writer, prefix string) *Client {
  11. return &Client{
  12. w: w,
  13. prefix: prefix,
  14. }
  15. }
  16. func (c *Client) Incr(stat string, count int64) error {
  17. return c.send(stat, "%d|c", count)
  18. }
  19. func (c *Client) Decr(stat string, count int64) error {
  20. return c.send(stat, "%d|c", -count)
  21. }
  22. func (c *Client) Timing(stat string, delta int64) error {
  23. return c.send(stat, "%d|ms", delta)
  24. }
  25. func (c *Client) Gauge(stat string, value int64) error {
  26. return c.send(stat, "%d|g", value)
  27. }
  28. func (c *Client) send(stat string, format string, value int64) error {
  29. format = fmt.Sprintf("%s%s:%s\n", c.prefix, stat, format)
  30. _, err := fmt.Fprintf(c.w, format, value)
  31. return err
  32. }