-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
telegraf.go
358 lines (301 loc) · 9.66 KB
/
telegraf.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package kv
import (
"context"
"encoding/json"
"fmt"
"github.com/influxdata/influxdb/v2"
)
var (
// ErrTelegrafNotFound is used when the telegraf configuration is not found.
ErrTelegrafNotFound = &influxdb.Error{
Msg: "telegraf configuration not found",
Code: influxdb.ENotFound,
}
// ErrInvalidTelegrafID is used when the service was provided
// an invalid ID format.
ErrInvalidTelegrafID = &influxdb.Error{
Code: influxdb.EInvalid,
Msg: "provided telegraf configuration ID has invalid format",
}
// ErrInvalidTelegrafOrgID is the error message for a missing or invalid organization ID.
ErrInvalidTelegrafOrgID = &influxdb.Error{
Code: influxdb.EEmptyValue,
Msg: "provided telegraf configuration organization ID is missing or invalid",
}
)
// UnavailableTelegrafServiceError is used if we aren't able to interact with the
// store, it means the store is not available at the moment (e.g. network).
func UnavailableTelegrafServiceError(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EInternal,
Msg: fmt.Sprintf("Unable to connect to telegraf service. Please try again; Err: %v", err),
Op: "kv/telegraf",
}
}
// InternalTelegrafServiceError is used when the error comes from an
// internal system.
func InternalTelegrafServiceError(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EInternal,
Msg: fmt.Sprintf("Unknown internal telegraf data error; Err: %v", err),
Op: "kv/telegraf",
}
}
// CorruptTelegrafError is used when the config cannot be unmarshalled from the
// bytes stored in the kv.
func CorruptTelegrafError(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EInternal,
Msg: fmt.Sprintf("Unknown internal telegraf data error; Err: %v", err),
Op: "kv/telegraf",
}
}
// ErrUnprocessableTelegraf is used when a telegraf is not able to be converted to JSON.
func ErrUnprocessableTelegraf(err error) *influxdb.Error {
return &influxdb.Error{
Code: influxdb.EUnprocessableEntity,
Msg: fmt.Sprintf("unable to convert telegraf configuration into JSON; Err %v", err),
}
}
var (
telegrafBucket = []byte("telegrafv1")
telegrafPluginsBucket = []byte("telegrafPluginsv1")
)
var _ influxdb.TelegrafConfigStore = (*Service)(nil)
func (s *Service) initializeTelegraf(ctx context.Context, tx Tx) error {
if _, err := s.telegrafBucket(tx); err != nil {
return err
}
if _, err := s.telegrafPluginsBucket(tx); err != nil {
return err
}
return nil
}
func (s *Service) telegrafBucket(tx Tx) (Bucket, error) {
b, err := tx.Bucket(telegrafBucket)
if err != nil {
return nil, UnavailableTelegrafServiceError(err)
}
return b, nil
}
func (s *Service) telegrafPluginsBucket(tx Tx) (Bucket, error) {
b, err := tx.Bucket(telegrafPluginsBucket)
if err != nil {
return nil, UnavailableTelegrafServiceError(err)
}
return b, nil
}
// FindTelegrafConfigByID returns a single telegraf config by ID.
func (s *Service) FindTelegrafConfigByID(ctx context.Context, id influxdb.ID) (*influxdb.TelegrafConfig, error) {
var (
tc *influxdb.TelegrafConfig
err error
)
err = s.kv.View(ctx, func(tx Tx) error {
tc, err = s.findTelegrafConfigByID(ctx, tx, id)
return err
})
return tc, err
}
func (s *Service) findTelegrafConfigByID(ctx context.Context, tx Tx, id influxdb.ID) (*influxdb.TelegrafConfig, error) {
encID, err := id.Encode()
if err != nil {
return nil, ErrInvalidTelegrafID
}
bucket, err := s.telegrafBucket(tx)
if err != nil {
return nil, err
}
v, err := bucket.Get(encID)
if IsNotFound(err) {
return nil, ErrTelegrafNotFound
}
if err != nil {
return nil, InternalTelegrafServiceError(err)
}
return unmarshalTelegraf(v)
}
// FindTelegrafConfigs returns a list of telegraf configs that match filter and the total count of matching telegraf configs.
// Additional options provide pagination & sorting.
func (s *Service) FindTelegrafConfigs(ctx context.Context, filter influxdb.TelegrafConfigFilter, opt ...influxdb.FindOptions) (tcs []*influxdb.TelegrafConfig, n int, err error) {
err = s.kv.View(ctx, func(tx Tx) error {
tcs, n, err = s.findTelegrafConfigs(ctx, tx, filter)
return err
})
return tcs, n, err
}
func (s *Service) findTelegrafConfigs(ctx context.Context, tx Tx, filter influxdb.TelegrafConfigFilter, opt ...influxdb.FindOptions) ([]*influxdb.TelegrafConfig, int, error) {
tcs := make([]*influxdb.TelegrafConfig, 0)
m, err := s.findUserResourceMappings(ctx, tx, filter.UserResourceMappingFilter)
if err != nil {
return nil, 0, err
}
if len(m) == 0 {
return tcs, 0, nil
}
for _, item := range m {
tc, err := s.findTelegrafConfigByID(ctx, tx, item.ResourceID)
if err == ErrTelegrafNotFound { // Stale user resource mappings are skipped
continue
}
if err != nil {
return nil, 0, InternalTelegrafServiceError(err)
}
// Restrict results by organization ID, if it has been provided
if filter.OrgID != nil && filter.OrgID.Valid() && tc.OrgID != *filter.OrgID {
continue
}
tcs = append(tcs, tc)
}
return tcs, len(tcs), nil
}
// PutTelegrafConfig put a telegraf config to storage.
func (s *Service) PutTelegrafConfig(ctx context.Context, tc *influxdb.TelegrafConfig) error {
return s.kv.Update(ctx, func(tx Tx) (err error) {
return s.putTelegrafConfig(ctx, tx, tc)
})
}
func (s *Service) putTelegrafConfig(ctx context.Context, tx Tx, tc *influxdb.TelegrafConfig) error {
encodedID, err := tc.ID.Encode()
if err != nil {
return ErrInvalidTelegrafID
}
if !tc.OrgID.Valid() {
return ErrInvalidTelegrafOrgID
}
v, err := marshalTelegraf(tc)
if err != nil {
return err
}
bucket, err := s.telegrafBucket(tx)
if err != nil {
return err
}
if err := bucket.Put(encodedID, v); err != nil {
return UnavailableTelegrafServiceError(err)
}
return s.putTelegrafConfigStats(encodedID, tx, tc)
}
func (s *Service) putTelegrafConfigStats(encodedID []byte, tx Tx, tc *influxdb.TelegrafConfig) error {
bucket, err := s.telegrafPluginsBucket(tx)
if err != nil {
return err
}
v, err := marshalTelegrafPlugins(tc.CountPlugins())
if err != nil {
return err
}
if err := bucket.Put(encodedID, v); err != nil {
return UnavailableTelegrafServiceError(err)
}
return nil
}
// CreateTelegrafConfig creates a new telegraf config and sets b.ID with the new identifier.
func (s *Service) CreateTelegrafConfig(ctx context.Context, tc *influxdb.TelegrafConfig, userID influxdb.ID) error {
return s.kv.Update(ctx, func(tx Tx) error {
return s.createTelegrafConfig(ctx, tx, tc, userID)
})
}
func (s *Service) createTelegrafConfig(ctx context.Context, tx Tx, tc *influxdb.TelegrafConfig, userID influxdb.ID) error {
tc.ID = s.IDGenerator.ID()
if err := s.putTelegrafConfig(ctx, tx, tc); err != nil {
return err
}
urm := &influxdb.UserResourceMapping{
ResourceID: tc.ID,
UserID: userID,
UserType: influxdb.Owner,
ResourceType: influxdb.TelegrafsResourceType,
}
return s.createUserResourceMapping(ctx, tx, urm)
}
// UpdateTelegrafConfig updates a single telegraf config.
// Returns the new telegraf config after update.
func (s *Service) UpdateTelegrafConfig(ctx context.Context, id influxdb.ID, tc *influxdb.TelegrafConfig, userID influxdb.ID) (*influxdb.TelegrafConfig, error) {
var err error
err = s.kv.Update(ctx, func(tx Tx) error {
tc, err = s.updateTelegrafConfig(ctx, tx, id, tc, userID)
return err
})
return tc, err
}
func (s *Service) updateTelegrafConfig(ctx context.Context, tx Tx, id influxdb.ID, tc *influxdb.TelegrafConfig, userID influxdb.ID) (*influxdb.TelegrafConfig, error) {
current, err := s.findTelegrafConfigByID(ctx, tx, id)
if err != nil {
return nil, err
}
// ID and OrganizationID can not be updated
tc.ID = current.ID
tc.OrgID = current.OrgID
err = s.putTelegrafConfig(ctx, tx, tc)
return tc, err
}
// DeleteTelegrafConfig removes a telegraf config by ID.
func (s *Service) DeleteTelegrafConfig(ctx context.Context, id influxdb.ID) error {
return s.kv.Update(ctx, func(tx Tx) error {
return s.deleteTelegrafConfig(ctx, tx, id)
})
}
func (s *Service) deleteTelegrafConfig(ctx context.Context, tx Tx, id influxdb.ID) error {
encodedID, err := id.Encode()
if err != nil {
return ErrInvalidTelegrafID
}
bucket, err := s.telegrafBucket(tx)
if err != nil {
return err
}
_, err = bucket.Get(encodedID)
if IsNotFound(err) {
return ErrTelegrafNotFound
}
if err != nil {
return InternalTelegrafServiceError(err)
}
if err := bucket.Delete(encodedID); err != nil {
return UnavailableTelegrafServiceError(err)
}
if err := s.deleteUserResourceMappings(ctx, tx, influxdb.UserResourceMappingFilter{
ResourceID: id,
ResourceType: influxdb.TelegrafsResourceType,
}); err != nil {
return err
}
return s.deleteTelegrafConfigStats(encodedID, tx)
}
func (s *Service) deleteTelegrafConfigStats(encodedID []byte, tx Tx) error {
bucket, err := s.telegrafPluginsBucket(tx)
if err != nil {
return err
}
if err := bucket.Delete(encodedID); err != nil {
return &influxdb.Error{
Code: influxdb.EInternal,
Msg: fmt.Sprintf("Unable to connect to telegraf config stats service. Please try again; Err: %v", err),
Op: "kv/telegraf",
}
}
return nil
}
// unmarshalTelegraf turns the stored byte slice in the kv into a *influxdb.TelegrafConfig.
func unmarshalTelegraf(v []byte) (*influxdb.TelegrafConfig, error) {
t := &influxdb.TelegrafConfig{}
if err := json.Unmarshal(v, t); err != nil {
return nil, CorruptTelegrafError(err)
}
return t, nil
}
func marshalTelegraf(tc *influxdb.TelegrafConfig) ([]byte, error) {
v, err := json.Marshal(tc)
if err != nil {
return nil, ErrUnprocessableTelegraf(err)
}
return v, nil
}
func marshalTelegrafPlugins(plugins map[string]float64) ([]byte, error) {
v, err := json.Marshal(plugins)
if err != nil {
return nil, ErrUnprocessableTelegraf(err)
}
return v, nil
}