Skip to content

feat: edit rpc_relay_consensusnode_* metrics #3876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/relay/src/lib/clients/mirrorNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1575,7 +1575,6 @@ export class MirrorNodeClient {
* Retrieves and processes transaction record metrics from the mirror node based on the provided transaction ID.
*
* @param {string} transactionId - The ID of the transaction for which the record is being retrieved.
* @param {string} callerName - The name of the caller requesting the transaction record.
* @param {string} txConstructorName - The name of the transaction constructor associated with the transaction.
* @param {string} operatorAccountId - The account ID of the operator, used to calculate transaction fees.
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
Expand All @@ -1584,7 +1583,6 @@ export class MirrorNodeClient {
*/
public async getTransactionRecordMetrics(
transactionId: string,
callerName: string,
txConstructorName: string,
operatorAccountId: string,
requestDetails: RequestDetails,
Expand All @@ -1593,7 +1591,7 @@ export class MirrorNodeClient {

if (this.logger.isLevelEnabled('debug')) {
this.logger.debug(
`${formattedRequestId} Get transaction record via mirror node: transactionId=${transactionId}, txConstructorName=${txConstructorName}, callerName=${callerName}`,
`${formattedRequestId} Get transaction record via mirror node: transactionId=${transactionId}, txConstructorName=${txConstructorName}`,
);
}

Expand All @@ -1611,7 +1609,7 @@ export class MirrorNodeClient {
);

if (!transactionRecords) {
const notFoundMessage = `No transaction record retrieved: transactionId=${transactionId}, txConstructorName=${txConstructorName}, callerName=${callerName}.`;
const notFoundMessage = `No transaction record retrieved: transactionId=${transactionId}, txConstructorName=${txConstructorName}.`;
throw new MirrorNodeClientError({ message: notFoundMessage }, MirrorNodeClientError.statusCodes.NOT_FOUND);
}

Expand Down
11 changes: 2 additions & 9 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,8 @@ export class SDKClient {
executionMode: constants.EXECUTION_MODE.QUERY,
transactionId: query.paymentTransactionId?.toString(),
txConstructorName: queryConstructorName,
callerName,
cost: queryCost,
gasUsed: 0,
interactingEntity,
status,
requestDetails,
originalCallerAddress,
Expand Down Expand Up @@ -487,11 +485,9 @@ export class SDKClient {
if (transactionId?.length) {
this.eventEmitter.emit(constants.EVENTS.EXECUTE_TRANSACTION, {
transactionId,
callerName,
requestDetails,
txConstructorName,
operatorAccountId: this.clientMain.operatorAccountId!.toString(),
interactingEntity,
originalCallerAddress,
} as IExecuteTransactionEventPayload);
}
Expand Down Expand Up @@ -558,11 +554,9 @@ export class SDKClient {
if (transactionResponse.transactionId) {
this.eventEmitter.emit(constants.EVENTS.EXECUTE_TRANSACTION, {
transactionId: transactionResponse.transactionId.toString(),
callerName,
requestDetails,
txConstructorName,
operatorAccountId: this.clientMain.operatorAccountId!.toString(),
interactingEntity,
originalCallerAddress,
} as IExecuteTransactionEventPayload);
}
Expand Down Expand Up @@ -736,7 +730,6 @@ export class SDKClient {
*/
public async getTransactionRecordMetrics(
transactionId: string,
callerName: string,
txConstructorName: string,
operatorAccountId: string,
requestDetails: RequestDetails,
Expand All @@ -747,7 +740,7 @@ export class SDKClient {
try {
if (this.logger.isLevelEnabled('debug')) {
this.logger.debug(
`${requestDetails.formattedRequestId} Get transaction record via consensus node: transactionId=${transactionId}, txConstructorName=${txConstructorName}, callerName=${callerName}`,
`${requestDetails.formattedRequestId} Get transaction record via consensus node: transactionId=${transactionId}, txConstructorName=${txConstructorName}`,
);
}

Expand All @@ -769,7 +762,7 @@ export class SDKClient {
const sdkClientError = new SDKClientError(e, e.message);
this.logger.warn(
e,
`${requestDetails.formattedRequestId} Error raised during TransactionRecordQuery: transactionId=${transactionId}, txConstructorName=${txConstructorName}, callerName=${callerName}, recordStatus=${sdkClientError.status} (${sdkClientError.status._code}), cost=${transactionFee}, gasUsed=${gasUsed}`,
`${requestDetails.formattedRequestId} Error raised during TransactionRecordQuery: transactionId=${transactionId}, txConstructorName=${txConstructorName}, recordStatus=${sdkClientError.status} (${sdkClientError.status._code}), cost=${transactionFee}, gasUsed=${gasUsed}`,
);
throw sdkClientError;
}
Expand Down
39 changes: 7 additions & 32 deletions packages/relay/src/lib/services/metricService/metricService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,14 @@ export default class MetricService {
* @returns {Promise<void>} - A promise that resolves when the transaction metrics have been captured.
*/
public async captureTransactionMetrics({
callerName,
transactionId,
txConstructorName,
operatorAccountId,
interactingEntity,
requestDetails,
originalCallerAddress,
}: IExecuteTransactionEventPayload): Promise<void> {
const transactionRecordMetrics = await this.getTransactionRecordMetrics(
transactionId,
callerName,
txConstructorName,
operatorAccountId,
requestDetails,
Expand All @@ -159,10 +156,8 @@ export default class MetricService {
executionMode: constants.EXECUTION_MODE.TRANSACTION,
transactionId,
txConstructorName,
callerName,
cost: transactionFee,
gasUsed,
interactingEntity,
status,
requestDetails,
originalCallerAddress,
Expand All @@ -174,10 +169,8 @@ export default class MetricService {
executionMode: constants.EXECUTION_MODE.RECORD,
transactionId,
txConstructorName,
callerName,
cost: txRecordChargeAmount,
gasUsed: 0,
interactingEntity,
status,
requestDetails,
originalCallerAddress,
Expand All @@ -193,10 +186,8 @@ export default class MetricService {
* @param {string} payload.executionMode - The mode of the execution (TRANSACTION, QUERY, RECORD).
* @param {string} payload.transactionId - The unique identifier for the transaction.
* @param {string} payload.txConstructorName - The name of the transaction constructor.
* @param {string} payload.callerName - The name of the entity calling the transaction.
* @param {number} payload.cost - The cost of the transaction in tinybars.
* @param {number} payload.gasUsed - The amount of gas used during the transaction.
* @param {string} payload.interactingEntity - The entity interacting with the transaction.
* @param {string} payload.status - The entity interacting with the transaction.
* @param {string} payload.requestDetails - The request details for logging and tracking.
* @param {string | undefined} payload.originalCallerAddress - The address of the original caller making the request.
Expand All @@ -206,22 +197,20 @@ export default class MetricService {
executionMode,
transactionId,
txConstructorName,
callerName,
cost,
gasUsed,
interactingEntity,
status,
requestDetails,
originalCallerAddress,
}: IExecuteQueryEventPayload): Promise<void> => {
if (this.logger.isLevelEnabled('debug')) {
this.logger.debug(
`${requestDetails.formattedRequestId} Capturing transaction fee charged to operator: executionMode=${executionMode} transactionId=${transactionId}, txConstructorName=${txConstructorName}, callerName=${callerName}, cost=${cost} tinybars`,
`${requestDetails.formattedRequestId} Capturing transaction fee charged to operator: executionMode=${executionMode} transactionId=${transactionId}, txConstructorName=${txConstructorName}, cost=${cost} tinybars`,
);
}

await this.hbarLimitService.addExpense(cost, originalCallerAddress ?? '', requestDetails);
this.captureMetrics(executionMode, txConstructorName, status, cost, gasUsed, callerName, interactingEntity);
this.captureMetrics(executionMode, txConstructorName, status, cost, gasUsed);
};

/**
Expand All @@ -235,7 +224,7 @@ export default class MetricService {
return new Histogram({
name: metricHistogramCost,
help: 'Relay consensusnode mode type status cost histogram',
labelNames: ['mode', 'type', 'status', 'caller', 'interactingEntity'],
labelNames: ['mode', 'type', 'status'],
registers: [register],
});
}
Expand All @@ -251,7 +240,7 @@ export default class MetricService {
return new Histogram({
name: metricHistogramGasFee,
help: 'Relay consensusnode mode type status gas fee histogram',
labelNames: ['mode', 'type', 'status', 'caller', 'interactingEntity'],
labelNames: ['mode', 'type', 'status'],
registers: [register],
});
}
Expand All @@ -275,21 +264,11 @@ export default class MetricService {
* @param {string} status - The status of the transaction.
* @param {number} cost - The cost of the transaction in tinybars.
* @param {number} gas - The gas used by the transaction.
* @param {string} caller - The name of the caller executing the transaction.
* @param {string} interactingEntity - The entity interacting with the transaction.
* @returns {void}
*/
private captureMetrics = (
mode: string,
type: string,
status: string,
cost: number,
gas: number,
caller: string,
interactingEntity: string,
): void => {
this.consensusNodeClientHistogramCost.labels(mode, type, status, caller, interactingEntity).observe(cost);
this.consensusNodeClientHistogramGasFee.labels(mode, type, status, caller, interactingEntity).observe(gas);
private captureMetrics = (mode: string, type: string, status: string, cost: number, gas: number): void => {
this.consensusNodeClientHistogramCost.labels(mode, type, status).observe(cost);
this.consensusNodeClientHistogramGasFee.labels(mode, type, status).observe(gas);
};

/**
Expand All @@ -298,15 +277,13 @@ export default class MetricService {
* consensus node via the SDK client or from the mirror node.
*
* @param {string} transactionId - The ID of the transaction for which metrics are being retrieved.
* @param {string} callerName - The name of the caller requesting the metrics.
* @param {string} txConstructorName - The name of the transaction constructor.
* @param {string} operatorAccountId - The account ID of the operator.
* @param {RequestDetails} requestDetails - The request details for logging and tracking.
* @returns {Promise<ITransactionRecordMetric | undefined>} - The transaction record metrics or undefined if retrieval fails.
*/
private async getTransactionRecordMetrics(
transactionId: string,
callerName: string,
txConstructorName: string,
operatorAccountId: string,
requestDetails: RequestDetails,
Expand All @@ -318,15 +295,13 @@ export default class MetricService {
if (defaultToConsensusNode) {
return await this.sdkClient.getTransactionRecordMetrics(
transactionId,
callerName,
txConstructorName,
operatorAccountId,
requestDetails,
);
} else {
return await this.mirrorNodeClient.getTransactionRecordMetrics(
transactionId,
callerName,
txConstructorName,
operatorAccountId,
requestDetails,
Expand Down
2 changes: 0 additions & 2 deletions packages/relay/src/lib/types/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ export interface IExecuteQueryEventPayload {
executionMode: string;
transactionId: string;
txConstructorName: string;
callerName: string;
cost: number;
gasUsed: number;
interactingEntity: string;
status: string;
requestDetails: RequestDetails;
originalCallerAddress: string | undefined;
Expand Down
4 changes: 1 addition & 3 deletions packages/relay/tests/lib/mirrorNodeClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,6 @@ describe('MirrorNodeClient', async function () {

const transactionRecordMetrics = await mirrorNodeInstance.getTransactionRecordMetrics(
mockedTransactionId,
mockedCallerName,
mockedConstructorName,
operatorAcocuntId,
requestDetails,
Expand All @@ -1591,15 +1590,14 @@ describe('MirrorNodeClient', async function () {
try {
await mirrorNodeInstance.getTransactionRecordMetrics(
mockedTransactionId,
mockedCallerName,
mockedConstructorName,
operatorAcocuntId,
requestDetails,
);

expect.fail('should have thrown an error');
} catch (error) {
const notFoundMessage = `No transaction record retrieved: transactionId=${mockedTransactionId}, txConstructorName=${mockedConstructorName}, callerName=${mockedCallerName}.`;
const notFoundMessage = `No transaction record retrieved: transactionId=${mockedTransactionId}, txConstructorName=${mockedConstructorName}.`;
const expectedError = new MirrorNodeClientError(
{ message: notFoundMessage },
MirrorNodeClientError.statusCodes.NOT_FOUND,
Expand Down
2 changes: 0 additions & 2 deletions packages/relay/tests/lib/sdkClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2808,7 +2808,6 @@ describe('SdkClient', async function () {

const transactionRecordMetrics = await sdkClient.getTransactionRecordMetrics(
transactionId.toString(),
mockedCallerName,
mockedConstructorName,
accountId.toString(),
requestDetails,
Expand All @@ -2827,7 +2826,6 @@ describe('SdkClient', async function () {
try {
await sdkClient.getTransactionRecordMetrics(
transactionId.toString(),
mockedCallerName,
mockedConstructorName,
accountId.toString(),
requestDetails,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ describe('Metric Service', function () {
)!;

expect(gasMetricObject.metricName).to.eq(metricHistogramGasFeeSumTitle);
expect(gasMetricObject.labels.caller).to.eq(mockedCallerName);
expect(gasMetricObject.labels.interactingEntity).to.eq(mockedInteractingEntity);
expect(gasMetricObject.value).to.eq(
mockedConsensusNodeTransactionRecord.contractFunctionResult?.gasUsed.toNumber(),
);
Expand All @@ -108,17 +106,13 @@ describe('Metric Service', function () {
);
});
expect(txRecordFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle);
expect(txRecordFeeMetricObject?.labels.caller).to.eq(mockedCallerName);
expect(txRecordFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity);
expect(txRecordFeeMetricObject?.value).to.eq(expectedTxRecordFee);
}

const transactionFeeMetricObject = metricObjects.values.find((metric) => {
return metric.labels.mode === executionMode && metric.metricName === metricHistogramCostSumTitle;
});
expect(transactionFeeMetricObject?.metricName).to.eq(metricHistogramCostSumTitle);
expect(transactionFeeMetricObject?.labels.caller).to.eq(mockedCallerName);
expect(transactionFeeMetricObject?.labels.interactingEntity).to.eq(mockedInteractingEntity);
expect(transactionFeeMetricObject?.value).to.eq(mockedTxFee);
};

Expand Down Expand Up @@ -281,10 +275,8 @@ describe('Metric Service', function () {
executionMode: constants.EXECUTION_MODE.QUERY,
transactionId: mockedTransactionId,
txConstructorName: mockedConstructorName,
callerName: mockedCallerName,
cost: mockedTxFee,
gasUsed: mockedGasUsed,
interactingEntity: mockedInteractingEntity,
status: 'SUCCESS',
requestDetails,
originalCallerAddress: mockedOriginalCallerAddress,
Expand Down
Loading