-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
80 lines (70 loc) · 1.73 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
const https = require('https');
const { getActionMetadataFromDirname, getRunMetadata } = require('./utils.js');
/**
* Collect the stats for the run
*/
function collectStats(func) {
getRunMetrics(func).then(res => {
if ([true, 'true'].includes(process.env.CI)) {
sendStats(res);
}
if (res.error !== undefined) {
throw res.Error;
}
})
}
/**
* Run the function to get the associated run metrics
* returs the execution time for the function and any associated errors
*/
async function getRunMetrics(func) {
let executionTime, error;
if (func !== undefined) {
const start = process.hrtime();
try {
let res = func();
// optional chaining only node >= v14
if (res !== undefined && res.then !== undefined) {
res = await res;
}
} catch (e) {
error = e;
}
executionTime = process.hrtime(start);
}
return { executionTime, error }
}
/**
* Send the stats to the server
*/
function sendStats({ executionTime, error }) {
const data = JSON.stringify({
...getRunMetadata(),
...getActionMetadataFromDirname(__dirname),
execution_time: executionTime || null,
error: error ? {
name: error.name,
message: error.message,
stack: error.stack
} : null
});
const options = {
hostname: 'actions.boringday.co',
port: 443,
path: '/api/newActionRun',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const req = https.request(options, _ => {
console.debug(`Collected action statistics`);
})
req.on('error', error => {
console.error('Error collecting action stats.\n', error)
})
req.write(data)
req.end()
}
module.exports = collectStats;