1
1
import * as assert from 'assert' ;
2
+ import * as prom from 'prom-client' ;
2
3
import { getOrAdd , I32_MAX , getIbdBlockHeight , getUintEnvOrDefault } from '../helpers' ;
3
4
import {
4
5
DbBlock ,
@@ -130,6 +131,12 @@ type TransactionHeader = {
130
131
export class PgWriteStore extends PgStore {
131
132
readonly isEventReplay : boolean ;
132
133
protected isIbdBlockHeightReached = false ;
134
+ private metrics :
135
+ | {
136
+ blockHeight : prom . Gauge ;
137
+ burnBlockHeight : prom . Gauge ;
138
+ }
139
+ | undefined ;
133
140
134
141
constructor (
135
142
sql : PgSqlClient ,
@@ -138,6 +145,18 @@ export class PgWriteStore extends PgStore {
138
145
) {
139
146
super ( sql , notifier ) ;
140
147
this . isEventReplay = isEventReplay ;
148
+ if ( isProdEnv ) {
149
+ this . metrics = {
150
+ blockHeight : new prom . Gauge ( {
151
+ name : 'stacks_block_height' ,
152
+ help : 'Current chain tip block height' ,
153
+ } ) ,
154
+ burnBlockHeight : new prom . Gauge ( {
155
+ name : 'burn_block_height' ,
156
+ help : 'Current burn block height' ,
157
+ } ) ,
158
+ } ;
159
+ }
141
160
}
142
161
143
162
static async connect ( {
@@ -356,7 +375,7 @@ export class PgWriteStore extends PgStore {
356
375
if ( ! this . isEventReplay ) {
357
376
this . debounceMempoolStat ( ) ;
358
377
}
359
- if ( isCanonical )
378
+ if ( isCanonical ) {
360
379
await sql `
361
380
WITH new_tx_count AS (
362
381
SELECT tx_count + ${ data . txs . length } AS tx_count FROM chain_tip
@@ -372,6 +391,10 @@ export class PgWriteStore extends PgStore {
372
391
tx_count = (SELECT tx_count FROM new_tx_count),
373
392
tx_count_unanchored = (SELECT tx_count FROM new_tx_count)
374
393
` ;
394
+ if ( this . metrics ) {
395
+ this . metrics . blockHeight . set ( data . block . block_height ) ;
396
+ }
397
+ }
375
398
} ) ;
376
399
// Do we have an IBD height defined in ENV? If so, check if this block update reached it.
377
400
const ibdHeight = getIbdBlockHeight ( ) ;
@@ -1938,9 +1961,13 @@ export class PgWriteStore extends PgStore {
1938
1961
}
1939
1962
1940
1963
async updateBurnChainBlockHeight ( args : { blockHeight : number } ) : Promise < void > {
1941
- await this . sql `
1964
+ const result = await this . sql < { burn_block_height : number } [ ] > `
1942
1965
UPDATE chain_tip SET burn_block_height = GREATEST(${ args . blockHeight } , burn_block_height)
1966
+ RETURNING burn_block_height
1943
1967
` ;
1968
+ if ( this . metrics && result . length > 0 ) {
1969
+ this . metrics . burnBlockHeight . set ( result [ 0 ] . burn_block_height ) ;
1970
+ }
1944
1971
}
1945
1972
1946
1973
async insertSlotHoldersBatch ( sql : PgSqlClient , slotHolders : DbRewardSlotHolder [ ] ) : Promise < void > {
0 commit comments