-
Notifications
You must be signed in to change notification settings - Fork 211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OG-570 Server readiness stats #666
Changes from 10 commits
9fb5bd7
7a741e2
7a0688c
b43d6be
35965bf
98672ab
9abffc8
ffbc3fe
ba1802c
6ca9c02
b1ac797
7f922bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface ReadinessInfo { | ||
runningSince: number | ||
currentStateTimestamp: number | ||
|
||
totalReadyTime: number | ||
totalNotReadyTime: number | ||
totalReadinessChanges: number | ||
} | ||
|
||
export interface StatsResponse extends ReadinessInfo { | ||
totalUptime: number | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,10 @@ export class HttpServer { | |
// used to work before workspaces, needs research | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
this.app.get('/getaddr', this.pingHandler.bind(this)) | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
this.app.post('/stats', this.statsHandler.bind(this)) | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
this.app.get('/stats', this.statsHandler.bind(this)) | ||
// used to work before workspaces, needs research | ||
// eslint-disable-next-line @typescript-eslint/no-misused-promises | ||
this.app.post('/relay', this.relayHandler.bind(this)) | ||
|
@@ -85,6 +89,21 @@ export class HttpServer { | |
} | ||
} | ||
|
||
statsHandler (req: Request, res: Response): void { | ||
if (this.relayService == null) { | ||
throw new Error('RelayServer not initialized') | ||
} | ||
try { | ||
const statsResponse = this.relayService.statsHandler() | ||
res.send(statsResponse) | ||
console.log('stats sent.') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logger.debug There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} catch (e) { | ||
const message: string = e.message | ||
res.send({ message }) | ||
this.logger.error(`stats handler rejected: ${message}`) | ||
} | ||
} | ||
|
||
async relayHandler (req: Request, res: Response): Promise<void> { | ||
if (this.relayService == null) { | ||
throw new Error('RelayServer not initialized') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ import { ServerAction } from './StoredTransaction' | |
import { TxStoreManager } from './TxStoreManager' | ||
import { configureServer, ServerConfigParams, ServerDependencies } from './ServerConfigParams' | ||
import { toBuffer } from 'ethereumjs-util' | ||
import { ReadinessInfo, StatsResponse } from '@opengsn/common/dist/StatsResponse' | ||
import Timeout = NodeJS.Timeout | ||
|
||
/** | ||
|
@@ -71,6 +72,7 @@ export class RelayServer extends EventEmitter { | |
config: ServerConfigParams | ||
transactionManager: TransactionManager | ||
txStoreManager: TxStoreManager | ||
readinessInfo: ReadinessInfo | ||
|
||
lastMinedActiveTransaction?: EventData | ||
|
||
|
@@ -103,6 +105,14 @@ export class RelayServer extends EventEmitter { | |
} | ||
this.reputationManager = dependencies.reputationManager | ||
} | ||
const now = Date.now() | ||
this.readinessInfo = { | ||
runningSince: now, | ||
currentStateTimestamp: now, | ||
totalReadyTime: 0, | ||
totalNotReadyTime: 0, | ||
totalReadinessChanges: 0 | ||
} | ||
this.printServerAddresses() | ||
this.logger.warn(`RelayServer version', ${gsnRuntimeVersion}`) | ||
this.logger.info(`Using server configuration:\n ${JSON.stringify(this.config)}`) | ||
|
@@ -138,6 +148,17 @@ export class RelayServer extends EventEmitter { | |
} | ||
} | ||
|
||
statsHandler (): StatsResponse { | ||
const now = Date.now() | ||
const statsResponse: StatsResponse = { ...this.readinessInfo, totalUptime: now - this.readinessInfo.runningSince } | ||
if (this.isReady()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. took me a while to understand: you update here the partial state, since last state change... maybe we can put a comment.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
statsResponse.totalReadyTime = this.readinessInfo.totalReadyTime + now - this.readinessInfo.currentStateTimestamp | ||
} else { | ||
statsResponse.totalNotReadyTime = this.readinessInfo.totalNotReadyTime + now - this.readinessInfo.currentStateTimestamp | ||
} | ||
return statsResponse | ||
} | ||
|
||
validateInput (req: RelayTransactionRequest, currentBlockNumber: number): void { | ||
// Check that the relayHub is the correct one | ||
if (req.metadata.relayHubAddress !== this.relayHubContract.address) { | ||
|
@@ -791,16 +812,21 @@ latestBlock timestamp | ${latestBlock.timestamp} | |
|
||
setReadyState (isReady: boolean): void { | ||
if (this.isReady() !== isReady) { | ||
const now = Date.now() | ||
if (isReady) { | ||
if (this.lastSuccessfulRounds < this.config.successfulRoundsForReady) { | ||
const roundsUntilReady = this.config.successfulRoundsForReady - this.lastSuccessfulRounds | ||
this.logger.warn(chalk.yellow(`Relayer state: almost READY (in ${roundsUntilReady} rounds)`)) | ||
} else { | ||
this.logger.warn(chalk.greenBright('Relayer state: READY')) | ||
} | ||
this.readinessInfo.totalNotReadyTime += now - this.readinessInfo.currentStateTimestamp | ||
} else { | ||
this.readinessInfo.totalReadyTime += now - this.readinessInfo.currentStateTimestamp | ||
this.logger.warn(chalk.redBright('Relayer state: NOT-READY')) | ||
} | ||
this.readinessInfo.currentStateTimestamp = now | ||
this.readinessInfo.totalReadinessChanges++ | ||
} | ||
this.ready = isReady | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, its required... how did the test pass without it ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was given default owner (zero address), which is now no longer allowed. This is from previous PR #665