-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
159 lines (138 loc) · 4.94 KB
/
index.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
/**
* @module datadog-events
* @description Send events to DataDog **without** DogStatsD or StatsD
* @author Kyle Ross
*/
"use strict";
const axios = require('axios');
const isError = require('is-error');
/**
* @class DataDogEvents
*/
class DataDogEvents {
/**
* Creates an instance of DataDogEvents.
* @param {?Object} [options={}] Options to configure DataDogEvents
*
* @memberOf DataDogEvents
*/
constructor(options = {}) {
this.options = Object.assign({
apiKey: process.env.DATADOG_API_KEY || null,
domain: process.env.DATADOG_DOMAIN || 'datadoghq.com',
titlePrefix: null,
bodyPrefix: null,
bodyPostfix: null,
priority: 'normal',
host: null,
tags: [],
aggregationKey: null,
sourceType: null,
markdown: true
}, options || {});
if(!this.options.apiKey)
throw new Error('DataDog API key was not set');
['error', 'warning', 'info', 'success'].forEach(type => {
this[type] = (title, body, options = {}) => {
return this.sendEvent(type, title, body, options);
};
});
}
/**
* Sends an event to DataDog.
*
* @param {String} type The alert type to send (can be `error`, `warning`, `info` or `success`).
* @param {String} title The title of the event.
* @param {String} body The body of the event which may contain markdown.
* @param {?Object} [options={}] Additional options to configure the event.
* @returns {Promise}
*
* @memberOf DataDogEvents
*/
sendEvent(type, title, body, options = {}) {
options = Object.assign(options || {}, { title, body, type });
let data = this._prepareParams(options);
return new Promise((resolve, reject) => {
axios.request({
method: 'post',
url: `https://app.${this.options.domain}/api/v1/events`,
params: {
api_key: this.options.apiKey
},
data
}).then(resp => {
resolve(resp.data);
}).catch(err => {
if(err.response) {
let status = err.response.status;
if(status === 403) {
err.message = 'Invalid API Key provided';
} else {
let resp = err.response.data;
if(resp.errors) err.message = resp.errors.join(', ');
}
}
reject(err);
});
});
}
/**
* Prepares parameters to match format of the DataDog API before sending an event.
*
* @param {any} cfg Custom options for the event.
* @returns {Object} The compiled params object.
*
* @memberOf DataDogEvents
*/
_prepareParams(cfg) {
let opts = this.options;
if(typeof cfg.body === 'object') {
if(isError(cfg.body)) {
cfg.body = [
'```',
cfg.body.toString(),
cfg.body.stack,
'```'
].join('\n');
} else {
cfg.body = [
'```',
JSON.stringify(cfg.body, null, 4),
'```'
].join('\n');
}
cfg.markdown = true;
}
let params = {
title: `${opts.titlePrefix || ''}${cfg.title}`,
text: `${opts.bodyPrefix || ''}${cfg.body}${opts.bodyPostfix || ''}`,
priority: cfg.priority || opts.priority || 'normal',
tags: opts.tags || [],
alert_type: cfg.type || 'info'
};
if(cfg.markdown || opts.markdown)
params.text = `%%% \n ${params.text} \n %%%`;
if(cfg.date && cfg.date instanceof Date)
params.date_happened = Math.round(cfg.date.getTime() / 1000);
if(cfg.host || opts.host)
params.host = cfg.host || opts.host;
if(cfg.tags && Array.isArray(cfg.tags))
params.tags = params.tags.concat(cfg.tags);
if(cfg.aggregationKey || opts.aggregationKey)
params.aggregation_key = cfg.aggregationKey || opts.aggregationKey;
if(cfg.sourceType || opts.sourceType)
params.source_type_name = cfg.sourceType || opts.sourceType;
return params;
}
}
/**
* Shortcut for creating a new instance of DataDogEvents.
*
* @param {?Object} [options={}] Options to pass to DataDogEvents.
* @returns {DataDogEvents}
*/
function dataDogEvents(options = {}) {
return new DataDogEvents(options);
}
dataDogEvents.DataDogEvents = DataDogEvents;
module.exports = dataDogEvents;