Skip to content

Commit 233b387

Browse files
authored
chore: fix pool metrics (#9652)
1 parent 598c1b1 commit 233b387

File tree

10 files changed

+71
-31
lines changed

10 files changed

+71
-31
lines changed

yarn-project/p2p/src/client/p2p_client.test.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { retryUntil } from '@aztec/foundation/retry';
55
import { sleep } from '@aztec/foundation/sleep';
66
import { type AztecKVStore } from '@aztec/kv-store';
77
import { openTmpStore } from '@aztec/kv-store/utils';
8-
import { type TelemetryClient } from '@aztec/telemetry-client';
9-
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
108

119
import { expect, jest } from '@jest/globals';
1210

@@ -32,7 +30,6 @@ describe('In-Memory P2P Client', () => {
3230
let p2pService: Mockify<P2PService>;
3331
let kvStore: AztecKVStore;
3432
let client: P2PClient;
35-
const telemetryClient: TelemetryClient = new NoopTelemetryClient();
3633

3734
beforeEach(() => {
3835
txPool = {
@@ -80,7 +77,7 @@ describe('In-Memory P2P Client', () => {
8077
};
8178

8279
kvStore = openTmpStore();
83-
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
80+
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
8481
});
8582

8683
const advanceToProvenBlock = async (getProvenBlockNumber: number, provenEpochNumber = getProvenBlockNumber) => {
@@ -150,7 +147,7 @@ describe('In-Memory P2P Client', () => {
150147
await client.start();
151148
await client.stop();
152149

153-
const client2 = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
150+
const client2 = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
154151
expect(client2.getSyncedLatestBlockNum()).toEqual(client.getSyncedLatestBlockNum());
155152
});
156153

@@ -165,7 +162,7 @@ describe('In-Memory P2P Client', () => {
165162
});
166163

167164
it('deletes txs after waiting the set number of blocks', async () => {
168-
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10, telemetryClient);
165+
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10);
169166
blockSource.setProvenBlockNumber(0);
170167
await client.start();
171168
expect(txPool.deleteTxs).not.toHaveBeenCalled();
@@ -182,7 +179,7 @@ describe('In-Memory P2P Client', () => {
182179
});
183180

184181
it('stores and returns epoch proof quotes', async () => {
185-
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
182+
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
186183

187184
blockSource.setProvenEpochNumber(2);
188185
await client.start();
@@ -213,7 +210,7 @@ describe('In-Memory P2P Client', () => {
213210
});
214211

215212
it('deletes expired proof quotes', async () => {
216-
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
213+
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
217214

218215
blockSource.setProvenEpochNumber(1);
219216
blockSource.setProvenBlockNumber(1);
@@ -276,7 +273,7 @@ describe('In-Memory P2P Client', () => {
276273
});
277274

278275
it('deletes txs created from a pruned block', async () => {
279-
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10, telemetryClient);
276+
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10);
280277
blockSource.setProvenBlockNumber(0);
281278
await client.start();
282279

@@ -298,7 +295,7 @@ describe('In-Memory P2P Client', () => {
298295
});
299296

300297
it('moves mined and valid txs back to the pending set', async () => {
301-
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10, telemetryClient);
298+
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10);
302299
blockSource.setProvenBlockNumber(0);
303300
await client.start();
304301

yarn-project/p2p/src/client/p2p_client.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js/constants';
1515
import { createDebugLogger } from '@aztec/foundation/log';
1616
import { type AztecKVStore, type AztecMap, type AztecSingleton } from '@aztec/kv-store';
1717
import { Attributes, type TelemetryClient, WithTracer, trackSpan } from '@aztec/telemetry-client';
18+
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
1819

1920
import { type ENR } from '@chainsafe/enr';
2021

@@ -218,10 +219,10 @@ export class P2PClient extends WithTracer implements P2P {
218219
mempools: MemPools,
219220
private p2pService: P2PService,
220221
private keepProvenTxsFor: number,
221-
telemetryClient: TelemetryClient,
222+
telemetry: TelemetryClient = new NoopTelemetryClient(),
222223
private log = createDebugLogger('aztec:p2p'),
223224
) {
224-
super(telemetryClient, 'P2PClient');
225+
super(telemetry, 'P2PClient');
225226

226227
const { blockCheckIntervalMS, blockRequestBatchSize } = getP2PConfigFromEnv();
227228

yarn-project/p2p/src/mem_pools/attestation_pool/memory_attestation_pool.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import { type BlockAttestation } from '@aztec/circuit-types';
22
import { createDebugLogger } from '@aztec/foundation/log';
33
import { type TelemetryClient } from '@aztec/telemetry-client';
4-
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
54

6-
import { PoolInstrumentation } from '../instrumentation.js';
5+
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
76
import { type AttestationPool } from './attestation_pool.js';
87

98
export class InMemoryAttestationPool implements AttestationPool {
109
private metrics: PoolInstrumentation<BlockAttestation>;
1110

1211
private attestations: Map</*slot=*/ bigint, Map</*proposalId*/ string, Map</*address=*/ string, BlockAttestation>>>;
1312

14-
constructor(_telemetry: TelemetryClient, private log = createDebugLogger('aztec:attestation_pool')) {
13+
constructor(telemetry: TelemetryClient, private log = createDebugLogger('aztec:attestation_pool')) {
1514
this.attestations = new Map();
16-
this.metrics = new PoolInstrumentation(new NoopTelemetryClient(), 'InMemoryAttestationPool');
15+
this.metrics = new PoolInstrumentation(telemetry, PoolName.ATTESTATION_POOL);
1716
}
1817

1918
public getAttestationsForSlot(slot: bigint, proposalId: string): Promise<BlockAttestation[]> {

yarn-project/p2p/src/mem_pools/epoch_proof_quote_pool/memory_epoch_proof_quote_pool.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { type EpochProofQuote } from '@aztec/circuit-types';
22
import { type TelemetryClient } from '@aztec/telemetry-client';
3-
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
43

5-
import { PoolInstrumentation } from '../instrumentation.js';
4+
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
65
import { type EpochProofQuotePool } from './epoch_proof_quote_pool.js';
76

87
export class MemoryEpochProofQuotePool implements EpochProofQuotePool {
98
private quotes: Map<bigint, EpochProofQuote[]>;
109
private metrics: PoolInstrumentation<EpochProofQuote>;
1110

12-
constructor(_telemetry: TelemetryClient) {
11+
constructor(telemetry: TelemetryClient) {
1312
this.quotes = new Map();
14-
this.metrics = new PoolInstrumentation(new NoopTelemetryClient(), 'MemoryEpochProofQuotePool');
13+
this.metrics = new PoolInstrumentation(telemetry, PoolName.EPOCH_PROOF_QUOTE_POOL);
1514
}
1615

1716
addQuote(quote: EpochProofQuote) {

yarn-project/p2p/src/mem_pools/instrumentation.ts

+42-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,43 @@ import {
88
type UpDownCounter,
99
} from '@aztec/telemetry-client';
1010

11+
export enum PoolName {
12+
TX_POOL = 'TxPool',
13+
ATTESTATION_POOL = 'AttestationPool',
14+
EPOCH_PROOF_QUOTE_POOL = 'EpochProofQuotePool',
15+
}
16+
17+
type MetricsLabels = {
18+
objectInMempool: Metrics;
19+
objectSize: Metrics;
20+
};
21+
22+
/**
23+
* Get the metrics labels for a given pool name.
24+
* They must all have different names, as if duplicates appear, it will brick
25+
* the metrics instance
26+
*/
27+
function getMetricsLabels(name: PoolName): MetricsLabels {
28+
if (name === PoolName.TX_POOL) {
29+
return {
30+
objectInMempool: Metrics.MEMPOOL_TX_COUNT,
31+
objectSize: Metrics.MEMPOOL_TX_SIZE,
32+
};
33+
} else if (name === PoolName.ATTESTATION_POOL) {
34+
return {
35+
objectInMempool: Metrics.MEMPOOL_ATTESTATIONS_COUNT,
36+
objectSize: Metrics.MEMPOOL_ATTESTATIONS_SIZE,
37+
};
38+
} else if (name === PoolName.EPOCH_PROOF_QUOTE_POOL) {
39+
return {
40+
objectInMempool: Metrics.MEMPOOL_PROVER_QUOTE_COUNT,
41+
objectSize: Metrics.MEMPOOL_PROVER_QUOTE_SIZE,
42+
};
43+
}
44+
45+
throw new Error('Invalid pool type');
46+
}
47+
1148
/**
1249
* Instrumentation class for the Pools (TxPool, AttestationPool, etc).
1350
*/
@@ -21,15 +58,17 @@ export class PoolInstrumentation<PoolObject extends Gossipable> {
2158

2259
private defaultAttributes;
2360

24-
constructor(telemetry: TelemetryClient, name: string) {
61+
constructor(telemetry: TelemetryClient, name: PoolName) {
2562
const meter = telemetry.getMeter(name);
2663
this.defaultAttributes = { [Attributes.POOL_NAME]: name };
2764

28-
this.objectsInMempool = meter.createUpDownCounter(Metrics.MEMPOOL_TX_COUNT, {
65+
const metricsLabels = getMetricsLabels(name);
66+
67+
this.objectsInMempool = meter.createUpDownCounter(metricsLabels.objectInMempool, {
2968
description: 'The current number of transactions in the mempool',
3069
});
3170

32-
this.objectSize = meter.createHistogram(Metrics.MEMPOOL_TX_SIZE, {
71+
this.objectSize = meter.createHistogram(metricsLabels.objectSize, {
3372
unit: 'By',
3473
description: 'The size of transactions in the mempool',
3574
advice: {

yarn-project/p2p/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { type Logger, createDebugLogger } from '@aztec/foundation/log';
44
import { type AztecKVStore, type AztecMap, type AztecSet } from '@aztec/kv-store';
55
import { type TelemetryClient } from '@aztec/telemetry-client';
66

7-
import { PoolInstrumentation } from '../instrumentation.js';
7+
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
88
import { type TxPool } from './tx_pool.js';
99

1010
/**
@@ -37,7 +37,7 @@ export class AztecKVTxPool implements TxPool {
3737

3838
this.#store = store;
3939
this.#log = log;
40-
this.#metrics = new PoolInstrumentation(telemetry, 'AztecKVTxPool');
40+
this.#metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL);
4141
}
4242

4343
public markAsMined(txHashes: TxHash[], blockNumber: number): Promise<void> {

yarn-project/p2p/src/mem_pools/tx_pool/memory_tx_pool.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type TxAddedToPoolStats } from '@aztec/circuit-types/stats';
33
import { createDebugLogger } from '@aztec/foundation/log';
44
import { type TelemetryClient } from '@aztec/telemetry-client';
55

6-
import { PoolInstrumentation } from '../instrumentation.js';
6+
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
77
import { type TxPool } from './tx_pool.js';
88

99
/**
@@ -27,7 +27,7 @@ export class InMemoryTxPool implements TxPool {
2727
this.txs = new Map<bigint, Tx>();
2828
this.minedTxs = new Map();
2929
this.pendingTxs = new Set();
30-
this.metrics = new PoolInstrumentation(telemetry, 'InMemoryTxPool');
30+
this.metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL);
3131
}
3232

3333
public markAsMined(txHashes: TxHash[], blockNumber: number): Promise<void> {

yarn-project/p2p/src/service/libp2p_service.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class LibP2PService extends WithTracer implements P2PService {
8585
private discoveryRunningPromise?: RunningPromise;
8686

8787
// Request and response sub service
88-
private reqresp: ReqResp;
88+
public reqresp: ReqResp;
8989

9090
/**
9191
* Callback for when a block is received from a peer.
@@ -102,11 +102,10 @@ export class LibP2PService extends WithTracer implements P2PService {
102102
private l2BlockSource: L2BlockSource,
103103
private proofVerifier: ClientProtocolCircuitVerifier,
104104
private worldStateSynchronizer: WorldStateSynchronizer,
105-
telemetry: TelemetryClient,
105+
private telemetry: TelemetryClient,
106106
private requestResponseHandlers: ReqRespSubProtocolHandlers = DEFAULT_SUB_PROTOCOL_HANDLERS,
107107
private logger = createDebugLogger('aztec:libp2p_service'),
108108
) {
109-
// Instatntiate tracer
110109
super(telemetry, 'LibP2PService');
111110

112111
this.peerManager = new PeerManager(node, peerDiscoveryService, config, logger);

yarn-project/prover-node/src/prover-node.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ describe('prover-node', () => {
307307
port,
308308
);
309309
const kvStore = openTmpStore();
310-
return new P2PClient(kvStore, l2BlockSource, mempools, libp2pService, 0, telemetryClient);
310+
return new P2PClient(kvStore, l2BlockSource, mempools, libp2pService, 0);
311311
};
312312

313313
beforeEach(async () => {

yarn-project/telemetry-client/src/metrics.ts

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ export const MEMPOOL_DB_NUM_ITEMS = 'aztec.mempool.db.num_items';
2929
export const MEMPOOL_DB_MAP_SIZE = 'aztec.mempool.db.map_size';
3030
export const MEMPOOL_DB_USED_SIZE = 'aztec.mempool.db.used_size';
3131

32+
export const MEMPOOL_ATTESTATIONS_COUNT = 'aztec.mempool.attestations_count';
33+
export const MEMPOOL_ATTESTATIONS_SIZE = 'aztec.mempool.attestations_size';
34+
35+
export const MEMPOOL_PROVER_QUOTE_COUNT = 'aztec.mempool.prover_quote_count';
36+
export const MEMPOOL_PROVER_QUOTE_SIZE = 'aztec.mempool.prover_quote_size';
37+
3238
export const ARCHIVER_SYNC_DURATION = 'aztec.archiver.sync_duration';
3339
export const ARCHIVER_BLOCK_HEIGHT = 'aztec.archiver.block_height';
3440
export const ARCHIVER_BLOCK_SIZE = 'aztec.archiver.block_size';

0 commit comments

Comments
 (0)