-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
vault.go
200 lines (167 loc) · 4.64 KB
/
vault.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//go:generate ../../../tools/readme_config_includer/generator
package vault
import (
"context"
_ "embed"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
common_http "github.com/influxdata/telegraf/plugins/common/http"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
// Vault configuration object
type Vault struct {
URL string `toml:"url"`
TokenFile string `toml:"token_file"`
Token string `toml:"token"`
Log telegraf.Logger `toml:"-"`
common_http.HTTPClientConfig
client *http.Client
}
const timeLayout = "2006-01-02 15:04:05 -0700 MST"
func (*Vault) SampleConfig() string {
return sampleConfig
}
func (n *Vault) Init() error {
if n.URL == "" {
n.URL = "http://127.0.0.1:8200"
}
if n.TokenFile == "" && n.Token == "" {
return errors.New("token missing")
}
if n.TokenFile != "" && n.Token != "" {
return errors.New("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))
}
ctx := context.Background()
client, err := n.HTTPClientConfig.CreateClient(ctx, n.Log)
if err != nil {
return fmt.Errorf("creating client failed: %w", err)
}
n.client = client
return nil
}
func (n *Vault) Start(_ telegraf.Accumulator) error {
return nil
}
// Gather, collects metrics from Vault endpoint
func (n *Vault) Gather(acc telegraf.Accumulator) error {
sysMetrics, err := n.loadJSON(n.URL + "/v1/sys/metrics")
if err != nil {
return err
}
return buildVaultMetrics(acc, sysMetrics)
}
func (n *Vault) Stop() {
if n.client != nil {
n.client.CloseIdleConnections()
}
}
func (n *Vault) loadJSON(url string) (*SysMetrics, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("X-Vault-Token", n.Token)
req.Header.Add("Accept", "application/json")
resp, err := n.client.Do(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 SysMetrics
err = json.NewDecoder(resp.Body).Decode(&metrics)
if err != nil {
return nil, fmt.Errorf("error parsing json response: %w", err)
}
return &metrics, nil
}
// buildVaultMetrics, it builds all the metrics and adds them to the accumulator
func buildVaultMetrics(acc telegraf.Accumulator, sysMetrics *SysMetrics) error {
t, err := internal.ParseTimestamp(timeLayout, sysMetrics.Timestamp, nil)
if err != nil {
return fmt.Errorf("error parsing time: %w", err)
}
for _, counters := range sysMetrics.Counters {
tags := make(map[string]string)
for key, val := range counters.baseInfo.Labels {
convertedVal, err := internal.ToString(val)
if err != nil {
return fmt.Errorf("converting counter %s=%v failed: %w", key, val, err)
}
tags[key] = convertedVal
}
fields := map[string]interface{}{
"count": counters.Count,
"rate": counters.Rate,
"sum": counters.Sum,
"min": counters.Min,
"max": counters.Max,
"mean": counters.Mean,
"stddev": counters.Stddev,
}
acc.AddCounter(counters.baseInfo.Name, fields, tags, t)
}
for _, gauges := range sysMetrics.Gauges {
tags := make(map[string]string)
for key, val := range gauges.baseInfo.Labels {
convertedVal, err := internal.ToString(val)
if err != nil {
return fmt.Errorf("converting gauges %s=%v failed: %w", key, val, err)
}
tags[key] = convertedVal
}
fields := map[string]interface{}{
"value": gauges.Value,
}
acc.AddGauge(gauges.Name, fields, tags, t)
}
for _, summary := range sysMetrics.Summaries {
tags := make(map[string]string)
for key, val := range summary.baseInfo.Labels {
convertedVal, err := internal.ToString(val)
if err != nil {
return fmt.Errorf("converting summary %s=%v failed: %w", key, val, err)
}
tags[key] = convertedVal
}
fields := map[string]interface{}{
"count": summary.Count,
"rate": summary.Rate,
"sum": summary.Sum,
"stddev": summary.Stddev,
"min": summary.Min,
"max": summary.Max,
"mean": summary.Mean,
}
acc.AddCounter(summary.Name, fields, tags, t)
}
return nil
}
func init() {
inputs.Add("vault", func() telegraf.Input {
return &Vault{
HTTPClientConfig: common_http.HTTPClientConfig{
ResponseHeaderTimeout: config.Duration(5 * time.Second),
},
}
})
}