-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export metrics for watcher config and upstream and external chain hea…
…ds (#497) * Add a cli to export chain head block numbers * Use ETH RPC endpoint and allow env overrides * Use ethers provider * Export upstream chain head block number in watcher metrics * Remove unnecessary exports * Upgrade package versions * Fix defaults usage * Export watcher config in metrics * Add metric for watcher sync mode * Remove cache flag from watcher config metrics * Update watcher config field names
- Loading branch information
1 parent
37732d1
commit 78e43bc
Showing
18 changed files
with
197 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// | ||
// Copyright 2023 Vulcanize, Inc. | ||
// | ||
|
||
import express from 'express'; | ||
import * as promClient from 'prom-client'; | ||
import debug from 'debug'; | ||
import { ethers } from 'ethers'; | ||
import JsonRpcProvider = ethers.providers.JsonRpcProvider; | ||
|
||
import { fetchLatestBlockNumber } from '@cerc-io/util'; | ||
|
||
const log = debug('laconic:chain-head-exporter'); | ||
|
||
// Env overrides: | ||
// ETH_RPC_ENDPOINT - Ethereum RPC API endpoint | ||
// ETH_RPC_API_KEY - Ethereum RPC API endpoint key | ||
// FIL_RPC_ENDPOINT - Filecoin RPC API endpoint | ||
// PORT - Metrics server listening port | ||
|
||
// Defaults | ||
const DEFAULT_ETH_RPC_ENDPOINT = 'https://mainnet.infura.io/v3'; | ||
const DEFAULT_FIL_RPC_ENDPOINT = 'https://api.node.glif.io/rpc/v1'; | ||
const DEFAULT_PORT = 5000; | ||
|
||
async function main (): Promise<void> { | ||
const app = express(); | ||
const metricsRegister = new promClient.Registry(); | ||
|
||
const ethRpcApiKey = process.env.ETH_RPC_API_KEY; | ||
if (!ethRpcApiKey) { | ||
log('WARNING: ETH_RPC_API_KEY not set'); | ||
} | ||
|
||
const ethRpcBaseUrl = process.env.ETH_RPC_ENDPOINT || DEFAULT_ETH_RPC_ENDPOINT; | ||
const ethUrlSuffix = ethRpcApiKey ? `/${ethRpcApiKey}` : ''; | ||
const ethRpcUrl = `${ethRpcBaseUrl}${ethUrlSuffix}`; | ||
let ethProvider: JsonRpcProvider; | ||
try { | ||
ethProvider = new JsonRpcProvider(ethRpcUrl); | ||
} catch (err) { | ||
log(`Error creating ETH RPC provider from URL ${ethRpcBaseUrl}`, err); | ||
} | ||
|
||
const filRpcUrl = process.env.FILECOIN_RPC_ENDPOINT || DEFAULT_FIL_RPC_ENDPOINT; | ||
let filProvider: JsonRpcProvider; | ||
try { | ||
filProvider = new JsonRpcProvider(filRpcUrl); | ||
} catch (err) { | ||
log(`Error creating FIL RPC provider from URL ${filRpcUrl}`, err); | ||
} | ||
|
||
// eslint-disable-next-line no-new | ||
new promClient.Gauge({ | ||
name: 'latest_block_number', | ||
help: 'Latest block number / height from various block chains', | ||
registers: [metricsRegister], | ||
labelNames: ['chain'] as const, | ||
async collect () { | ||
const [ | ||
latestEthBlockNumber, | ||
latestFilBlockNumber | ||
] = await Promise.all([ | ||
fetchLatestBlockNumber(ethProvider), | ||
fetchLatestBlockNumber(filProvider) | ||
]); | ||
|
||
this.set({ chain: 'ethereum' }, latestEthBlockNumber); | ||
this.set({ chain: 'filecoin' }, latestFilBlockNumber); | ||
} | ||
}); | ||
|
||
app.get('/metrics', async (req, res) => { | ||
res.set('Content-Type', metricsRegister.contentType); | ||
const metrics = await metricsRegister.metrics(); | ||
res.send(metrics); | ||
}); | ||
|
||
const port = Number(process.env.PORT) || DEFAULT_PORT; | ||
app.listen(port, () => { | ||
log(`Server running on port ${port}`); | ||
}); | ||
} | ||
|
||
main().catch(err => { | ||
log(err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.