-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (57 loc) · 1.94 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
require('dotenv').config();
const {launchChrome, runLighthouse} = require('./lighthouse');
const {generateMetricsPayload, sendMetricsToDatadog} = require('./datadog');
const args = require('yargs')
.option('url', {
alias: 'u',
description: 'URL to audit',
type: 'string',
demandOption: true,
})
.option('app', {
alias: 'a',
description: 'App name used to define the metric namspace',
type: 'string',
demandOption: true,
})
.option('tag', {
alias: 't',
description: 'Tag(s) to apply to all datadog metrics',
type: 'array',
})
.option('dry-run', {
description: 'Print metric data instead of sending it to datadog',
type: 'boolean',
})
.example('', 'node index.js -u https://wuphf.com/ -a wuphf -t tagA:value -t tagB:value')
.help().argv;
(async () => {
try {
const {url, app, tag: tags, dryRun} = args;
if (!dryRun && !process.env.DATADOG_API_KEY) {
/* An API key is required when actually sending to Datadog
* so validate and fail early before doing any processing */
console.error('Error: An API key is required to send metrics to Datadog.');
return;
}
const chrome = await launchChrome({headless: true});
const metrics = await runLighthouse(url, chrome);
await chrome.kill();
const payload = generateMetricsPayload(app, metrics, tags);
if (dryRun) {
/* The datadog client uses nested arrays which do not print well so to
* make the table a little more human readible, we extract the metric value
* and timestamp into their own fields */
const breakdown = payload.map((metric) => {
let [timestamp, value] = metric.points[0];
timestamp = new Date(timestamp * 1000);
return {...metric, timestamp, value};
});
console.table(breakdown, ['metric', 'value', 'timestamp', 'tags']);
} else {
await sendMetricsToDatadog(payload);
}
} catch (error) {
console.error(error);
}
})();