Reuse transport on next interval in jolokia agent (#4137)

(cherry picked from commit 5030373a4c)
This commit is contained in:
Daniel Nelson 2018-05-11 17:48:27 -07:00 committed by Daniel Nelson
parent 649d840441
commit 1b2a9f12eb
No known key found for this signature in database
GPG Key ID: CAAD59C9444F6155
1 changed files with 16 additions and 8 deletions

View File

@ -25,6 +25,7 @@ type JolokiaAgent struct {
Metrics []MetricConfig `toml:"metric"`
gatherer *Gatherer
clients []*Client
}
func (ja *JolokiaAgent) SampleConfig() string {
@ -62,20 +63,27 @@ func (ja *JolokiaAgent) Gather(acc telegraf.Accumulator) error {
ja.gatherer = NewGatherer(ja.createMetrics())
}
// Initialize clients once
if ja.clients == nil {
ja.clients = make([]*Client, 0, len(ja.URLs))
for _, url := range ja.URLs {
client, err := ja.createClient(url)
if err != nil {
acc.AddError(fmt.Errorf("Unable to create client for %s: %v", url, err))
continue
}
ja.clients = append(ja.clients, client)
}
}
var wg sync.WaitGroup
for _, url := range ja.URLs {
client, err := ja.createClient(url)
if err != nil {
acc.AddError(fmt.Errorf("Unable to create client for %s: %v", url, err))
continue
}
for _, client := range ja.clients {
wg.Add(1)
go func(client *Client) {
defer wg.Done()
err = ja.gatherer.Gather(client, acc)
err := ja.gatherer.Gather(client, acc)
if err != nil {
acc.AddError(fmt.Errorf("Unable to gather metrics for %s: %v", client.URL, err))
}