/
stats.go
47 lines (40 loc) · 1.4 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Package stats uses uber-go/tally for reporting hierarchical stats. Tally
// supports multiple sinks including prometheus, m3, and statsd. xds-relay
// currently defaults to statsd, but this will be made configurable in the
// future to support the other sink alternatives.
package stats
import (
"io"
"time"
"github.com/cactus/go-statsd-client/statsd"
"github.com/uber-go/tally"
)
// Config holds the configuration options for stats reporting.
type Config struct {
// StatsdAddress that the statsd sink is running on, with format addr:port.
StatsdAddress string
// RootPrefix is the prefix for the root scope.
RootPrefix string
// The maximum interval for packet sending. If set to 0 it defaults to 300ms.
FlushInterval time.Duration
}
// NewRootScope creates a new root Scope with the set of configured options and
// statsd reporter.
func NewRootScope(config Config) (tally.Scope, io.Closer, error) {
// Configure statsd client for reporting stats.
statsdClient, err := statsd.NewClientWithConfig(&statsd.ClientConfig{
Address: config.StatsdAddress,
UseBuffered: true,
FlushInterval: config.FlushInterval,
})
if err != nil {
return nil, nil, err
}
reporter := NewStatsdPointTagsReporter(statsdClient)
scope, closer := tally.NewRootScope(tally.ScopeOptions{
Prefix: config.RootPrefix,
Tags: map[string]string{},
Reporter: reporter,
}, time.Second)
return scope, closer, nil
}