362f447d9f
* cmd/statreceiver: lua-scriptable stat receiver Change-Id: I3ce0fe3f1ef4b1f4f27eed90bac0e91cfecf22d7 * some updates Change-Id: I7c3485adcda1278fce01ae077b4761b3ddb9fb7a * more comments Change-Id: I0bb22993cd934c3d40fc1da80d07e49e686b80dd * linter fixes Change-Id: Ied014304ecb9aadcf00a6b66ad28f856a428d150 * catch errors Change-Id: I6e1920f1fd941e66199b30bc427285c19769fc70 * review feedback Change-Id: I9d4051851eab18970c5f5ddcf4ff265508e541d3 * errorgroup improvements Change-Id: I4699dda3022f0485fbb50c9dafe692d3921734ff * too tricky the previous thing was better for memory with lots of errors at a time but https://play.golang.org/p/RweTMRjoSCt is too much of a foot gun Change-Id: I23f0b3d77dd4288fcc20b3756a7110359576bf44
82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
// Copyright (C) 2018 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// GraphiteDest is a MetricDest that sends data with the Graphite TCP wire
|
|
// protocol
|
|
type GraphiteDest struct {
|
|
mtx sync.Mutex
|
|
address string
|
|
conn net.Conn
|
|
buf *bufio.Writer
|
|
stopped bool
|
|
}
|
|
|
|
// NewGraphiteDest creates a GraphiteDest with TCP address address. Because
|
|
// this function is called in a Lua pipeline domain-specific language, the DSL
|
|
// wants a graphite destination to be flushing every few seconds, so this
|
|
// constructor will start that process. Use Close to stop it.
|
|
func NewGraphiteDest(address string) *GraphiteDest {
|
|
rv := &GraphiteDest{address: address}
|
|
go rv.flush()
|
|
return rv
|
|
}
|
|
|
|
// Metric implements MetricDest
|
|
func (d *GraphiteDest) Metric(application, instance string,
|
|
key []byte, val float64, ts time.Time) error {
|
|
|
|
d.mtx.Lock()
|
|
defer d.mtx.Unlock()
|
|
|
|
if d.conn == nil {
|
|
conn, err := net.Dial("tcp", d.address)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.conn = conn
|
|
d.buf = bufio.NewWriter(conn)
|
|
}
|
|
|
|
_, err := fmt.Fprintf(d.buf, "%s.%s.%s %v %d\n", application, string(key),
|
|
instance, val, ts.Unix())
|
|
return err
|
|
}
|
|
|
|
// Close stops the flushing goroutine
|
|
func (d *GraphiteDest) Close() error {
|
|
d.mtx.Lock()
|
|
d.stopped = true
|
|
d.mtx.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func (d *GraphiteDest) flush() {
|
|
for {
|
|
time.Sleep(5 * time.Second)
|
|
d.mtx.Lock()
|
|
if d.stopped {
|
|
d.mtx.Unlock()
|
|
return
|
|
}
|
|
var err error
|
|
if d.buf != nil {
|
|
err = d.buf.Flush()
|
|
}
|
|
d.mtx.Unlock()
|
|
if err != nil {
|
|
log.Printf("failed flushing: %v", err)
|
|
}
|
|
}
|
|
}
|