-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.js
More file actions
69 lines (62 loc) · 2.41 KB
/
metrics.js
File metadata and controls
69 lines (62 loc) · 2.41 KB
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
const { InfluxDB, Point } = require('@influxdata/influxdb-client');
const Discord = require('discord.js');
const botSettings = require('./settings.json');
const process = require('process');
const influxDB = botSettings.influxDB ? new InfluxDB({ token: botSettings.influxDB.token, url: botSettings.influxDB.url }).getWriteApi('vibot', botSettings.influxDB.bucket, 'ns') : undefined;
// write node resource/cpu/memory usage
function writeProcessUsage() {
const point = new Point('node_process');
const ru = process.resourceUsage();
for (const [key, value] of Object.entries(ru)) {
point.intField(key, value);
}
const mu = process.memoryUsage();
for (const [key, value] of Object.entries(mu)) {
point.intField(key, value);
}
point.floatField('uptime', process.uptime());
if (!influxDB) return;
influxDB.writePoint(point);
}
const nodeUsageTimer = setInterval(writeProcessUsage, 10000);
function onShutdown() {
clearInterval(nodeUsageTimer);
}
process.on('exit', onShutdown);
module.exports = {
genPoint(measure, message) {
const point = new Point(measure)
.stringField('id', message.id)
.tag('type', message instanceof Discord.Message ? 'message' : (message instanceof Discord.BaseInteraction ? 'interaction' : undefined));
if (message.inGuild()) {
point.tag('guild', `${message.guild?.name}:${message.guildId}`);
point.tag('channel', `${message.channel?.name}:${message.channelId}`);
} else if (message.channel.isDMBased()) {
point.tag('dmuser', `${message.channel.recipient?.tag}:${message.channel.recipientId}`);
}
return point;
},
logMessage(measure, message, f) {
let point = module.exports.genPoint(measure, message);
if (f) point = f(point);
if (!influxDB) return;
influxDB.writePoint(point);
influxDB.flush();
},
logWrapper(measurement, f) {
return async (message, ...argv) => {
let point = module.exports.genPoint(measurement, message);
const log = (tag) => {
if (tag) point = point.tag(tag, true);
};
const ret = await f(log, message, ...argv);
module.exports.writePoint(point);
return ret;
};
},
writePoint(point) {
if (!influxDB) return;
influxDB.writePoint(point);
influxDB.flush();
}
};