forked from sirkjohannsen/grafana-datasource-datadog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.js
433 lines (381 loc) · 13.5 KB
/
datasource.js
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
'use strict';
System.register(['lodash', './showdown.min.js', './query_builder'], function (_export, _context) {
"use strict";
var _, showdown, queryBuilder, _createClass, DataDogDatasource;
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
/*
* Convert tags to key-value pairs
* [region:east, region:nw] => {region: [east, nw]}
*/
function mapTagsToKVPairs(tags) {
var kv_tags = _.filter(tags, function (tag) {
return tag.indexOf(':') !== -1;
});
var kv_pairs = kv_tags.map(function (tag) {
return tag.split(':', 2); // Limit to 2
});
var kv_object = {};
kv_pairs.forEach(function (pair) {
var key = pair[0];
var val = pair[1];
if (kv_object[key]) {
kv_object[key].push(val);
} else {
kv_object[key] = [val];
}
});
return kv_object;
}
/*
* Convert DataDog event text from markdown to pure HTML
* http://docs.datadoghq.com/guides/markdown/
*/
function convertDataDogMdToHtml(str) {
var MD_START = '%%%\n';
var MD_END = '\n%%%';
var md_start_index = str.indexOf(MD_START) + MD_START.length;
var md_end_index = str.indexOf(MD_END);
var md = str.substring(md_start_index, md_end_index);
md = removeImagesFromDataDogMarkdown(md);
var converter = new showdown.Converter();
return converter.makeHtml(md);
}
function isDataDogMarkdown(str) {
var MD_START = '%%%\n';
return str.indexOf(MD_START) >= 0;
}
function removeImagesFromDataDogMarkdown(str) {
var dataDogImagePattern = /\[{0,1}\!\[.*\]\(https{0,1}\:\/\/p\.datadoghq\.com\/snapshot.+\)/g;
return str.replace(dataDogImagePattern, '');
}
function dataDogTagFormat(value) {
if (_.isArray(value)) {
return value.join(',');
}
return value;
}
return {
setters: [function (_lodash) {
_ = _lodash.default;
}, function (_showdownMinJs) {
showdown = _showdownMinJs.default;
}, function (_query_builder) {
queryBuilder = _query_builder;
}],
execute: function () {
_createClass = function () {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || false;
descriptor.configurable = true;
if ("value" in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function (Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
}();
_export('DataDogDatasource', DataDogDatasource = function () {
function DataDogDatasource(instanceSettings, backendSrv, templateSrv) {
_classCallCheck(this, DataDogDatasource);
this.type = instanceSettings.type;
this.url = instanceSettings.url;
this.name = instanceSettings.name;
this.api_key = instanceSettings.jsonData.api_key;
this.application_key = instanceSettings.jsonData.app_key;
this.supportMetrics = true;
this.backendSrv = backendSrv;
this.templateSrv = templateSrv;
this._cached_metrics = false;
}
// Function to check Datasource health
_createClass(DataDogDatasource, [{
key: 'testDatasource',
value: function testDatasource() {
return this.invokeDataDogApiRequest('/downtime').then(function () {
return {
status: "success",
title: "Success",
message: "Data source is working"
};
}).catch(function (error) {
var message = "Connection error";
if (error && error.message) {
message = error.message;
}
return {
status: "error",
title: "Error",
message: message
};
});
}
}, {
key: 'tagFindQuery',
value: function tagFindQuery() {
return this.getTagsFromCache().then(function (tags) {
return _.map(tags, function (hosts, tag) {
return {
text: tag,
value: tag
};
});
});
}
}, {
key: 'metricFindQuery',
value: function metricFindQuery(query) {
var _this = this;
if (query === 'tag') {
return this.tagFindQuery();
}
if (this._cached_metrics) {
return Promise.resolve(this._cached_metrics);
}
if (this.fetching) {
return this.fetching;
}
var d = new Date();
d.setDate(d.getDate() - 1);
var from = Math.floor(d.getTime() / 1000);
this.fetching = this.getMetrics(from).then(function (metrics) {
_this._cached_metrics = _.map(metrics, function (metric) {
return {
text: metric,
value: metric
};
});
return _this._cached_metrics;
});
return this.fetching;
}
}, {
key: 'getTagKeys',
value: function getTagKeys() {
return this.getTagsFromCache().then(function (tagsHosts) {
var tags = Object.keys(tagsHosts);
var kv = mapTagsToKVPairs(tags);
var grafanaTags = Object.keys(kv);
return grafanaTags.map(function (tag) {
return {
text: tag,
value: tag
};
});
});
}
}, {
key: 'getTagValues',
value: function getTagValues(options) {
return this.getTagsFromCache().then(function (tagsHosts) {
var tags = Object.keys(tagsHosts);
var kv = mapTagsToKVPairs(tags);
var grafanaValues = kv[options.key];
return grafanaValues.map(function (val) {
return {
text: val,
value: val
};
});
});
}
}, {
key: 'query',
value: function query(options) {
var from = Math.floor(options.range.from.valueOf() / 1000);
var to = Math.floor(options.range.to.valueOf() / 1000);
var targets = options.targets.filter(function (t) {
return !t.hide;
});
if (targets.length <= 0) {
return Promise.resolve({ data: [] });
}
// add global adhoc filters
var adhocFilters = this.templateSrv.getAdhocFilters(this.name);
var queries = _.map(targets, function (target) {
var query = void 0;
if (target.rawQuery) {
query = target.query;
} else {
query = queryBuilder.buildQuery(target, adhocFilters);
}
return query;
});
var queryString = queries.join(',');
queryString = this.templateSrv.replace(queryString, options.scopedVars, dataDogTagFormat);
var params = {
from: from,
to: to,
query: queryString
};
return this.invokeDataDogApiRequest('/query', params).then(function (result) {
var dataResponse = _.map(result.series, function (series, i) {
var target = targets[i];
return {
'target': target.alias || series.expression,
'datapoints': _.map(series.pointlist, function (point) {
return [point[1], point[0]];
})
};
});
return { data: dataResponse };
});
}
}, {
key: 'annotationQuery',
value: function annotationQuery(options) {
var timeFrom = Math.floor(options.range.from.valueOf() / 1000);
var timeTo = Math.floor(options.range.to.valueOf() / 1000);
var _options$annotation = options.annotation,
priority = _options$annotation.priority,
sources = _options$annotation.sources,
tags = _options$annotation.tags;
return this.getEventStream(timeFrom, timeTo, priority, sources, tags).then(function (eventStreams) {
var eventAnnotations = eventStreams.map(function (eventStream) {
var allEvents = eventStream.children;
var filteredEvents = _.filter(allEvents, function (event) {
return event.alert_type !== 'success';
});
return _.map(filteredEvents, function (event) {
var renderedText = eventStream.text;
if (isDataDogMarkdown(eventStream.text)) {
renderedText = convertDataDogMdToHtml(eventStream.text);
}
return {
annotation: options.annotation,
time: event.date_happened * 1000,
title: eventStream.title,
text: renderedText,
tags: eventStream.tags
};
});
});
return _.flatten(eventAnnotations);
});
}
}, {
key: 'getHosts',
value: function getHosts() {
return this.searchEntities('hosts');
}
}, {
key: 'searchEntities',
value: function searchEntities(entity) {
var params = { q: '' };
if (entity) {
params.q = entity + ':';
}
return this.invokeDataDogApiRequest('/search', params).then(function (result) {
if (result && result.results) {
return result.results[entity];
}
});
}
}, {
key: 'getMetrics',
value: function getMetrics(timeFrom) {
var params = {};
if (timeFrom) {
params.from = timeFrom;
}
return this.invokeDataDogApiRequest('/metrics', params).then(function (result) {
if (result.metrics) {
return result.metrics;
} else {
return [];
}
});
}
}, {
key: 'getTagsHosts',
value: function getTagsHosts() {
return this.invokeDataDogApiRequest('/tags/hosts').then(function (result) {
if (result && result.tags) {
return result.tags;
}
});
}
}, {
key: 'getTagsFromCache',
value: function getTagsFromCache() {
var _this2 = this;
var getTags = void 0;
if (this._cached_tags && this._cached_tags.length) {
getTags = Promise.resolve(this._cached_tags);
} else {
getTags = this.getTagsHosts().then(function (tags) {
_this2._cached_tags = tags;
return tags;
});
}
return getTags;
}
}, {
key: 'getEventStream',
value: function getEventStream(timeFrom, timeTo, priority, sources, tags) {
var params = {
start: timeFrom,
end: timeTo
};
if (priority) {
params.priority = priority;
}
if (sources) {
params.sources = sources;
}
if (tags) {
params.tags = tags;
}
return this.invokeDataDogApiRequest('/events', params).then(function (result) {
if (result.events) {
return result.events;
} else {
return [];
}
});
}
}, {
key: 'invokeDataDogApiRequest',
value: function invokeDataDogApiRequest(url) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
// Set auth params
params.api_key = this.api_key;
params.application_key = this.application_key;
return this.backendSrv.datasourceRequest({
method: 'GET',
url: this.url + url,
params: params
}).then(function (response) {
if (response.data) {
return response.data;
} else {
throw { message: 'DataDog API request error' };
}
}).catch(function (error) {
var message = 'DataDog API request error';
if (error.statusText) {
message = error.status + ' ' + error.statusText;
throw { message: message };
} else if (error.err.statusText) {
throw { message: error.err.statusText };
} else {
throw { message: message };
}
});
}
}]);
return DataDogDatasource;
}());
_export('DataDogDatasource', DataDogDatasource);
}
};
});
//# sourceMappingURL=datasource.js.map