-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
66 lines (62 loc) · 2.6 KB
/
index.ts
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
import Prometheus from "prom-client";
import toml from "toml";
import { Command } from 'commander';
import fs from 'fs'
import { fetchWithTimeoutAndFallback, urlResolve } from "./helper";
const program = new Command();
const freezedProviders = new Prometheus.Gauge({
name: 'provider_frozen',
help: 'Freezed status of a provider. 1 for frozen, 0 can be (not frozen) or (not found) in the provider\'s response.',
labelNames: ['chain'],
});
type Config = {
lava_rest_api: string[],
chains: string[],
lava_provider_address: string,
}
async function getFrozenChains(config: Config) {
const frozenChains = [];
const latestBlockUrl = config.lava_rest_api.map((url) => urlResolve(url, '/cosmos/base/tendermint/v1beta1/blocks/latest'));
const latestBlockResponse = await fetchWithTimeoutAndFallback(latestBlockUrl, 5000) as any;
const latestHeight = latestBlockResponse.block.header.height;
for (const chain of config.chains) {
const providerStatusUrl = config.lava_rest_api.map((url) => urlResolve(url, `/lavanet/lava/pairing/providers/${chain}?showFrozen=true`));
const providerStatusResponse = await fetchWithTimeoutAndFallback(providerStatusUrl, 5000) as any;
const frozenTotal = providerStatusResponse.stakeEntry.filter((provider: { stake_applied_block: string; }) => Number(provider.stake_applied_block) > Number(latestHeight))
const frozenProvider = frozenTotal.find((provider: { address: string; }) => config.lava_provider_address === provider.address);
if (frozenProvider) {
frozenChains.push(chain);
}
}
return frozenChains;
}
program
.name('cosmos-exporter')
.description('Prometheus lava exporter')
.version('1.0.0');
program.command('start')
.description('run exporter')
.option('--config <config>', 'Path to config file', './config.toml')
.option('-p, --port <port>', 'port', '5000')
.action(async (opt) => {
const tomlConfig = toml.parse(fs.readFileSync(opt.config, "utf-8")) as Config;
Bun.serve({
port: opt.port,
async fetch(req) {
const frozenChains = await getFrozenChains(tomlConfig);
tomlConfig.chains.forEach((chain) => {
freezedProviders.labels({ chain }).set(0);
});
frozenChains.forEach((chain) => {
freezedProviders.labels({ chain }).set(1);
});
req.headers.set('Content-Type', Prometheus.register.contentType);
const metrics = await Prometheus.register.metrics();
return new Response(metrics)
},
});
console.log('RUN WITH CONFIG:')
console.log(tomlConfig)
console.log('listening on port ' + opt.port)
})
program.parse(process.argv);