Skip to content

Commit cdbd6be

Browse files
first commit
0 parents  commit cdbd6be

File tree

9 files changed

+375
-0
lines changed

9 files changed

+375
-0
lines changed

.gitignore

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# lava-exporter
2+
> Simple exporter to check your lava provider frozen or not
3+
4+
## Install
5+
6+
### Download exporter:
7+
```bash
8+
wget
9+
```
10+
### Build from source:
11+
1. Intall bun(javaScript runtime) https://bun.sh
12+
2. Clone this repo
13+
3. To install dependencies:
14+
```bash
15+
bun install
16+
```
17+
4. build binary:
18+
```bash
19+
bun build ./index.ts --compile --outfile lava-exporter
20+
```
21+
## Usage
22+
### Config example
23+
```toml
24+
lava_rest_api = ['https://rest-public-rpc.lavanet.xyz']
25+
chains = ['EVMOST', 'EVMOS', 'AXELAR', 'AXELART']
26+
lava_provider_address = 'lava@1rgs6cp3vleue3vwffrvttjtl4laqhk8fthu466'
27+
```
28+
Look at [config.example.toml](config.example.toml) for more details
29+
### Run
30+
```bash
31+
./lava-exporter start --config config.toml --port 3440
32+
```
33+
34+
### Metrics expose example
35+
```
36+
# HELP provider_frozen Freezed status of a provider. 1 for frozen, 0 can be not frozen or not found in providers response.
37+
# TYPE provider_frozen gauge
38+
provider_frozen{chain="EVMOST"} 0
39+
provider_frozen{chain="EVMOS"} 0
40+
provider_frozen{chain="AXELAR"} 0
41+
provider_frozen{chain="AXELART"} 0
42+
```

bun.lockb

5.47 KB
Binary file not shown.

config.example.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# array of lava rest api endpoints. Will be used next URL if previous is down
2+
lava_rest_api = ['https://rest-public-rpc.lavanet.xyz']
3+
# Your chains to track for your provider.
4+
# Name should be match with the chainID from this list:
5+
# https://rest-public-rpc.lavanet.xyz/lavanet/lava/spec/show_all_chains
6+
# Otherwise provider_frozen always will be 0
7+
chains = ['EVMOST', 'EVMOS', 'AXELAR', 'AXELART']
8+
# Your provider address
9+
lava_provider_address = 'lava@1rgs6cp3vleue3vwffrvttjtl4laqhk8fthu466'

helper.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export function urlResolve(from: string, to: string): string {
2+
const resolvedUrl = new URL(to, new URL(from, 'resolve://'))
3+
if (resolvedUrl.protocol === 'resolve:') {
4+
// `from` is a relative URL.
5+
const { pathname, search, hash } = resolvedUrl
6+
return pathname + search + hash
7+
}
8+
return resolvedUrl.toString()
9+
}
10+
export async function fetchWithTimeoutAndFallback(urls: string[], timeout = 5000) {
11+
// Function to fetch with a timeout
12+
async function fetchWithTimeout(url: string, timeout: number) {
13+
const controller = new AbortController();
14+
const signal = controller.signal;
15+
const fetchPromise = fetch(url, { signal });
16+
17+
// Timeout logic
18+
const timeoutId = setTimeout(() => controller.abort(), timeout);
19+
20+
try {
21+
const response = await fetchPromise;
22+
clearTimeout(timeoutId);
23+
return response;
24+
} catch (error) {
25+
clearTimeout(timeoutId);
26+
throw error;
27+
}
28+
}
29+
30+
// Loop through the URLs
31+
for (const url of urls) {
32+
try {
33+
const response = await fetchWithTimeout(url, timeout);
34+
if (response.ok) {
35+
return await response.json(); // or response.text(), depending on your needs
36+
}
37+
// Handle HTTP error statuses
38+
console.error(`Fetch failed for ${url}: ${response.statusText}`);
39+
} catch (error) {
40+
console.error(`Error fetching ${url}:`, (error as Error).message);
41+
// If it's a network error or timeout, try the next URL
42+
}
43+
}
44+
throw new Error("All fetch attempts failed");
45+
}

index.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import Prometheus from "prom-client";
2+
import toml from "toml";
3+
import { Command } from 'commander';
4+
import fs from 'fs'
5+
import { fetchWithTimeoutAndFallback, urlResolve } from "./helper";
6+
const program = new Command();
7+
const freezedProviders = new Prometheus.Gauge({
8+
name: 'provider_frozen',
9+
help: 'Freezed status of a provider. 1 for frozen, 0 can be not frozen or not found in providers response.',
10+
labelNames: ['chain'],
11+
});
12+
type Config = {
13+
lava_rest_api: string[],
14+
chains: string[],
15+
lava_provider_address: string,
16+
}
17+
async function getFrozenChains(config: Config) {
18+
const frozenChains = [];
19+
const latestBlockUrl = config.lava_rest_api.map((url) => urlResolve(url, '/cosmos/base/tendermint/v1beta1/blocks/latest'));
20+
const latestBlockResponse = await fetchWithTimeoutAndFallback(latestBlockUrl, 5000) as any;
21+
const latestHeight = latestBlockResponse.block.header.height;
22+
for (const chain of config.chains) {
23+
const providerStatusUrl = config.lava_rest_api.map((url) => urlResolve(url, `/lavanet/lava/pairing/providers/${chain}?showFrozen=true`));
24+
const providerStatusResponse = await fetchWithTimeoutAndFallback(providerStatusUrl, 5000) as any;
25+
const frozenTotal = providerStatusResponse.stakeEntry.filter((provider: { stake_applied_block: string; }) => Number(provider.stake_applied_block) > Number(latestHeight))
26+
const frozenProvider = frozenTotal.find((provider: { address: string; }) => config.lava_provider_address === provider.address);
27+
if (frozenProvider) {
28+
frozenChains.push(chain);
29+
}
30+
}
31+
return frozenChains;
32+
}
33+
34+
program
35+
.name('cosmos-exporter')
36+
.description('Prometheus lava exporter')
37+
.version('1.0.0');
38+
39+
program.command('start')
40+
.description('run exporter')
41+
.option('--config <config>', 'Path to config file', './config.toml')
42+
.option('-p, --port <port>', 'port', '5000')
43+
.action(async (opt) => {
44+
const tomlConfig = toml.parse(fs.readFileSync(opt.config, "utf-8")) as Config;
45+
46+
Bun.serve({
47+
port: opt.port,
48+
async fetch(req) {
49+
const frozenChains = await getFrozenChains(tomlConfig);
50+
tomlConfig.chains.forEach((chain) => {
51+
freezedProviders.labels({ chain }).set(0);
52+
});
53+
frozenChains.forEach((chain) => {
54+
freezedProviders.labels({ chain }).set(1);
55+
});
56+
req.headers.set('Content-Type', Prometheus.register.contentType);
57+
const metrics = await Prometheus.register.metrics();
58+
return new Response(metrics)
59+
},
60+
});
61+
console.log('RUN WITH CONFIG:')
62+
console.log(tomlConfig)
63+
console.log('listening on port ' + opt.port)
64+
})
65+
66+
program.parse(process.argv);

lava-exporter

45.1 MB
Binary file not shown.

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "lava-exporter",
3+
"module": "index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest"
7+
},
8+
"peerDependencies": {
9+
"typescript": "^5.0.0"
10+
},
11+
"dependencies": {
12+
"commander": "^11.1.0",
13+
"prom-client": "^15.1.0",
14+
"toml": "^3.0.0"
15+
}
16+
}

tsconfig.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["ESNext"],
4+
"target": "ESNext",
5+
"module": "ESNext",
6+
"moduleDetection": "force",
7+
"jsx": "react-jsx",
8+
"allowJs": true,
9+
10+
/* Bundler mode */
11+
"moduleResolution": "bundler",
12+
"allowImportingTsExtensions": true,
13+
"verbatimModuleSyntax": true,
14+
"noEmit": true,
15+
16+
/* Linting */
17+
"skipLibCheck": true,
18+
"strict": true,
19+
"noFallthroughCasesInSwitch": true,
20+
"forceConsistentCasingInFileNames": true
21+
}
22+
}

0 commit comments

Comments
 (0)