-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
82 lines (64 loc) · 2.18 KB
/
main.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
const express = require("express");
const Prometheus = require("prom-client");
const { EC2Client, DescribeInstancesCommand } = require('@aws-sdk/client-ec2');
const { INSTANCE_SIZE_NORMALIZATION_DATA } = require('./instance-size-normalization');
const ec2Client = new EC2Client({ region: 'us-east-2' });
const metric = new Prometheus.Gauge({
name: "ec2_instance_family_cmopute_units",
help: "EC2 instance family compute units",
labelNames: ["family"],
});
const app = express();
app.get("/metrics", async function (req, res) {
const describeCommand = new DescribeInstancesCommand({})
const describeResults = await ec2Client.send(describeCommand);
const grouped = describeResults.Reservations.reduce((acc, reservation) => {
const instances = reservation.Instances || [];
instances.forEach(i => {
const instanceType = i.InstanceType;
const instanceState = i.State.Name;
const [family, size] = instanceType.split('.');
acc.debug.push({
state: instanceState,
keyName: i.KeyName,
instanceType
});
if (instanceState === 'stopped') {
return;
}
if (!(family in acc.counts)) {
acc.counts[family] = {
familyCount: 0
};
}
acc.counts[family].familyCount += 1;
if (!(size in acc.counts[family])) {
acc.counts[family][size] = 0;
}
acc.counts[family][size] += 1;
});
return acc;
}, { counts: {}, debug: [] });
const values = Object.entries(grouped.counts).reduce((acc, [familyKey, data]) => {
Object.keys(data).forEach((dataKey) => {
if (dataKey === 'familyCount') {
return;
}
if (!(familyKey in acc)) {
acc[familyKey] = 0;
}
const normalized = INSTANCE_SIZE_NORMALIZATION_DATA[dataKey] * data[dataKey];
acc[familyKey] += normalized;
});
return acc;
}, {});
console.log(grouped, values);
Object.entries(values).forEach(([familyKey, computeUnits]) => {
metric.labels({ family: familyKey }).set(computeUnits);
});
res.set("Content-Type", Prometheus.register.contentType);
const d = await Prometheus.register.metrics();
res.end(d);
metric.reset();
});
app.listen(3000);