-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (50 loc) · 1.61 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
require('dotenv').config()
const http = require('http');
const port = 3000;
const requestHandler = async (request, response) => {
switch (request.url) {
case '/metrics':
response.writeHead(200, {
'Content-Type': 'text/plain',
'Cache-Control': 'no-cache'
});
response.write(await getBrevoAccountMetrics());
response.end();
break;
default:
response.end('<a href="/metrics">Metrics</a>');
break;
}
};
const server = http.createServer(requestHandler);
server.listen(port, (err) => {
if (err) {
return console.log('Error starting webserver', err);
}
if (!process.env.BREVO_API_KEY) {
console.error('BREVO_API_KEY environment variable is not set');
process.exit(1);
}
console.log(`Webserver is listening on http://localhost:${port}`);
});
async function getBrevoAccountMetrics() {
const response = await fetch('https://api.brevo.com/v3/account', {
method: 'GET',
cache: 'no-cache',
headers: {
'api-key': process.env.BREVO_API_KEY
}
});
const jsonData = await response.json();
console.log(jsonData);
let plans = jsonData.plan.filter((plan) => {
return plan.creditsType == 'sendLimit';
})
let metric = '';
metric += '# TYPE brevo_credits gauge\n';
metric += '# HELP brevo_credits Number of credits remaining in the Brevo account per plan\n';
plans.forEach(plan => {
metric += `brevo_credits{type="${plan.type}"} ${plan.credits}\n`;
});
return metric;
}