This repository has been archived by the owner on Dec 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
61 lines (48 loc) · 1.5 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
const tls = require('tls');
const Transport = require('winston-transport');
const safeStringify = require('fast-safe-stringify');
const config = {
host: 'intake.logs.datadoghq.com',
port: 10516
};
const setupListeners = socket =>
new Promise((resolve, reject) => {
socket.on('secureConnect', resolve).on('error', reject).on('timeout', reject);
});
// @see @credits https://git.io/fhwzM
module.exports = class DatadogTransport extends Transport {
constructor(opts) {
super(opts);
this.metadata = {};
config.apiKey = opts.apiKey;
// add port and host as optional options
//so the user can change the port and host when his Databog region is not US
config.port = opts.port || config.port;
config.host = opts.host || config.host;
if (opts.metadata) {
Object.assign(this.metadata, opts.metadata);
}
}
async log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});
const socket = tls.connect(config.port, config.host);
try {
// connect to socket
await setupListeners(socket);
} catch (exception) {
// catch any exception that might happen and report it back
return callback(exception);
}
if (!socket.authorized) {
return callback('Error connecting to DataDog');
}
// Merge the metadata with the log
const logEntry = {...this.metadata, ...info};
socket.write(`${config.apiKey} ${safeStringify(logEntry)}\r\n`, () => {
socket.end();
return callback();
});
}
};