1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:34:45 -07:00

internal/event: re-use ocagent exporters

This attempts to detect a Connect call with the same configuration and return
the same exporter.
This mostly affects tests where we end up starting a new ticking go-routine per
test, even though they all have the same configuration. It was noticable that
a goroutine dump of a test would have a very large number of go routines that
were just ticking exporters, making it very hard to see the real work.

Change-Id: Ie8b64b69fce736d2bf3dbadffc1681f7caee6dd8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/230463
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Ian Cottrell 2020-04-27 15:41:24 -04:00
parent 28a3422bd6
commit 4b814e0613

View File

@ -37,6 +37,11 @@ type Config struct {
Rate time.Duration
}
var (
connectMu sync.Mutex
exporters = make(map[Config]*Exporter)
)
// Discover finds the local agent to export to, it will return nil if there
// is not one running.
// TODO: Actually implement a discovery protocol rather than a hard coded address
@ -60,26 +65,34 @@ func Connect(config *Config) *Exporter {
if config == nil || config.Address == "off" {
return nil
}
exporter := &Exporter{config: *config}
if exporter.config.Start.IsZero() {
exporter.config.Start = time.Now()
resolved := *config
if resolved.Start.IsZero() {
resolved.Start = time.Now()
}
if exporter.config.Host == "" {
if resolved.Host == "" {
hostname, _ := os.Hostname()
exporter.config.Host = hostname
resolved.Host = hostname
}
if exporter.config.Process == 0 {
exporter.config.Process = uint32(os.Getpid())
if resolved.Process == 0 {
resolved.Process = uint32(os.Getpid())
}
if exporter.config.Client == nil {
exporter.config.Client = http.DefaultClient
if resolved.Client == nil {
resolved.Client = http.DefaultClient
}
if exporter.config.Service == "" {
exporter.config.Service = filepath.Base(os.Args[0])
if resolved.Service == "" {
resolved.Service = filepath.Base(os.Args[0])
}
if exporter.config.Rate == 0 {
exporter.config.Rate = 2 * time.Second
if resolved.Rate == 0 {
resolved.Rate = 2 * time.Second
}
connectMu.Lock()
defer connectMu.Unlock()
if exporter, found := exporters[resolved]; found {
return exporter
}
exporter := &Exporter{config: resolved}
exporters[resolved] = exporter
go func() {
for range time.Tick(exporter.config.Rate) {
exporter.Flush()