Skip to content

Commit

Permalink
feat: add metrics for rpc requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Igorgro committed Sep 26, 2023
1 parent 785a35b commit af892c3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions substrate/substrate-dump/src/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ export class Dumper {
: this.options.withTrace ? '' : undefined
}

return this.src().getFinalizedBlocks([{
const blocks = this.src().getFinalizedBlocks([{
range,
request
}])
return blocks;
}

private async *process(from?: number, prevHash?: string): AsyncIterable<BlockData[]> {
Expand Down Expand Up @@ -140,6 +141,9 @@ export class Dumper {
)
}
}
const metrics = this.rpc().getMetrics();
this.prometheus().setSuccesfulRequestCount(metrics.requestsServed);
this.prometheus().setFailedRequestCount(metrics.connectionErrors);

yield batch.blocks

Expand Down Expand Up @@ -205,7 +209,7 @@ export class Dumper {
const prometheus = this.prometheus();
if (this.options.metricsPort) {
await prometheus.serve();
this.log().info(`prometheus metrics are available on port {port}`)
this.log().info(`prometheus metrics are available on port ${this.options.metricsPort}`)
}
await archive.appendRawBlocks({
blocks: (nextBlock, prevHash) => this.saveMetadata(this.process(nextBlock, prevHash)),
Expand Down
19 changes: 19 additions & 0 deletions substrate/substrate-dump/src/prometheus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class PrometheusServer {
private port?: number | string
private chainHeightGauge: Gauge;
private lastWrittenBlockGauge: Gauge;
private rpcRequestsGauge: Gauge;

constructor(port: number) {
this.port = port;
Expand All @@ -22,6 +23,13 @@ export class PrometheusServer {
registers: [this.registry]
});

this.rpcRequestsGauge = new Gauge({
name: 'sqd_rpc_requests_count',
help: 'Number of rpc requests of different kinds',
labelNames: ['kind'],
registers: [this.registry]
});

collectDefaultMetrics({register: this.registry})
}

Expand All @@ -33,6 +41,17 @@ export class PrometheusServer {
this.lastWrittenBlockGauge.set(block);
}

setSuccesfulRequestCount(requests: number) {
this.rpcRequestsGauge.set({
'kind': 'successful'
}, requests)
}

setFailedRequestCount(requests: number) {
this.rpcRequestsGauge.set({
'kind': 'failed'
}, requests)
}

serve(): Promise<ListeningServer> {
return createPrometheusServer(this.registry, this.port)
Expand Down

0 comments on commit af892c3

Please sign in to comment.