/
provider.go
373 lines (358 loc) · 13 KB
/
provider.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package v1
import (
"net/http"
"facette.io/facette/storage"
"facette.io/httputil"
"facette.io/sqlstorage"
"github.com/vbatoufflet/httprouter"
)
// api:section providers "Providers"
//
// ## Providers Connectors Settings
//
// Catalog providers can be configured with settings and filters:
//
// ### Facette
//
// | Name | Type | Description |
// | --- | --- | --- |
// | `url`<br>__required__ | string | URL of the upstream Facette instance (without the `/api` path) |
// | `timeout` | integer | delay in seconds before declaring a timeout (default: `10`) |
// | `allow_insecure_tls` | boolean | allow invalid or expired SSL certificates when accessing the Facette API through HTTPS (default: `false`) |
//
// ### Graphite
//
// | Name | Type | Description |
// | --- | --- | --- |
// | `url`<br>__required__ | string | URL of the Graphite webapp (without the `/api` path) |
// | `pattern`<br>__required__ | string | regular expression (RE2 syntax) describing the pattern mapping sources/metrics to the metrics series names. `<source>` and `<metric>` regexp named group are mandatory to effectively map a series name to these objects |
// | `timeout` | integer | delay in seconds before declaring a timeout (default: `10`) |
// | `allow_insecure_tls` | boolean | allow invalid or expired SSL certificates when accessing the Graphite API through HTTPS (default: `false`) |
//
// ### InfluxDB
//
// | Name | Type | Description |
// | --- | --- | --- |
// | `url`<br>__required__ | string | URL of the InfluxDB instance |
// | `database`<br>__required__ | string | InfluxDB database to query series from |
// | `pattern` | string | regular expression (RE2 syntax) describing the pattern mapping *sources*/*metrics* to the measurements. `<source>` and `<metric>` regexp named group are mandatory to effectively map a measurement to these objects |
// | `mapping` | object | measurements and columns to map the objects on (see _Mapping parameters_ below) |
// | `username` | string | username to connect to the database (default: _empty_) |
// | `password` | string | password to connect to the database (default: _empty_) |
// | `timeout` | integer | delay in seconds before declaring a timeout (default: `10`) |
// | `allow_insecure_tls` | boolean | allow invalid or expired SSL certificates when accessing the InfluxDB API through HTTPS (default: `false`) |
//
// Mapping parameters:
//
// | Name | Type | Description |
// | --- | --- | --- |
// | `source` | array of strings | list of columns entries to map *sources* on |
// | `metric` | array of strings | list of columns entries to map *metrics* on |
// | `glue` | string | separator used to join defined columns |
//
// ```javascript
// {
// "source": ["column:host"],
// "metric": ["name", "column:instance", "column:type", "column:type_instance"],
// "glue": "."
// }
// ```
//
// Note: you should either use `pattern` or `mapping`, but not both.
//
// ### KairosDB
//
// | Name | Type | Description |
// | --- | --- | --- |
// | `url`<br>__required__ | string | URL of the KairosDB instance (without the `/api` path) |
// | `aggregators` | array of strings | KairosDB [aggregators](http://kairosdb.github.io/docs/build/html/restapi/Aggregators.html) to use for sampling (default: `["avg","max","min"]`) |
// | `source_tags` | array of strings | KairosDB [tags](http://kairosdb.github.io/docs/build/html/restapi/QueryMetricTags.html) to look into for sources (default: `["host","server","device"]`) |
// | `timeout` | integer | delay in seconds before declaring a timeout (default: `10`) |
// | `allow_insecure_tls` | boolean | allow invalid or expired SSL certificates when accessing the Facette API through HTTPS (default: `false`) |
//
// ### RRDtool
//
// | Name | Type | Description |
// | --- | --- | --- |
// | `path`<br>__required__ | string | base path on the local filesystem where the RRDtool files are stored |
// | `pattern`<br>__required__ | string | regular expression (RE2 syntax) describing the pattern mapping *sources*/*metrics* to the filesystem structure under the base directory defined with the `path` setting. `<source>` and `<metric>` regexp named group are mandatory to effectively map a filesystem path to these objects |
// | `daemon` | string | rrdcached daemon socket address, see `-l` option in `rrdcached(1)` manual for details |
//
// ## Provider Filters
//
// Provider filters allow changing how _sources_ and _metrics_ appear in the catalog, and discard the ones you don’t
// want to deal with. Filter rule format:
//
// ```javascript
// {
// "action": "<action to perform on record (discard|rewrite|sieve)>",
// "target": "<record field to match (any|metric|source)>",
// "pattern": "<regular expression pattern>"
// "into": "<replacement value (for \"rewrite\" action)>"
// }
// ```
//
// Note: regular expressions must follow the [RE2 syntax](https://github.com/google/re2).
// api:method POST /api/v1/providers "Create a provider"
//
// This endpoint creates a new catalog provider. Required fields:
//
// * `name` (type _string_): provider name
// * `connector` (type _string_): provider connector type, see `facette -V` output to find which connectors are supported
//
// Optional fields:
//
// * `description` (type _string_): a description for the provider
// * `settings` (type _object_): connector settings
// * `filters` (type _array of objects_): list of provider filters
// * `priority` (type _integer_): in case multiple providers expose the same metric, the provider with higher priority wins (default: `0`)
// * `refresh_internal` (type _integer_): interval (in seconds) to trigger a periodic refresh of the provider (default: `0`, no refresh)
//
// Caution: in JSON you need to double the escaping character `\` when writing regular expressions (e.g. `\d` → `\\d`).
//
// ---
// section: providers
// request:
// type: object
// examples:
// - format: javascript
// headers:
// Content-Type: application/json
// body: |
// {
// "name": "graphite",
// "description": "Metrics from Graphite",
// "connector": "graphite",
// "settings": {
// "url": "graphite.example.net:8080",
// "pattern": "(?P<source>[^\\\\.]+)\\\\.(?P<metric>.+)"
// },
// "refresh_interval": 3600,
// "filters": [
// {
// "action": "rewrite",
// "target": "source",
// "pattern": "_",
// "into": "."
// }
// ]
// }
// responses:
// 201:
func (a *API) providerCreate(rw http.ResponseWriter, r *http.Request) {
a.storageCreate(rw, httprouter.SetContextParam(r, "type", "providers"))
}
// api:method GET /api/v1/providers/:id "Get a provider"
//
// This endpoint returns a provider given its identifier.
//
// ---
// section: providers
// parameters:
// - name: id
// type: string
// description: identifier of the provider
// required: true
// in: path
// responses:
// 200:
// type: object
// examples:
// - format: javascript
// body: |
// {
// "id": "4654e374-70e8-5621-afab-ac9c1ff91261",
// "name": "kairosdb",
// "description": null,
// "created": "2017-05-11T12:51:07Z",
// "modified": "2017-05-22T07:59:57Z",
// "connector": "kairosdb",
// "settings": {
// "aggregators": [
// "avg"
// ],
// "url": "http://kairosdb.example.net:8080"
// },
// "filters": [
// {
// "action": "discard",
// "target": "metric",
// "pattern": "^kairosdb\\.",
// "into": ""
// },
// {
// "action": "rewrite",
// "target": "source",
// "pattern": "_",
// "into": "."
// },
// {
// "action": "rewrite",
// "target": "metric",
// "pattern": "/avg$",
// "into": ""
// },
// {
// "action": "rewrite",
// "target": "metric",
// "pattern": "\\.value$",
// "into": ""
// },
// {
// "action": "rewrite",
// "target": "metric",
// "pattern": "^(.+)\\.(?:derive|gauge|latency)\\.(.+)",
// "into": "$1.$2"
// }
// ],
// "refresh_interval": 900,
// "priority": 0,
// "enabled": true
// }
func (a *API) providerGet(rw http.ResponseWriter, r *http.Request) {
a.storageGet(rw, httprouter.SetContextParam(r, "type", "providers"))
}
// api:method PUT /api/v1/providers/:id "Update a provider"
//
// This endpoint updates a provider given its identifier. The request body is similar to the _Create a new catalog
// provider_ endpoint.
//
// If the instance is *read-only* the operation will be rejected with `403 Forbidden`.
//
// ---
// section: providers
// parameters:
// - name: id
// type: string
// description: identifier of the provider
// required: true
// in: path
// responses:
// 204:
// api:method PATCH /api/v1/providers/:id "Partially update a provider"
//
// This endpoint partially updates a provider given its identifier. The request body is similar to the _Update a
// provider_ endpoint, but only specified fields will be modified.
//
// If the instance is *read-only* the operation will be rejected with `403 Forbidden`.
//
// ---
// section: providers
// parameters:
// - name: id
// type: string
// description: identifier of the provider
// required: true
// in: path
// responses:
// 204:
func (a *API) providerUpdate(rw http.ResponseWriter, r *http.Request) {
a.storageUpdate(rw, httprouter.SetContextParam(r, "type", "providers"))
}
// api:method DELETE /api/v1/providers/:id "Delete a provider"
//
// This endpoint deletes a provider given its identifier.
//
// If the instance is *read-only* the operation will be rejected with `403 Forbidden`.
//
// ---
// section: providers
// parameters:
// - name: id
// type: string
// description: identifier of the provider
// required: true
// in: path
// responses:
// 204:
func (a *API) providerDelete(rw http.ResponseWriter, r *http.Request) {
a.storageDelete(rw, httprouter.SetContextParam(r, "type", "providers"))
}
// api:method DELETE /api/v1/providers "Delete all providers"
//
// This endpoint deletes all providers.
//
// If the request header `X-Confirm-Action` is not present or if the instance is *read-only* the operation will be
// rejected with `403 Forbidden`.
//
// ---
// section: providers
// request:
// headers:
// X-Confirm-Action: action confirmation flag
// responses:
// 204:
func (a *API) providerDeleteAll(rw http.ResponseWriter, r *http.Request) {
a.storageDeleteAll(rw, httprouter.SetContextParam(r, "type", "providers"))
}
// api:method GET /api/v1/providers "List providers"
//
// This endpoint returns providers. If a `filter` query parameter is given, only providers having
// their name matching the filter will be returned.
//
// This endpoint supports pagination through the `offset` and `limit` query parameters and sorting using `sort` query
// parameter (separated by commas; prefix field name with "-" to reverse sort order).
//
// ---
// section: providers
// parameters:
// - name: filter
// type: string
// description: term to filter names on
// in: query
// - name: sort
// type: string
// description: fields to sort results on
// in: query
// - name: offset
// type: integer
// description: offset to return providers from
// in: query
// - name: limit
// type: integer
// description: number of providers to return
// in: query
// responses:
// 200:
// type: array
// headers:
// X-Total-Records: total number of catalog records for this type
// examples:
// - format: javascript
// body: |
// [
// {
// "created": "2017-06-14T06:09:19Z",
// "description": null,
// "enabled": true,
// "id": "e91ac07e-5f74-5845-6a09-4903ecd30995",
// "modified": "2017-06-14T06:12:57Z",
// "name": "collectd"
// }
// ]
func (a *API) providerList(rw http.ResponseWriter, r *http.Request) {
a.storageList(rw, httprouter.SetContextParam(r, "type", "providers"))
}
// api:method POST /api/v1/providers/:id/refresh "Refresh a provider"
//
// This endpoint triggers a catalog refresh for a provider given its identifier.
//
// ---
// section: providers
// responses:
// 204:
func (a *API) providerRefresh(rw http.ResponseWriter, r *http.Request) {
id := httprouter.ContextParam(r, "id").(string)
provider := storage.Provider{}
// Request item from storage
if err := a.storage.SQL().Get("id", id, &provider, false); err == sqlstorage.ErrItemNotFound {
httputil.WriteJSON(rw, newMessage(err), http.StatusNotFound)
return
} else if err != nil {
a.logger.Error("failed to fetch item: %s", err)
httputil.WriteJSON(rw, newMessage(errUnhandledError), http.StatusInternalServerError)
return
}
a.poller.Refresh(provider)
httputil.WriteJSON(rw, nil, http.StatusNoContent)
}