Skip to content

Commit 9699699

Browse files
authored
Add trace id to logs (#423)
* Add trace id to logs Comments fixes * Reducing code reuse
1 parent 44f31ca commit 9699699

File tree

8 files changed

+447
-342
lines changed

8 files changed

+447
-342
lines changed

packages/relay/src/formatters.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,17 @@
1818
*
1919
*/
2020

21+
import constants from "./lib/constants";
22+
2123
const hashNumber = (num) => {
2224
return '0x' + num.toString(16);
2325
};
2426

25-
export { hashNumber };
27+
/**
28+
* Format message prefix for logger.
29+
*/
30+
const formatRequestIdMessage = (requestId?: string): string => {
31+
return requestId ? `[${constants.REQUEST_ID_STRING}${requestId}]` : '';
32+
}
33+
34+
export { hashNumber, formatRequestIdMessage };

packages/relay/src/index.ts

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -46,75 +46,75 @@ export interface Net {
4646

4747
export interface Eth {
4848

49-
blockNumber(): Promise<string>;
49+
blockNumber(requestId?: string): Promise<string>;
5050

51-
call(call: any, blockParam: string | null): Promise<string | JsonRpcError>;
51+
call(call: any, blockParam: string | null, requestId?: string): Promise<string | JsonRpcError>;
5252

53-
coinbase(): JsonRpcError;
53+
coinbase(requestId?: string): JsonRpcError;
5454

55-
estimateGas(transaction:any, blockParam: string| null): Promise<string>;
55+
estimateGas(transaction:any, blockParam: string| null, requestId?: string): Promise<string>;
5656

57-
gasPrice(): Promise<string>;
57+
gasPrice(requestId?: string): Promise<string>;
5858

59-
getBalance(account: string, blockNumber: string | null): Promise<string>;
59+
getBalance(account: string, blockNumber: string | null, requestId?: string): Promise<string>;
6060

61-
getBlockByHash(hash: string, showDetails: boolean): Promise<Block | null>;
61+
getBlockByHash(hash: string, showDetails: boolean, requestId?: string): Promise<Block | null>;
6262

63-
getBlockByNumber(blockNum: string, showDetails: boolean): Promise<Block | null>;
63+
getBlockByNumber(blockNum: string, showDetails: boolean, requestId?: string): Promise<Block | null>;
6464

65-
getBlockTransactionCountByHash(hash: string): Promise<string | null>;
65+
getBlockTransactionCountByHash(hash: string, requestId?: string): Promise<string | null>;
6666

67-
getBlockTransactionCountByNumber(blockNum: string): Promise<string | null>
67+
getBlockTransactionCountByNumber(blockNum: string, requestId?: string): Promise<string | null>
6868

69-
getCode(address: string, blockNumber: string | null): Promise<string>;
69+
getCode(address: string, blockNumber: string | null, requestId?: string): Promise<string>;
7070

71-
chainId(): string;
71+
chainId(requestId?: string): string;
7272

73-
getLogs(blockHash: string|null, fromBlock: string|null, toBlock: string|null, address: string|null, topics: any[]|null): Promise<Log[]>;
73+
getLogs(blockHash: string|null, fromBlock: string|null, toBlock: string|null, address: string|null, topics: any[]|null, requestId?: string): Promise<Log[]>;
7474

75-
getStorageAt(address: string, slot: string, blockNumber: string|null): JsonRpcError;
75+
getStorageAt(address: string, slot: string, blockNumber: string|null, requestId?: string): JsonRpcError;
7676

77-
getTransactionByBlockHashAndIndex(hash: string, index: number): Promise<Transaction | null>;
77+
getTransactionByBlockHashAndIndex(hash: string, index: number, requestId?: string): Promise<Transaction | null>;
7878

79-
getTransactionByBlockNumberAndIndex(blockNum: string, index: number): Promise<Transaction | null>;
79+
getTransactionByBlockNumberAndIndex(blockNum: string, index: number, requestId?: string): Promise<Transaction | null>;
8080

81-
getTransactionByHash(hash: string): Promise<Transaction | null>;
81+
getTransactionByHash(hash: string, requestId?: string): Promise<Transaction | null>;
8282

83-
getTransactionCount(address: string, blocknum: string): Promise<string | JsonRpcError>;
83+
getTransactionCount(address: string, blocknum: string, requestId?: string): Promise<string | JsonRpcError>;
8484

85-
getTransactionReceipt(hash: string): Promise<Receipt | null>;
85+
getTransactionReceipt(hash: string, requestId?: string): Promise<Receipt | null>;
8686

87-
getUncleByBlockHashAndIndex(): Promise<any>;
87+
getUncleByBlockHashAndIndex(requestId?: string): Promise<any>;
8888

89-
getUncleByBlockNumberAndIndex(): Promise<any>;
89+
getUncleByBlockNumberAndIndex(requestId?: string): Promise<any>;
9090

91-
getUncleCountByBlockHash(): Promise<string>;
91+
getUncleCountByBlockHash(requestId?: string): Promise<string>;
9292

93-
getUncleCountByBlockNumber(): Promise<string>;
93+
getUncleCountByBlockNumber(requestId?: string): Promise<string>;
9494

95-
getWork(): JsonRpcError;
95+
getWork(requestId?: string): JsonRpcError;
9696

97-
feeHistory(blockCount: number, newestBlock: string, rewardPercentiles: Array<number>|null): Promise<any>;
97+
feeHistory(blockCount: number, newestBlock: string, rewardPercentiles: Array<number>|null, requestId?: string): Promise<any>;
9898

99-
hashrate(): Promise<string>;
99+
hashrate(requestId?: string): Promise<string>;
100100

101-
mining(): Promise<boolean>;
101+
mining(requestId?: string): Promise<boolean>;
102102

103-
protocolVersion(): JsonRpcError;
103+
protocolVersion(requestId?: string): JsonRpcError;
104104

105-
sendRawTransaction(transaction: string): Promise<string | JsonRpcError>;
105+
sendRawTransaction(transaction: string, requestId?: string): Promise<string | JsonRpcError>;
106106

107-
sendTransaction(): JsonRpcError;
107+
sendTransaction(requestId?: string): JsonRpcError;
108108

109-
sign(): JsonRpcError;
109+
sign(requestId?: string): JsonRpcError;
110110

111-
signTransaction(): JsonRpcError;
111+
signTransaction(requestId?: string): JsonRpcError;
112112

113-
submitHashrate(): JsonRpcError;
113+
submitHashrate(requestId?: string): JsonRpcError;
114114

115-
submitWork(): Promise<boolean>;
115+
submitWork(requestId?: string): Promise<boolean>;
116116

117-
syncing(): Promise<boolean>;
117+
syncing(requestId?: string): Promise<boolean>;
118118

119-
accounts(): Array<any>;
119+
accounts(requestId?: string): Array<any>;
120120
}

packages/relay/src/lib/clients/mirrorNodeClient.ts

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { MirrorNodeClientError } from './../errors/MirrorNodeClientError';
2323
import { Logger } from "pino";
2424
import constants from './../constants';
2525
import { Histogram, Registry } from 'prom-client';
26+
import { formatRequestIdMessage } from '../../formatters';
2627

2728
export interface ILimitOrderParams {
2829
limit?: number;
@@ -140,110 +141,123 @@ export class MirrorNodeClient {
140141
this.logger.info(`Mirror Node client successfully configured to ${this.baseUrl}`);
141142
}
142143

143-
async request(path: string, pathLabel: string, allowedErrorStatuses?: number[]): Promise<any> {
144+
async request(path: string, pathLabel: string, allowedErrorStatuses?: number[], requestId?: string): Promise<any> {
144145
const start = Date.now();
146+
const requestIdPrefix = formatRequestIdMessage(requestId);
145147
let ms;
146148
try {
147149
const response = await this.client.get(path);
148150
ms = Date.now() - start;
149-
this.logger.debug(`[GET] ${path} ${response.status} ${ms} ms`);
151+
this.logger.debug(`${requestIdPrefix} [GET] ${path} ${response.status} ${ms} ms`);
150152
this.mirrorResponseHistogram.labels(pathLabel, response.status).observe(ms);
151153
return response.data;
152154
} catch (error: any) {
153155
ms = Date.now() - start;
154156
const effectiveStatusCode = error.response !== undefined ? error.response.status : MirrorNodeClient.unknownServerErrorHttpStatusCode;
155157
this.mirrorResponseHistogram.labels(pathLabel, effectiveStatusCode).observe(ms);
156-
this.handleError(error, path, effectiveStatusCode, allowedErrorStatuses);
158+
this.handleError(error, path, effectiveStatusCode, allowedErrorStatuses, requestId);
157159
}
158160
return null;
159161
}
160162

161-
handleError(error: any, path: string, effectiveStatusCode: number, allowedErrorStatuses?: number[]) {
163+
handleError(error: any, path: string, effectiveStatusCode: number, allowedErrorStatuses?: number[], requestId?: string) {
164+
const requestIdPrefix = formatRequestIdMessage(requestId);
162165
if (allowedErrorStatuses && allowedErrorStatuses.length) {
163166
if (error.response && allowedErrorStatuses.indexOf(effectiveStatusCode) !== -1) {
164-
this.logger.debug(`[GET] ${path} ${effectiveStatusCode} status`);
167+
this.logger.debug(`${requestIdPrefix} [GET] ${path} ${effectiveStatusCode} status`);
165168
return null;
166169
}
167170
}
168171

169-
this.logger.error(new Error(error.message), `[GET] ${path} ${effectiveStatusCode} status`);
172+
this.logger.error(new Error(error.message), `${requestIdPrefix} [GET] ${path} ${effectiveStatusCode} status`);
170173
throw new MirrorNodeClientError(error.message, effectiveStatusCode);
171174
}
172175

173-
public async getAccountLatestTransactionByAddress(idOrAliasOrEvmAddress: string): Promise<object> {
176+
public async getAccountLatestTransactionByAddress(idOrAliasOrEvmAddress: string, requestId?: string): Promise<object> {
174177
return this.request(`${MirrorNodeClient.GET_ACCOUNTS_ENDPOINT}${idOrAliasOrEvmAddress}?order=desc&limit=1`,
175178
MirrorNodeClient.GET_ACCOUNTS_ENDPOINT,
176-
[400]);
179+
[400],
180+
requestId);
177181
}
178182

179-
public async getAccount(idOrAliasOrEvmAddress: string): Promise<object> {
183+
public async getAccount(idOrAliasOrEvmAddress: string, requestId?: string): Promise<object> {
180184
return this.request(`${MirrorNodeClient.GET_ACCOUNTS_ENDPOINT}${idOrAliasOrEvmAddress}`,
181185
MirrorNodeClient.GET_ACCOUNTS_ENDPOINT,
182-
[400, 404]);
186+
[400, 404],
187+
requestId);
183188
}
184189

185-
public async getBlock(hashOrBlockNumber: string | number) {
190+
public async getBlock(hashOrBlockNumber: string | number, requestId?: string) {
186191
return this.request(`${MirrorNodeClient.GET_BLOCK_ENDPOINT}${hashOrBlockNumber}`,
187192
MirrorNodeClient.GET_BLOCK_ENDPOINT,
188-
[400]);
193+
[400],
194+
requestId);
189195
}
190196

191-
public async getBlocks(blockNumber?: number | string[], timestamp?: string, limitOrderParams?: ILimitOrderParams) {
197+
public async getBlocks(blockNumber?: number | string[], timestamp?: string, limitOrderParams?: ILimitOrderParams, requestId?: string) {
192198
const queryParamObject = {};
193199
this.setQueryParam(queryParamObject, 'block.number', blockNumber);
194200
this.setQueryParam(queryParamObject, 'timestamp', timestamp);
195201
this.setLimitOrderParams(queryParamObject, limitOrderParams);
196202
const queryParams = this.getQueryParams(queryParamObject);
197203
return this.request(`${MirrorNodeClient.GET_BLOCKS_ENDPOINT}${queryParams}`,
198204
MirrorNodeClient.GET_BLOCKS_ENDPOINT,
199-
[400, 404]);
205+
[400, 404],
206+
requestId);
200207
}
201208

202-
public async getContract(contractIdOrAddress: string) {
209+
public async getContract(contractIdOrAddress: string, requestId?: string) {
203210
return this.request(`${MirrorNodeClient.GET_CONTRACT_ENDPOINT}${contractIdOrAddress}`,
204211
MirrorNodeClient.GET_CONTRACT_ENDPOINT,
205-
[400, 404]);
212+
[400, 404],
213+
requestId);
206214
}
207215

208-
public async getContractResult(transactionIdOrHash: string) {
216+
public async getContractResult(transactionIdOrHash: string, requestId?: string) {
209217
return this.request(`${MirrorNodeClient.GET_CONTRACT_RESULT_ENDPOINT}${transactionIdOrHash}`,
210218
MirrorNodeClient.GET_CONTRACT_RESULT_ENDPOINT,
211-
[400, 404]);
219+
[400, 404],
220+
requestId);
212221
}
213222

214-
public async getContractResults(contractResultsParams?: IContractResultsParams, limitOrderParams?: ILimitOrderParams) {
223+
public async getContractResults(contractResultsParams?: IContractResultsParams, limitOrderParams?: ILimitOrderParams, requestId?: string) {
215224
const queryParamObject = {};
216225
this.setContractResultsParams(queryParamObject, contractResultsParams);
217226
this.setLimitOrderParams(queryParamObject, limitOrderParams);
218227
const queryParams = this.getQueryParams(queryParamObject);
219228
return this.request(`${MirrorNodeClient.GET_CONTRACT_RESULTS_ENDPOINT}${queryParams}`,
220229
MirrorNodeClient.GET_CONTRACT_RESULTS_ENDPOINT,
221-
[400]);
230+
[400],
231+
requestId);
222232
}
223233

224-
public async getContractResultsDetails(contractId: string, timestamp: string) {
234+
public async getContractResultsDetails(contractId: string, timestamp: string, requestId?: string) {
225235
return this.request(`${this.getContractResultsDetailsByContractIdAndTimestamp(contractId, timestamp)}`,
226236
MirrorNodeClient.GET_CONTRACT_RESULTS_DETAILS_BY_CONTRACT_ID_ENDPOINT,
227-
[400]);
237+
[400],
238+
requestId);
228239
}
229240

230241
public async getContractResultsByAddress(
231242
contractIdOrAddress: string,
232243
contractResultsParams?: IContractResultsParams,
233-
limitOrderParams?: ILimitOrderParams) {
244+
limitOrderParams?: ILimitOrderParams,
245+
requestId?: string) {
234246
const queryParamObject = {};
235247
this.setContractResultsParams(queryParamObject, contractResultsParams);
236248
this.setLimitOrderParams(queryParamObject, limitOrderParams);
237249
const queryParams = this.getQueryParams(queryParamObject);
238250
return this.request(`${MirrorNodeClient.getContractResultsByAddressPath(contractIdOrAddress)}${queryParams}`,
239251
MirrorNodeClient.GET_CONTRACT_RESULTS_BY_ADDRESS_ENDPOINT,
240-
[400]);
252+
[400],
253+
requestId);
241254
}
242255

243-
public async getContractResultsByAddressAndTimestamp(contractIdOrAddress: string, timestamp: string) {
256+
public async getContractResultsByAddressAndTimestamp(contractIdOrAddress: string, timestamp: string, requestId?: string) {
244257
return this.request(`${MirrorNodeClient.getContractResultsByAddressPath(contractIdOrAddress)}/${timestamp}`,
245258
MirrorNodeClient.GET_CONTRACT_RESULTS_BY_ADDRESS_ENDPOINT,
246-
[206, 400, 404]);
259+
[206, 400, 404],
260+
requestId);
247261
}
248262

249263
private prepareLogsParams(
@@ -265,17 +279,20 @@ export class MirrorNodeClient {
265279

266280
public async getContractResultsLogs(
267281
contractLogsResultsParams?: IContractLogsResultsParams,
268-
limitOrderParams?: ILimitOrderParams) {
282+
limitOrderParams?: ILimitOrderParams,
283+
requestId?: string) {
269284
const queryParams = this.prepareLogsParams(contractLogsResultsParams, limitOrderParams);
270285
return this.request(`${MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_ENDPOINT}${queryParams}`,
271286
MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_ENDPOINT,
272-
[400, 404]);
287+
[400, 404],
288+
requestId);
273289
}
274290

275291
public async getContractResultsLogsByAddress(
276292
address: string,
277293
contractLogsResultsParams?: IContractLogsResultsParams,
278-
limitOrderParams?: ILimitOrderParams
294+
limitOrderParams?: ILimitOrderParams,
295+
requestId?: string
279296
) {
280297
const queryParams = this.prepareLogsParams(contractLogsResultsParams, limitOrderParams);
281298
const apiEndpoint = MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT.replace(
@@ -284,34 +301,37 @@ export class MirrorNodeClient {
284301
);
285302
return this.request(`${apiEndpoint}${queryParams}`,
286303
MirrorNodeClient.GET_CONTRACT_RESULT_LOGS_BY_ADDRESS_ENDPOINT,
287-
[400, 404]);
304+
[400, 404],
305+
requestId);
288306
}
289307

290-
public async getLatestBlock() {
291-
return this.getBlocks(undefined, undefined, this.getLimitOrderQueryParam(1, MirrorNodeClient.ORDER.DESC));
308+
public async getLatestBlock(requestId?: string) {
309+
return this.getBlocks(undefined, undefined, this.getLimitOrderQueryParam(1, MirrorNodeClient.ORDER.DESC), requestId);
292310
}
293311

294312
public getLimitOrderQueryParam(limit: number, order: string): ILimitOrderParams {
295313
return { limit: limit, order: order };
296314
}
297315

298-
public async getNetworkExchangeRate(timestamp?: string) {
316+
public async getNetworkExchangeRate(timestamp?: string, requestId?: string) {
299317
const queryParamObject = {};
300318
this.setQueryParam(queryParamObject, 'timestamp', timestamp);
301319
const queryParams = this.getQueryParams(queryParamObject);
302320
return this.request(`${MirrorNodeClient.GET_NETWORK_EXCHANGERATE_ENDPOINT}${queryParams}`,
303321
MirrorNodeClient.GET_NETWORK_EXCHANGERATE_ENDPOINT,
304-
[400, 404]);
322+
[400, 404],
323+
requestId);
305324
}
306325

307-
public async getNetworkFees(timestamp?: string, order?: string) {
326+
public async getNetworkFees(timestamp?: string, order?: string, requestId?: string) {
308327
const queryParamObject = {};
309328
this.setQueryParam(queryParamObject, 'timestamp', timestamp);
310329
this.setQueryParam(queryParamObject, 'order', order);
311330
const queryParams = this.getQueryParams(queryParamObject);
312331
return this.request(`${MirrorNodeClient.GET_NETWORK_FEES_ENDPOINT}${queryParams}`,
313332
MirrorNodeClient.GET_NETWORK_FEES_ENDPOINT,
314-
[400, 404]);
333+
[400, 404],
334+
requestId);
315335
}
316336

317337
private static getContractResultsByAddressPath(address: string) {
@@ -362,15 +382,15 @@ export class MirrorNodeClient {
362382
}
363383
}
364384

365-
public async resolveEntityType(entityIdentifier: string) {
366-
const contractResult = await this.getContract(entityIdentifier);
385+
public async resolveEntityType(entityIdentifier: string, requestId?: string) {
386+
const contractResult = await this.getContract(entityIdentifier, requestId);
367387
if (contractResult) {
368388
return {
369389
type: constants.TYPE_CONTRACT,
370390
entity: contractResult
371391
};
372392
}
373-
const accountResult = await this.getAccount(entityIdentifier);
393+
const accountResult = await this.getAccount(entityIdentifier, requestId);
374394
if (accountResult) {
375395
return {
376396
type: constants.TYPE_ACCOUNT,

0 commit comments

Comments
 (0)