Skip to content

Commit 84db441

Browse files
committed
Added @trace decorator
1 parent 2d435f1 commit 84db441

File tree

7 files changed

+83
-36
lines changed

7 files changed

+83
-36
lines changed

packages/api/src/metrics/OpenTelemetryServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export type OpenTelemetryServerConfig = {
2424
prometheus?: ConstructorParameters<typeof PrometheusExporter>[0];
2525
nodeScrapeInterval?: number;
2626
};
27-
tracing: {
27+
tracing?: {
2828
enabled?: boolean;
2929
otlp?: ConstructorParameters<typeof OTLPTraceExporter>[0];
3030
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import snakeCase from "lodash/snakeCase";
2+
3+
import { Tracer } from "./Tracer";
4+
5+
type Attributes = Record<string, string | number | boolean>;
6+
7+
export function trace<P extends any[], R>(
8+
name?: string,
9+
metadata?: ((p: P /*, r: R*/) => Attributes) | Attributes
10+
) {
11+
return (
12+
target: { tracer: Tracer },
13+
methodName: string,
14+
descriptor: TypedPropertyDescriptor<(...args: P) => Promise<R>>
15+
) => {
16+
const originalMethod = descriptor.value!;
17+
18+
descriptor.value = async function replaced(
19+
this: { tracer: Tracer },
20+
...args: P
21+
) {
22+
const metadataRecord =
23+
typeof metadata === "function" ? metadata(args) : metadata;
24+
return await this.tracer.trace(
25+
name ?? snakeCase(methodName),
26+
async () => {
27+
return await originalMethod.bind(this)(...args);
28+
},
29+
metadataRecord ?? {}
30+
);
31+
};
32+
};
33+
}

packages/sequencer/src/mempool/private/PrivateMempool.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import { CachedStateService } from "../../state/state/CachedStateService";
3030
import { AsyncStateService } from "../../state/async/AsyncStateService";
3131
import { distinctByPredicate } from "../../helpers/utils";
3232
import { Tracer } from "../../logging/Tracer";
33+
import { trace } from "../../logging/trace";
3334

3435
type MempoolTransactionPaths = {
3536
transaction: PendingTransaction;
3637
paths: Field[];
3738
};
39+
3840
@sequencerModule()
3941
export class PrivateMempool extends SequencerModule implements Mempool {
4042
public readonly events = new EventEmitter<MempoolEvents>();
@@ -51,7 +53,7 @@ export class PrivateMempool extends SequencerModule implements Mempool {
5153
private readonly sequencer: Sequencer<SequencerModulesRecord>,
5254
@inject("UnprovenStateService")
5355
private readonly stateService: AsyncStateService,
54-
@inject("Tracer") private readonly tracer: Tracer
56+
@inject("Tracer") public readonly tracer: Tracer
5557
) {
5658
super();
5759
this.accountStateHook =
@@ -102,30 +104,25 @@ export class PrivateMempool extends SequencerModule implements Mempool {
102104
return result?.result.afterNetworkState;
103105
}
104106

107+
@trace("mempool.get_txs")
105108
public async getTxs(limit?: number): Promise<PendingTransaction[]> {
106-
return await this.tracer.trace("mempool.get_txs", async () => {
107-
const txs = await this.transactionStorage.getPendingUserTransactions();
108-
109-
const baseCachedStateService = new CachedStateService(this.stateService);
110-
111-
const networkState =
112-
(await this.getStagedNetworkState()) ?? NetworkState.empty();
113-
114-
const sortedTxs = await this.tracer.trace(
115-
"mempool.validate_txs",
116-
async () =>
117-
await this.checkTxValid(
118-
txs,
119-
baseCachedStateService,
120-
this.protocol.stateServiceProvider,
121-
networkState,
122-
limit
123-
)
124-
);
125-
126-
this.protocol.stateServiceProvider.popCurrentStateService();
127-
return sortedTxs;
128-
});
109+
const txs = await this.transactionStorage.getPendingUserTransactions();
110+
111+
const baseCachedStateService = new CachedStateService(this.stateService);
112+
113+
const networkState =
114+
(await this.getStagedNetworkState()) ?? NetworkState.empty();
115+
116+
const sortedTxs = await this.checkTxValid(
117+
txs,
118+
baseCachedStateService,
119+
this.protocol.stateServiceProvider,
120+
networkState,
121+
limit
122+
);
123+
124+
this.protocol.stateServiceProvider.popCurrentStateService();
125+
return sortedTxs;
129126
}
130127

131128
// We iterate through the transactions. For each tx we run the account state hook.
@@ -135,6 +132,7 @@ export class PrivateMempool extends SequencerModule implements Mempool {
135132
// because a failed tx may succeed now if the failure was to do with a nonce issue, say.
136133
// TODO Refactor
137134
// eslint-disable-next-line sonarjs/cognitive-complexity
135+
@trace("mempool.validate_txs")
138136
private async checkTxValid(
139137
transactions: PendingTransaction[],
140138
baseService: CachedStateService,

packages/sequencer/src/protocol/production/BatchProducerModule.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Tracer } from "../../logging/Tracer";
2222
import { BlockProofSerializer } from "./tasks/serializers/BlockProofSerializer";
2323
import { BatchTracingService } from "./tracing/BatchTracingService";
2424
import { BatchFlow } from "./flow/BatchFlow";
25+
import { trace } from "../../logging/trace";
2526

2627
export type StateRecord = Record<string, Field[] | undefined>;
2728

@@ -54,7 +55,7 @@ export class BatchProducerModule extends SequencerModule {
5455
@inject("Database")
5556
private readonly database: Database,
5657
@inject("Tracer")
57-
private readonly tracer: Tracer,
58+
public readonly tracer: Tracer,
5859
private readonly batchFlow: BatchFlow,
5960
private readonly blockProofSerializer: BlockProofSerializer,
6061
private readonly batchTraceService: BatchTracingService
@@ -138,6 +139,7 @@ export class BatchProducerModule extends SequencerModule {
138139
noop();
139140
}
140141

142+
@trace("batch")
141143
private async produceBatch(
142144
blocks: BlockWithResult[],
143145
height: number

packages/sequencer/src/protocol/production/sequencing/BlockProducerModule.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Tracer } from "../../../logging/Tracer";
2929

3030
import { BlockProductionService } from "./BlockProductionService";
3131
import { BlockResultService } from "./BlockResultService";
32+
import { trace } from "../../../logging/trace";
3233

3334
export interface BlockConfig {
3435
allowEmptyBlock?: boolean;
@@ -108,6 +109,7 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
108109
}
109110
}
110111

112+
@trace("block.metadata", ([block]) => ({ height: block.height.toString() }))
111113
public async generateMetadata(block: Block): Promise<BlockResult> {
112114
const traceMetadata = {
113115
height: block.height.toString(),
@@ -135,10 +137,7 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
135137
public async tryProduceBlock(): Promise<Block | undefined> {
136138
if (!this.productionInProgress) {
137139
try {
138-
const block = await this.tracer.trace(
139-
"block",
140-
async () => await this.produceBlock()
141-
);
140+
const block = await this.produceBlock();
142141

143142
if (block === undefined) {
144143
if (!this.allowEmptyBlock()) {
@@ -168,6 +167,7 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
168167
return undefined;
169168
}
170169

170+
@trace("block.collect_inputs")
171171
private async collectProductionData(): Promise<{
172172
txs: PendingTransaction[];
173173
metadata: BlockWithResult;
@@ -210,13 +210,11 @@ export class BlockProducerModule extends SequencerModule<BlockConfig> {
210210
};
211211
}
212212

213+
@trace("block")
213214
private async produceBlock(): Promise<Block | undefined> {
214215
this.productionInProgress = true;
215216

216-
const { txs, metadata } = await this.tracer.trace(
217-
"block.collect_inputs",
218-
async () => await this.collectProductionData()
219-
);
217+
const { txs, metadata } = await this.collectProductionData();
220218

221219
// Skip production if no transactions are available for now
222220
if (txs.length === 0 && !this.allowEmptyBlock()) {

packages/sequencer/src/protocol/production/sequencing/TransactionExecutionService.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import {
4242
TransactionExecutionResult,
4343
} from "../../../storage/model/Block";
4444
import { UntypedStateTransition } from "../helpers/UntypedStateTransition";
45+
import { trace } from "../../../logging/trace";
46+
import { Tracer } from "../../../logging/Tracer";
4547

4648
const errors = {
4749
methodIdNotFound: (methodId: string) =>
@@ -185,7 +187,9 @@ export class TransactionExecutionService {
185187
protocol: Protocol<MandatoryProtocolModulesRecord & ProtocolModulesRecord>,
186188
// Coming in from the appchain scope (accessible by protocol & runtime)
187189
@inject("StateServiceProvider")
188-
private readonly stateServiceProvider: StateServiceProvider
190+
private readonly stateServiceProvider: StateServiceProvider,
191+
@inject("Tracer")
192+
public readonly tracer: Tracer
189193
) {
190194
this.transactionHooks = protocol.dependencyContainer.resolveAll(
191195
"ProvableTransactionHook"
@@ -277,6 +281,11 @@ export class TransactionExecutionService {
277281
);
278282
}
279283

284+
@trace("block.transaction", ([, tx, networkState]) => ({
285+
height: networkState.block.height.toString(),
286+
methodId: tx.methodId.toString(),
287+
isMessage: tx.isMessage,
288+
}))
280289
public async createExecutionTrace(
281290
asyncStateService: CachedStateService,
282291
tx: PendingTransaction,

packages/stack/src/scripts/graphql/server.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,14 @@ export async function startServer() {
195195
MerkleWitnessResolver: {},
196196
},
197197

198-
OpenTelemetryServer: {},
198+
OpenTelemetryServer: {
199+
tracing: {
200+
enabled: true,
201+
otlp: {
202+
url: "http://localhost:4318",
203+
},
204+
},
205+
},
199206

200207
Database: {
201208
// redis: {

0 commit comments

Comments
 (0)