-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (113 loc) · 3.75 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
var datadog = require("dogapi");
var os = require("os");
module.exports = function (sails) {
return {
/**
* Intialise the hook
*/
initialize: function(cb) {
// getting name and version from the parent package.json
var package = require('../../package.json')
var name = package.name;
var version = package.version;
var hostName = os.hostname();
var tags = [
`${name}_${sails.config.environment}`,
`host: ${hostName}`,
`version: ${version}`
];
if (!sails.config.datadog) {
sails.log.warn(
'You have installed [sails-hook-datadog] but you are ' +
'missing the configuration.' +
'check https://github.com/bxfsh/sails-hook-datadog for more info');
return cb();
}
/**
* Initialise datadog API
* @return {[type]} [description]
*/
var initialiseDataDogApi = function initialiseDataDogApi() {
datadog.initialize(sails.config.datadog);
};
/**
* Override the sails.log.error
* @return {[type]} [description]
*/
var overrideErrorLog = function overrideErrorLog() {
var errorLogMessage = sails.log.error;
sails.log.error = function() {
var args = [(new Date().toISOString() + ':')];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
};
errorLogMessage.apply(this, args);
// sending datadog event if active
if (sails.config.datadog.active) {
var title = `ERROR: ${name} ${sails.config.environment}`;
var text = JSON.stringify(args) + (new Error().stack);
var properties = {
tags: tags,
alert_type: 'error'
};
datadog.event.create(title, text, properties, function(err, res) { });
}
};
};
/**
* adding winston to the sails info logs
* @return {[type]} [description]
*/
var overrideInfoLog = function overrideInfoLog() {
var infoLogMessage = sails.log.info;
sails.log.info = function() {
var args = [(new Date().toISOString() + ':')];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
};
infoLogMessage.apply(this, args);
// sending datadog event if active
if (sails.config.datadog.active) {
var title = `INFO: ${name} ${sails.config.environment}`;
var text = JSON.stringify(args);
var properties = {
tags: tags,
alert_type: 'info'
};
datadog.event.create(title, text, properties, function(err, res){ });
}
};
};
var createSuccessLog = function createSuccessLog() {
sails.log.success = function() {
var args = [(new Date().toISOString() + ':')];
for (var i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
};
// sending datadog event if active
if (sails.config.datadog.active) {
var title = `SUCCESS: ${name} ${sails.config.environment}`;
var text = JSON.stringify(args);
var properties = {
tags: tags,
alert_type: 'success'
};
datadog.event.create(title, text, properties, function(err, res){ });
}
};
};
initialiseDataDogApi();
overrideErrorLog();
overrideInfoLog();
createSuccessLog();
sails.log.info(
'[sails-hook-datadog]', `${name} ${version} ${sails.config.environment}`);
return cb();
},
/**
* Configure the hook
*/
configure: () => {
}
}
};