Add health + stats endpoint

This commit is contained in:
Wietse Wind
2023-10-06 01:42:24 +02:00
parent e405e4d561
commit 9d1b9557d4
4 changed files with 40 additions and 0 deletions

View File

@@ -71,3 +71,16 @@ for automation.
This file browser is for development and test purposes only, for production, put a static webserver This file browser is for development and test purposes only, for production, put a static webserver
in front of this application & reverse proxy only the WebSocket (HTTP Upgrade) server. in front of this application & reverse proxy only the WebSocket (HTTP Upgrade) server.
#### Monitoring
A health check endpoint lives on `/health`, and returns e.g.:
```json
{
"uptime": 44023,
"lastLedger": 41800244,
"lastLedgerTx": 41800238,
"txCount": 276
}
```

View File

@@ -5,6 +5,12 @@ import expressWs from 'express-ws'
import autoindex from 'express-autoindex/dist/index.cjs.js' import autoindex from 'express-autoindex/dist/index.cjs.js'
import 'dotenv/config' import 'dotenv/config'
import { lastLedger } from '../lib/onLedger.mjs'
import { txCount } from '../lib/onTransaction.mjs'
const startDate = new Date()
let lastLedgerTx
let wss // WebSocket Server let wss // WebSocket Server
if (!wss) { if (!wss) {
@@ -27,6 +33,15 @@ if (!wss) {
app.use('/', express.static('./store/')) app.use('/', express.static('./store/'))
app.use('/health', (req, res) => {
res.json({
uptime: new Date() - startDate,
lastLedger: lastLedger ?? null,
lastLedgerTx: lastLedgerTx ?? null,
txCount: txCount ?? null,
})
})
app.use('/:networkId([0-9]{1,})', (req, res, next) => { app.use('/:networkId([0-9]{1,})', (req, res, next) => {
return autoindex('./store/' + req.params.networkId + '/', { json: /application\/json/.test(req.get('accept')) })(req, res, next) return autoindex('./store/' + req.params.networkId + '/', { json: /application\/json/.test(req.get('accept')) })(req, res, next)
}) })
@@ -78,6 +93,8 @@ const emit = _data => {
data.xpop.blob = undefined data.xpop.blob = undefined
} }
lastLedgerTx = data.origin.ledgerIndex
if (account === '' || data.account === account) { if (account === '' || data.account === account) {
client.send(JSON.stringify(data), { binary: false }) client.send(JSON.stringify(data), { binary: false })
} }

View File

@@ -9,6 +9,8 @@ import 'dotenv/config'
const obtainedHumanReadableLedgers = [] const obtainedHumanReadableLedgers = []
const obtainedBinaryTxLedgers = [] const obtainedBinaryTxLedgers = []
let lastLedger
const onLedger = async ({ const onLedger = async ({
networkId, networkId,
ledger, ledger,
@@ -19,6 +21,8 @@ const onLedger = async ({
const relativeStoreDir = 'store/' + networkId + '/' + ledgerIndexToFolders(ledger.ledger_index) const relativeStoreDir = 'store/' + networkId + '/' + ledgerIndexToFolders(ledger.ledger_index)
const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname
lastLedger = ledger.ledger_index
if (await dirExists(storeDir)) { if (await dirExists(storeDir)) {
const ledgerData = [ const ledgerData = [
...( ...(
@@ -148,4 +152,5 @@ const onLedger = async ({
export { export {
onLedger, onLedger,
lastLedger,
} }

View File

@@ -6,6 +6,8 @@ import { waitForLedgerReady } from './events/ledgerReady.mjs'
import { emit } from '../bin/webserver.mjs' import { emit } from '../bin/webserver.mjs'
import 'dotenv/config' import 'dotenv/config'
let txCount = 0
const xpopBinaryDir = new URL('../store/xpop', import.meta.url).pathname const xpopBinaryDir = new URL('../store/xpop', import.meta.url).pathname
const lastSeenTransactions = [] const lastSeenTransactions = []
@@ -81,6 +83,8 @@ const onTransaction = async ({
if (xpopWritten) { if (xpopWritten) {
console.log(' ### EMIT XPOP READY FOR', tx?.Account, Number(tx.Sequence), tx.hash) console.log(' ### EMIT XPOP READY FOR', tx?.Account, Number(tx.Sequence), tx.hash)
txCount++
return await emit({ return await emit({
account: tx?.Account, account: tx?.Account,
sequence: tx.Sequence, sequence: tx.Sequence,
@@ -116,4 +120,5 @@ const onTransaction = async ({
export { export {
onTransaction, onTransaction,
txCount,
} }