Skip to content
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

feat: implement dump metrics #200

Merged
merged 8 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@subsquid/substrate-dump",
"comment": "feat: implement prometheus metrics",
"type": "minor"
}
],
"packageName": "@subsquid/substrate-dump"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@subsquid/util-internal-archive-layout",
"comment": "feat: implement onSuccessWrite callback",
"type": "minor"
}
],
"packageName": "@subsquid/util-internal-archive-layout"
}
48 changes: 23 additions & 25 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion substrate/substrate-dump/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@
"@subsquid/util-internal-counters": "^1.3.0",
"@subsquid/util-internal-fs": "^0.1.0",
"@subsquid/util-internal-hex": "^1.2.0",
"@subsquid/util-internal-prometheus-server": "^1.2.0",
"@subsquid/util-internal-range": "^0.0.0",
"commander": "^11.0.0"
"commander": "^11.0.0",
"prom-client": "^14.2.0"
},
"devDependencies": {
"@types/node": "^18.16.17",
Expand Down
24 changes: 21 additions & 3 deletions substrate/substrate-dump/src/dumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {printTimeInterval, Progress} from '@subsquid/util-internal-counters'
import {createFs, Fs} from '@subsquid/util-internal-fs'
import {assertRange, printRange, Range, rangeEnd} from '@subsquid/util-internal-range'
import {MetadataWriter} from './metadata'
import { PrometheusServer } from './prometheus'


export interface DumperOptions {
Expand All @@ -26,6 +27,7 @@ export interface DumperOptions {
lastBlock?: number
withTrace?: boolean | string
chunkSize: number
metricsPort?: number
}


Expand Down Expand Up @@ -82,6 +84,11 @@ export class Dumper {
})
}

@def
prometheus() {
return new PrometheusServer(this.options.metricsPort ?? 3000);
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
}

ingest(range: Range): AsyncIterable<BlockBatch> {
let request: DataRequest = {
runtimeVersion: true,
Expand All @@ -92,10 +99,11 @@ export class Dumper {
: this.options.withTrace ? '' : undefined
}

return this.src().getFinalizedBlocks([{
const blocks = this.src().getFinalizedBlocks([{
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
range,
request
}])
return blocks;
}

private async *process(from?: number, prevHash?: string): AsyncIterable<BlockData[]> {
Expand All @@ -107,6 +115,7 @@ export class Dumper {

let height = new Throttler(() => this.src().getFinalizedHeight(), 300_000)
let chainHeight = await height.get()
this.prometheus().setChainHeight(chainHeight);
Igorgro marked this conversation as resolved.
Show resolved Hide resolved

let progress = new Progress({
initialValue: this.range().from,
Expand All @@ -132,6 +141,9 @@ export class Dumper {
)
}
}
const metrics = this.rpc().getMetrics();
this.prometheus().setSuccesfulRequestCount(metrics.requestsServed);
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
this.prometheus().setFailedRequestCount(metrics.connectionErrors);

yield batch.blocks

Expand Down Expand Up @@ -193,11 +205,17 @@ export class Dumper {
}
}
} else {
let archive = new ArchiveLayout(this.fs())
const archive = new ArchiveLayout(this.fs())
const prometheus = this.prometheus();
if (this.options.metricsPort) {
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
await prometheus.serve();
this.log().info(`prometheus metrics are available on port ${this.options.metricsPort}`)
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
}
await archive.appendRawBlocks({
blocks: (nextBlock, prevHash) => this.saveMetadata(this.process(nextBlock, prevHash)),
range: this.range(),
chunkSize: this.options.chunkSize * 1024 * 1024
chunkSize: this.options.chunkSize * 1024 * 1024,
onSuccessWrite: ({ blockRange: { to } }) => { prometheus.setLastWrittenBlock(to.height); }
})
}
}
Expand Down
1 change: 1 addition & 0 deletions substrate/substrate-dump/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ runProgram(() => {
program.option('--last-block <number>', 'Height of the last block to dump', nat)
program.option('--with-trace [targets]', 'Fetch block trace')
program.option('--chunk-size <MB>', 'Data chunk size in megabytes', positiveInt, 32)
program.option('--metrics-port <port>', 'Port to serve metrics on', nat)
Igorgro marked this conversation as resolved.
Show resolved Hide resolved

let args = program.parse().opts() as DumperOptions

Expand Down
59 changes: 59 additions & 0 deletions substrate/substrate-dump/src/prometheus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {createPrometheusServer, ListeningServer} from '@subsquid/util-internal-prometheus-server'
import promClient, { collectDefaultMetrics, Gauge, Registry } from 'prom-client';


export class PrometheusServer {
private registry = new Registry()
private port?: number | string
private chainHeightGauge: Gauge;
private lastWrittenBlockGauge: Gauge;
private rpcRequestsGauge: Gauge;

constructor(port: number) {
this.port = port;
this.chainHeightGauge = new Gauge({
name: 'sqd_dump_chain_height',
help: 'Last block available in the chain',
registers: [this.registry]
});

this.lastWrittenBlockGauge = new Gauge({
name: 'sqd_dump_last_written_block',
help: 'Last saved block',
registers: [this.registry]
});

this.rpcRequestsGauge = new Gauge({
name: 'sqd_rpc_requests_count',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General pattern for metric names in the SDK is sqd_{app}_{name}.

Since there could be multiple RPC connections used from a single process, url label should be added as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see currently only one url. But ok, will add this

help: 'Number of rpc requests of different kinds',
labelNames: ['kind'],
registers: [this.registry]
});

collectDefaultMetrics({register: this.registry})
}

setChainHeight(height: number) {
this.chainHeightGauge.set(height);
}

setLastWrittenBlock(block: number) {
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)
}
}
Loading
Loading