/
jolokia2_agent.go
97 lines (77 loc) · 2.26 KB
/
jolokia2_agent.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//go:generate ../../../../tools/readme_config_includer/generator
package jolokia2_agent
import (
_ "embed"
"fmt"
"sync"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs/jolokia2/common"
)
// DO NOT REMOVE THE NEXT TWO LINES! This is required to embed the sampleConfig data.
//go:embed sample.conf
var sampleConfig string
type JolokiaAgent struct {
DefaultFieldPrefix string
DefaultFieldSeparator string
DefaultTagPrefix string
URLs []string `toml:"urls"`
Username string
Password string
ResponseTimeout config.Duration `toml:"response_timeout"`
tls.ClientConfig
Metrics []common.MetricConfig `toml:"metric"`
gatherer *common.Gatherer
clients []*common.Client
}
func (*JolokiaAgent) SampleConfig() string {
return sampleConfig
}
func (ja *JolokiaAgent) Gather(acc telegraf.Accumulator) error {
if ja.gatherer == nil {
ja.gatherer = common.NewGatherer(ja.createMetrics())
}
// Initialize clients once
if ja.clients == nil {
ja.clients = make([]*common.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 _, client := range ja.clients {
wg.Add(1)
go func(client *common.Client) {
defer wg.Done()
err := ja.gatherer.Gather(client, acc)
if err != nil {
acc.AddError(fmt.Errorf("unable to gather metrics for %s: %v", client.URL, err))
}
}(client)
}
wg.Wait()
return nil
}
func (ja *JolokiaAgent) createMetrics() []common.Metric {
var metrics []common.Metric
for _, metricConfig := range ja.Metrics {
metrics = append(metrics, common.NewMetric(metricConfig,
ja.DefaultFieldPrefix, ja.DefaultFieldSeparator, ja.DefaultTagPrefix))
}
return metrics
}
func (ja *JolokiaAgent) createClient(url string) (*common.Client, error) {
return common.NewClient(url, &common.ClientConfig{
Username: ja.Username,
Password: ja.Password,
ResponseTimeout: time.Duration(ja.ResponseTimeout),
ClientConfig: ja.ClientConfig,
})
}