Add a functional option for periodically reporting client statistics
Same as the buffer-flushing one: This allows setting up the escape
hatch at client creation.
@@ -40,6 +40,7 @@ type Client struct {
cap uint
cancel context.CancelFunc
flush func(context.Context)
+ report func(context.Context)
ops chan op
failedFlushes int64
@@ -65,6 +66,9 @@ func (c *Client) run(ctx context.Context) {
if c.flush != nil {
go c.flush(ctx)
}
+ if c.report != nil {
+ go c.report(ctx)
+ }
for {
do, ok := <-c.ops
if !ok {
@@ -208,6 +212,27 @@ func ConnectTimeout(t time.Duration) ClientParam {
}
}
+// ReportStatistics sets up a goroutine that periodically (at
+// interval) sends statistics about backpressure experienced on the
+// client to a statsd server.
+func ReportStatistics(stats *statsd.Client, interval time.Duration, tags []string) ClientParam {
+ return func(cl *Client) error {
+ ticker := time.NewTicker(interval)
+ cl.report = func(ctx context.Context) {
+ defer ticker.Stop()
+ for {
+ select {
+ case <-ticker.C:
+ SendClientStatistics(cl, stats, tags)
+ case <-ctx.Done():
+ return
+ }
+ }
+ }
+ return nil
+ }
+}
+
// NewClient constructs a new client that will attempt to connect
// to addrStr (an address in veneur URL format) using the parameters
// in opts. It returns the constructed client or an error.
