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 2 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
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
18 changes: 16 additions & 2 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 @@ -107,6 +114,7 @@ export class Dumper {

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

let progress = new Progress({
initialValue: this.range().from,
Expand Down Expand Up @@ -193,11 +201,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 {port}`)
}
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: (block) => { prometheus.setLastSavedBlock(block); }
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
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
40 changes: 40 additions & 0 deletions substrate/substrate-dump/src/prometheus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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 lastBlockGauge: Gauge;
private lastSavedBlockGauge: Gauge;

constructor(port: number) {
this.port = port;
this.lastBlockGauge = new Gauge({
name: 'sqd_last_block_total',
Igorgro marked this conversation as resolved.
Show resolved Hide resolved
help: 'Last block available in the chain',
registers: [this.registry]
});

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

collectDefaultMetrics({register: this.registry})
}

setLastBlock(block: number) {
this.lastBlockGauge.set(block);
}

setLastSavedBlock(block: number) {
this.lastSavedBlockGauge.set(block);
}


serve(): Promise<ListeningServer> {
return createPrometheusServer(this.registry, this.port)
}
}
7 changes: 5 additions & 2 deletions util/util-internal-archive-layout/src/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ export class ArchiveLayout {
args: {
blocks: (nextBlock: number, prevHash?: string) => AsyncIterable<HashAndHeight[]>
range?: Range
chunkSize?: number
chunkSize?: number,
onSuccessWrite?: (lastBlock: number) => void
}
): Promise<void> {
return this.append(
Expand All @@ -190,7 +191,9 @@ export class ArchiveLayout {
assertNotNull(lastBlock)
).transactDir('.', async fs => {
let content = await out.end()
return fs.write('blocks.jsonl.gz', content)
const writeResult = fs.write('blocks.jsonl.gz', content)
writeResult.then(() => args.onSuccessWrite ? args.onSuccessWrite(lastBlock?.height || 0) : null)
return writeResult
})
firstBlock = undefined
lastBlock = undefined
Expand Down
Loading