-
Notifications
You must be signed in to change notification settings - Fork 20
/
report.js
219 lines (195 loc) · 5.59 KB
/
report.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
import os from 'os';
import { sendReport } from './sendReport';
import globals from './globals';
const system =
process.platform === 'linux'
? require('./system.js')
: require('./mockSystem.js');
const { log } = console;
class Report {
constructor(wrapperInstance = {}) {
this.initalPromises = {
statPromise: system.readstat('self'),
bootIdPromise: system.readbootid()
};
// flag on report sending status, reports are sent once
this.sent = false;
const {
config = {},
context = {},
dnsPromise = Promise.resolve(),
metrics = [],
plugins = [],
startTime = process.hrtime(),
startTimestamp = Date.now()
} = wrapperInstance;
this.config = config;
this.context = context;
this.startTime = startTime;
this.dnsPromise = dnsPromise;
// Populate initial report skeleton on construction
const {
functionName,
functionVersion,
awsRequestId,
logGroupName,
logStreamName,
memoryLimitInMB
} = this.context;
let { invokedFunctionArn } = this.context;
// Patch invokedFunctionArn in cases of SAM local invocations
if (process.env.AWS_SAM_LOCAL) {
invokedFunctionArn = `arn:aws:lambda:local:0:function:${functionName}`;
}
const pluginMetas = plugins
.filter(plugin => typeof plugin !== 'undefined')
.map(plugin => plugin.meta || {});
this.report = {
client_id: this.config.clientId || undefined,
installMethod: this.config.installMethod,
duration: undefined,
processId: globals.PROCESS_ID,
timestamp: startTimestamp,
aws: {
functionName,
functionVersion,
awsRequestId,
invokedFunctionArn,
logGroupName,
logStreamName,
memoryLimitInMB,
getRemainingTimeInMillis: undefined,
/*eslint-disable no-underscore-dangle*/
traceId: process.env._X_AMZN_TRACE_ID
/*eslint-enable no-underscore-dangle*/
},
environment: {
agent: {
runtime: 'nodejs',
version: globals.VERSION,
load_time: globals.MODULE_LOAD_TIME
},
runtime: {
name: process.release.name,
version: process.version
},
nodejs: {
version: process.version,
memoryUsage: undefined
},
host: {
container_id: undefined
},
os: {}
},
errors: {},
coldstart: globals.COLDSTART,
custom_metrics: metrics,
plugins: pluginMetas
};
// Set to false after coldstart
globals.COLDSTART = false;
}
send(err, callback) {
// Send report only once
if (this.sent) {
return;
}
this.sent = true;
const self = this;
const config = this.config;
const context = this.context;
// Add error to report if necessary
if (err) {
const reportError = typeof err === 'string' ? new Error(err) : err;
const {
name,
message,
stack,
lineNumber,
columnNumber,
fileName
} = reportError;
this.report.errors = {
name,
message,
stack,
lineNumber,
columnNumber,
fileName
};
}
// Resolve system promises/report data
Promise.all([
this.initalPromises.statPromise,
system.readstat('self'),
system.readstatus('self'),
this.initalPromises.bootIdPromise
]).then(results => {
const preProcSelfStat = results[0];
const procSelfStat = results[1];
const procSelfStatus = results[2];
const bootId = results[3];
const osStats = {
hostname: os.hostname(),
uptime: os.uptime(),
totalmem: os.totalmem(),
freemem: os.freemem(),
usedmem: os.totalmem() - os.freemem(),
cpus: os.cpus(),
arch: os.arch(),
linux: {
pid: {
self: {
stat_start: preProcSelfStat,
stat: procSelfStat,
status: procSelfStatus
}
}
}
};
this.report.environment.os = osStats;
this.report.environment.host.boot_id = bootId;
this.report.environment.nodejs.memoryUsage = process.memoryUsage();
if (context.getRemainingTimeInMillis) {
this.report.aws.getRemainingTimeInMillis = context.getRemainingTimeInMillis();
}
this.report.timestampEnd = Date.now();
const durationHrTime = process.hrtime(this.startTime);
this.report.duration = Math.ceil(
durationHrTime[0] * 1e9 + durationHrTime[1]
);
if (config.debug) {
log('IOPIPE-DEBUG: ', JSON.stringify(this.report));
}
this.dnsPromise
.then(ipAddress => {
sendReport(self.report, config, ipAddress)
.then(function afterRequest(res) {
if (config.debug) {
log(`API STATUS FROM ${config.host}: ${res.status}`);
log(`API RESPONSE FROM ${config.host}: ${res.apiResponse}`);
}
callback(err);
})
.catch(function handleErr(collectorErr) {
// Log errors, don't block on failed requests
if (config.debug) {
log('Write to IOpipe failed');
log(collectorErr);
}
callback(err);
});
})
.catch(dnsErr => {
// Log errors, don't block on failed requests
if (config.debug) {
log('Write to IOpipe failed. DNS resolution error.');
log(dnsErr);
}
callback(err);
});
});
}
}
module.exports = Report;