forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsul_agent.go
175 lines (140 loc) · 3.92 KB
/
consul_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//go:generate ../../../tools/readme_config_includer/generator
package consul_agent
import (
_ "embed"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
const timeLayout = "2006-01-02 15:04:05 -0700 MST"
type ConsulAgent struct {
URL string `toml:"url"`
TokenFile string `toml:"token_file"`
Token string `toml:"token"`
ResponseTimeout config.Duration `toml:"timeout"`
tls.ClientConfig
roundTripper http.RoundTripper
}
func (*ConsulAgent) SampleConfig() string {
return sampleConfig
}
func (n *ConsulAgent) Init() error {
if n.URL == "" {
n.URL = "http://127.0.0.1:8500"
}
if n.TokenFile != "" && n.Token != "" {
return errors.New("config error: both token_file and token are set")
}
if n.TokenFile != "" {
token, err := os.ReadFile(n.TokenFile)
if err != nil {
return fmt.Errorf("reading file failed: %w", err)
}
n.Token = strings.TrimSpace(string(token))
}
tlsCfg, err := n.ClientConfig.TLSConfig()
if err != nil {
return fmt.Errorf("setting up TLS configuration failed: %w", err)
}
n.roundTripper = &http.Transport{
TLSHandshakeTimeout: time.Duration(n.ResponseTimeout),
TLSClientConfig: tlsCfg,
ResponseHeaderTimeout: time.Duration(n.ResponseTimeout),
}
return nil
}
func (n *ConsulAgent) Gather(acc telegraf.Accumulator) error {
summaryMetrics, err := n.loadJSON(n.URL + "/v1/agent/metrics")
if err != nil {
return err
}
return buildConsulAgent(acc, summaryMetrics)
}
func (n *ConsulAgent) loadJSON(url string) (*agentInfo, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("X-Consul-Token", n.Token)
req.Header.Add("Accept", "application/json")
resp, err := n.roundTripper.RoundTrip(req)
if err != nil {
return nil, fmt.Errorf("error making HTTP request to %q: %w", url, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s returned HTTP status %s", url, resp.Status)
}
var metrics agentInfo
err = json.NewDecoder(resp.Body).Decode(&metrics)
if err != nil {
return nil, fmt.Errorf("error parsing json response: %w", err)
}
return &metrics, nil
}
// buildConsulAgent, it builds all the metrics and adds them to the accumulator)
func buildConsulAgent(acc telegraf.Accumulator, agentInfo *agentInfo) error {
t, err := internal.ParseTimestamp(timeLayout, agentInfo.Timestamp, nil)
if err != nil {
return fmt.Errorf("error parsing time: %w", err)
}
for _, counters := range agentInfo.Counters {
fields := map[string]interface{}{
"count": counters.Count,
"sum": counters.Sum,
"max": counters.Max,
"mean": counters.Mean,
"min": counters.Min,
"rate": counters.Rate,
"stddev": counters.Stddev,
}
tags := counters.Labels
acc.AddCounter(counters.Name, fields, tags, t)
}
for _, gauges := range agentInfo.Gauges {
fields := map[string]interface{}{
"value": gauges.Value,
}
tags := gauges.Labels
acc.AddGauge(gauges.Name, fields, tags, t)
}
for _, points := range agentInfo.Points {
fields := map[string]interface{}{
"value": points.Points,
}
tags := make(map[string]string)
acc.AddFields(points.Name, fields, tags, t)
}
for _, samples := range agentInfo.Samples {
fields := map[string]interface{}{
"count": samples.Count,
"sum": samples.Sum,
"max": samples.Max,
"mean": samples.Mean,
"min": samples.Min,
"rate": samples.Rate,
"stddev": samples.Stddev,
}
tags := samples.Labels
acc.AddCounter(samples.Name, fields, tags, t)
}
return nil
}
func init() {
inputs.Add("consul_agent", func() telegraf.Input {
return &ConsulAgent{
ResponseTimeout: config.Duration(5 * time.Second),
}
})
}