-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
122 lines (108 loc) · 3.74 KB
/
app.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
'use strict';
const winston = require('winston');
const WinstonCloudWatch = require("winston-cloudwatch");
const pm2 = require('pm2');
const pmx = require('pmx');
const os = require("os");
const http = require('http');
async function request(method, url, token = null) {
// const dataString = JSON.stringify(data)
const options = {
method,
headers: {
'Content-Type': 'application/json',
// 'Content-Length': dataString.length,
},
timeout: 1500, // in ms
}
if(token) {
options.headers['X-aws-ec2-metadata-token'] = token
} else {
options.headers['X-aws-ec2-metadata-token-ttl-seconds'] = 21600
}
return new Promise((resolve, reject) => {
const req = http.request(url, options, (res) => {
if (res.statusCode < 200 || res.statusCode > 299) {
return reject(new Error(`HTTP status code ${res.statusCode}`))
}
const body = []
res.on('data', (chunk) => body.push(chunk))
res.on('end', () => {
const resString = Buffer.concat(body).toString()
resolve(resString)
})
})
req.on('error', (err) => {
reject(err)
})
req.on('timeout', () => {
req.destroy()
reject(new Error('Request time out'))
})
// req.write(dataString)
req.end()
})
}
(async ()=>{
let region, instanceid
try {
const token = await request('PUT', "http://169.254.169.254/latest/api/token")
region = await request('GET', "http://169.254.169.254/latest/meta-data/placement/region", token)
instanceid = await request('GET', "http://169.254.169.254/latest/meta-data/instance-id", token)
console.log(`Auto set region to: ${region} and logStreamName to: ${instanceid}`)
} catch (error) {
console.log("NOT USING AWS")
}
pmx.initModule({
widget: {
logo: 'https://a0.awsstatic.com/libra-css/images/logos/[email protected]',
theme: ['#141A1F', '#222222', '#3ff', '#3ff'],
el: {
probes: false,
actions: false
},
block: {
actions: false,
issues: false,
meta: false,
}
}
}, function (err, conf) {
const loggerObj = {};
const log = function (level, name, message, packet) {
name = name.trim();
if (name === 'pm2-cloudwatch' || name === 'pm2-auto-pull') {
return;
}
if (!loggerObj[name]) {
loggerObj[name] = createLogger(name);
}
loggerObj[name][level](message);
};
const createLogger = function (program) {
return new winston.createLogger({
transports: [
// transport
new (WinstonCloudWatch)({
logGroupName: conf.logGroupName,
logStreamName: instanceid || conf.logStreamName,
awsRegion: region || conf.awsRegion,
awsAccessKeyId: conf.awsAccessKeyId,
awsSecretKey: conf.awsSecretKey
})]
});
};
//
pm2.connect(function () {
console.log('info', 'PM2: forwarding to Cloudwatch');
pm2.launchBus(function (err, bus) {
bus.on('log:out', function (packet) {
log('info', packet.process.name, packet.data, packet);
});
bus.on('log:err', function (packet) {
log('error', packet.process.name, packet.data, packet);
});
});
});
});
})();