Skip to content

Commit

Permalink
chore: fix pool metrics (#9652)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 authored Nov 25, 2024
1 parent 598c1b1 commit 233b387
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 31 deletions.
17 changes: 7 additions & 10 deletions yarn-project/p2p/src/client/p2p_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { retryUntil } from '@aztec/foundation/retry';
import { sleep } from '@aztec/foundation/sleep';
import { type AztecKVStore } from '@aztec/kv-store';
import { openTmpStore } from '@aztec/kv-store/utils';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

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

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

beforeEach(() => {
txPool = {
Expand Down Expand Up @@ -80,7 +77,7 @@ describe('In-Memory P2P Client', () => {
};

kvStore = openTmpStore();
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
});

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

const client2 = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
const client2 = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);
expect(client2.getSyncedLatestBlockNum()).toEqual(client.getSyncedLatestBlockNum());
});

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

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

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

blockSource.setProvenEpochNumber(2);
await client.start();
Expand Down Expand Up @@ -213,7 +210,7 @@ describe('In-Memory P2P Client', () => {
});

it('deletes expired proof quotes', async () => {
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0, telemetryClient);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 0);

blockSource.setProvenEpochNumber(1);
blockSource.setProvenBlockNumber(1);
Expand Down Expand Up @@ -276,7 +273,7 @@ describe('In-Memory P2P Client', () => {
});

it('deletes txs created from a pruned block', async () => {
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10, telemetryClient);
client = new P2PClient(kvStore, blockSource, mempools, p2pService, 10);
blockSource.setProvenBlockNumber(0);
await client.start();

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

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

Expand Down
5 changes: 3 additions & 2 deletions yarn-project/p2p/src/client/p2p_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { INITIAL_L2_BLOCK_NUM } from '@aztec/circuits.js/constants';
import { createDebugLogger } from '@aztec/foundation/log';
import { type AztecKVStore, type AztecMap, type AztecSingleton } from '@aztec/kv-store';
import { Attributes, type TelemetryClient, WithTracer, trackSpan } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

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

Expand Down Expand Up @@ -218,10 +219,10 @@ export class P2PClient extends WithTracer implements P2P {
mempools: MemPools,
private p2pService: P2PService,
private keepProvenTxsFor: number,
telemetryClient: TelemetryClient,
telemetry: TelemetryClient = new NoopTelemetryClient(),
private log = createDebugLogger('aztec:p2p'),
) {
super(telemetryClient, 'P2PClient');
super(telemetry, 'P2PClient');

const { blockCheckIntervalMS, blockRequestBatchSize } = getP2PConfigFromEnv();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { type BlockAttestation } from '@aztec/circuit-types';
import { createDebugLogger } from '@aztec/foundation/log';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import { PoolInstrumentation } from '../instrumentation.js';
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
import { type AttestationPool } from './attestation_pool.js';

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

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

constructor(_telemetry: TelemetryClient, private log = createDebugLogger('aztec:attestation_pool')) {
constructor(telemetry: TelemetryClient, private log = createDebugLogger('aztec:attestation_pool')) {
this.attestations = new Map();
this.metrics = new PoolInstrumentation(new NoopTelemetryClient(), 'InMemoryAttestationPool');
this.metrics = new PoolInstrumentation(telemetry, PoolName.ATTESTATION_POOL);
}

public getAttestationsForSlot(slot: bigint, proposalId: string): Promise<BlockAttestation[]> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { type EpochProofQuote } from '@aztec/circuit-types';
import { type TelemetryClient } from '@aztec/telemetry-client';
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';

import { PoolInstrumentation } from '../instrumentation.js';
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
import { type EpochProofQuotePool } from './epoch_proof_quote_pool.js';

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

constructor(_telemetry: TelemetryClient) {
constructor(telemetry: TelemetryClient) {
this.quotes = new Map();
this.metrics = new PoolInstrumentation(new NoopTelemetryClient(), 'MemoryEpochProofQuotePool');
this.metrics = new PoolInstrumentation(telemetry, PoolName.EPOCH_PROOF_QUOTE_POOL);
}

addQuote(quote: EpochProofQuote) {
Expand Down
45 changes: 42 additions & 3 deletions yarn-project/p2p/src/mem_pools/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,43 @@ import {
type UpDownCounter,
} from '@aztec/telemetry-client';

export enum PoolName {
TX_POOL = 'TxPool',
ATTESTATION_POOL = 'AttestationPool',
EPOCH_PROOF_QUOTE_POOL = 'EpochProofQuotePool',
}

type MetricsLabels = {
objectInMempool: Metrics;
objectSize: Metrics;
};

/**
* Get the metrics labels for a given pool name.
* They must all have different names, as if duplicates appear, it will brick
* the metrics instance
*/
function getMetricsLabels(name: PoolName): MetricsLabels {
if (name === PoolName.TX_POOL) {
return {
objectInMempool: Metrics.MEMPOOL_TX_COUNT,
objectSize: Metrics.MEMPOOL_TX_SIZE,
};
} else if (name === PoolName.ATTESTATION_POOL) {
return {
objectInMempool: Metrics.MEMPOOL_ATTESTATIONS_COUNT,
objectSize: Metrics.MEMPOOL_ATTESTATIONS_SIZE,
};
} else if (name === PoolName.EPOCH_PROOF_QUOTE_POOL) {
return {
objectInMempool: Metrics.MEMPOOL_PROVER_QUOTE_COUNT,
objectSize: Metrics.MEMPOOL_PROVER_QUOTE_SIZE,
};
}

throw new Error('Invalid pool type');
}

/**
* Instrumentation class for the Pools (TxPool, AttestationPool, etc).
*/
Expand All @@ -21,15 +58,17 @@ export class PoolInstrumentation<PoolObject extends Gossipable> {

private defaultAttributes;

constructor(telemetry: TelemetryClient, name: string) {
constructor(telemetry: TelemetryClient, name: PoolName) {
const meter = telemetry.getMeter(name);
this.defaultAttributes = { [Attributes.POOL_NAME]: name };

this.objectsInMempool = meter.createUpDownCounter(Metrics.MEMPOOL_TX_COUNT, {
const metricsLabels = getMetricsLabels(name);

this.objectsInMempool = meter.createUpDownCounter(metricsLabels.objectInMempool, {
description: 'The current number of transactions in the mempool',
});

this.objectSize = meter.createHistogram(Metrics.MEMPOOL_TX_SIZE, {
this.objectSize = meter.createHistogram(metricsLabels.objectSize, {
unit: 'By',
description: 'The size of transactions in the mempool',
advice: {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/p2p/src/mem_pools/tx_pool/aztec_kv_tx_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type Logger, createDebugLogger } from '@aztec/foundation/log';
import { type AztecKVStore, type AztecMap, type AztecSet } from '@aztec/kv-store';
import { type TelemetryClient } from '@aztec/telemetry-client';

import { PoolInstrumentation } from '../instrumentation.js';
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
import { type TxPool } from './tx_pool.js';

/**
Expand Down Expand Up @@ -37,7 +37,7 @@ export class AztecKVTxPool implements TxPool {

this.#store = store;
this.#log = log;
this.#metrics = new PoolInstrumentation(telemetry, 'AztecKVTxPool');
this.#metrics = new PoolInstrumentation(telemetry, PoolName.TX_POOL);
}

public markAsMined(txHashes: TxHash[], blockNumber: number): Promise<void> {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/p2p/src/mem_pools/tx_pool/memory_tx_pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type TxAddedToPoolStats } from '@aztec/circuit-types/stats';
import { createDebugLogger } from '@aztec/foundation/log';
import { type TelemetryClient } from '@aztec/telemetry-client';

import { PoolInstrumentation } from '../instrumentation.js';
import { PoolInstrumentation, PoolName } from '../instrumentation.js';
import { type TxPool } from './tx_pool.js';

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

public markAsMined(txHashes: TxHash[], blockNumber: number): Promise<void> {
Expand Down
5 changes: 2 additions & 3 deletions yarn-project/p2p/src/service/libp2p_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class LibP2PService extends WithTracer implements P2PService {
private discoveryRunningPromise?: RunningPromise;

// Request and response sub service
private reqresp: ReqResp;
public reqresp: ReqResp;

/**
* Callback for when a block is received from a peer.
Expand All @@ -102,11 +102,10 @@ export class LibP2PService extends WithTracer implements P2PService {
private l2BlockSource: L2BlockSource,
private proofVerifier: ClientProtocolCircuitVerifier,
private worldStateSynchronizer: WorldStateSynchronizer,
telemetry: TelemetryClient,
private telemetry: TelemetryClient,
private requestResponseHandlers: ReqRespSubProtocolHandlers = DEFAULT_SUB_PROTOCOL_HANDLERS,
private logger = createDebugLogger('aztec:libp2p_service'),
) {
// Instatntiate tracer
super(telemetry, 'LibP2PService');

this.peerManager = new PeerManager(node, peerDiscoveryService, config, logger);
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/prover-node/src/prover-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('prover-node', () => {
port,
);
const kvStore = openTmpStore();
return new P2PClient(kvStore, l2BlockSource, mempools, libp2pService, 0, telemetryClient);
return new P2PClient(kvStore, l2BlockSource, mempools, libp2pService, 0);
};

beforeEach(async () => {
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/telemetry-client/src/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export const MEMPOOL_DB_NUM_ITEMS = 'aztec.mempool.db.num_items';
export const MEMPOOL_DB_MAP_SIZE = 'aztec.mempool.db.map_size';
export const MEMPOOL_DB_USED_SIZE = 'aztec.mempool.db.used_size';

export const MEMPOOL_ATTESTATIONS_COUNT = 'aztec.mempool.attestations_count';
export const MEMPOOL_ATTESTATIONS_SIZE = 'aztec.mempool.attestations_size';

export const MEMPOOL_PROVER_QUOTE_COUNT = 'aztec.mempool.prover_quote_count';
export const MEMPOOL_PROVER_QUOTE_SIZE = 'aztec.mempool.prover_quote_size';

export const ARCHIVER_SYNC_DURATION = 'aztec.archiver.sync_duration';
export const ARCHIVER_BLOCK_HEIGHT = 'aztec.archiver.block_height';
export const ARCHIVER_BLOCK_SIZE = 'aztec.archiver.block_size';
Expand Down

0 comments on commit 233b387

Please sign in to comment.