Skip to content

Commit bfab2a4

Browse files
committed
feat(satp): multi gateway metrics
Signed-off-by: Jorge Santos <[email protected]>
1 parent dd99ce6 commit bfab2a4

File tree

9 files changed

+364
-481
lines changed

9 files changed

+364
-481
lines changed

packages/cactus-plugin-satp-hermes/src/main/typescript/core/session-utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,3 +1057,26 @@ export function getMessageInSessionData(
10571057
throw new Error("Message type not found");
10581058
}
10591059
}
1060+
1061+
export function collectSessionAttributes(
1062+
session: SATPSession,
1063+
sessionSide: "client" | "server",
1064+
) {
1065+
let data: SessionData;
1066+
if (sessionSide === "client") {
1067+
data = session.getClientSessionData?.() || {};
1068+
} else {
1069+
data = session.getServerSessionData?.() || {};
1070+
}
1071+
1072+
return {
1073+
senderNetworkId: data.senderAsset?.networkId?.id,
1074+
receiverNetworkId: data.receiverAsset?.networkId?.id,
1075+
senderGatewayNetworkId: data.senderGatewayNetworkId,
1076+
receiverGatewayNetworkId: data.recipientGatewayNetworkId,
1077+
assetProfileId: data.assetProfileId,
1078+
sessionId: session.getSessionId?.(),
1079+
sourceLedgerAssetId: data.sourceLedgerAssetId,
1080+
recipientLedgerAssetId: data.recipientLedgerAssetId,
1081+
};
1082+
}

packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-handlers/stage0-handler.ts

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ import {
2626
SenderGatewayNetworkIdError,
2727
SessionNotFoundError,
2828
} from "../errors/satp-handler-errors";
29-
import { saveMessageInSessionData, setError } from "../session-utils";
29+
import {
30+
collectSessionAttributes,
31+
saveMessageInSessionData,
32+
setError,
33+
} from "../session-utils";
3034
import { MessageType } from "../../generated/proto/cacti/satp/v02/common/message_pb";
3135
import { getMessageTypeName } from "../satp-utils";
3236
import { MonitorService } from "../../services/monitoring/monitor";
@@ -78,10 +82,6 @@ export class Stage0SATPHandler implements SATPHandler {
7882
const fnTag = `${this.getHandlerIdentifier()}#${stepTag}`;
7983
const { span, context: ctx } = this.monitorService.startSpan(fnTag);
8084
return context.with(ctx, async () => {
81-
const attributes: Record<
82-
string,
83-
undefined | string | number | boolean | string[] | number[] | boolean[]
84-
> = {};
8585
let session: SATPSession | undefined;
8686
try {
8787
this.Log.debug(`${fnTag}, New Session...`);
@@ -123,26 +123,16 @@ export class Stage0SATPHandler implements SATPHandler {
123123

124124
saveMessageInSessionData(session.getServerSessionData(), message);
125125

126-
attributes.senderNetworkId =
127-
session?.getServerSessionData().senderAsset?.networkId?.id ||
128-
undefined;
129-
attributes.receiverNetworkId =
130-
session?.getServerSessionData().receiverAsset?.networkId?.id ||
131-
undefined;
132-
attributes.senderGatewayNetworkId =
133-
session?.getServerSessionData().senderGatewayNetworkId || undefined;
134-
attributes.receiverGatewayNetworkId =
135-
session?.getServerSessionData().recipientGatewayNetworkId ||
136-
undefined;
137-
attributes.assetProfileId =
138-
session?.getServerSessionData().assetProfileId || undefined;
139-
attributes.sessionId = session?.getSessionId() || undefined;
140-
attributes.sourceLedgerAssetId =
141-
session?.getServerSessionData().sourceLedgerAssetId || undefined;
142-
attributes.recipientLedgerAssetId =
143-
session?.getServerSessionData().recipientLedgerAssetId || undefined;
144-
attributes.satp_phase = 0;
145-
attributes.operation = "newSession";
126+
const attributes: Record<
127+
string,
128+
| undefined
129+
| string
130+
| number
131+
| boolean
132+
| string[]
133+
| number[]
134+
| boolean[]
135+
> = collectSessionAttributes(session, "server");
146136

147137
const startTimestamp =
148138
session.getServerSessionData().receivedTimestamps?.stage0
@@ -156,7 +146,11 @@ export class Stage0SATPHandler implements SATPHandler {
156146
await this.monitorService.recordHistogram(
157147
"operation_duration",
158148
duration,
159-
attributes,
149+
{ ...attributes, satp_phase: 0, operation: "newSession" },
150+
);
151+
} else {
152+
this.Log.warn(
153+
`${fnTag}, Missing timestamps for operation duration calculation`,
160154
);
161155
}
162156

@@ -187,7 +181,7 @@ export class Stage0SATPHandler implements SATPHandler {
187181
const fnTag = `${this.getHandlerIdentifier()}#${stepTag}`;
188182
const { span, context: ctx } = this.monitorService.startSpan(fnTag);
189183
return context.with(ctx, async () => {
190-
const attributes: Record<
184+
let attributes: Record<
191185
string,
192186
undefined | string | number | boolean | string[] | number[] | boolean[]
193187
> = {};
@@ -226,26 +220,7 @@ export class Stage0SATPHandler implements SATPHandler {
226220

227221
saveMessageInSessionData(session.getServerSessionData(), message);
228222

229-
attributes.senderNetworkId =
230-
session?.getServerSessionData().senderAsset?.networkId?.id ||
231-
undefined;
232-
attributes.receiverNetworkId =
233-
session?.getServerSessionData().receiverAsset?.networkId?.id ||
234-
undefined;
235-
attributes.senderGatewayNetworkId =
236-
session?.getServerSessionData().senderGatewayNetworkId || undefined;
237-
attributes.receiverGatewayNetworkId =
238-
session?.getServerSessionData().recipientGatewayNetworkId ||
239-
undefined;
240-
attributes.assetProfileId =
241-
session?.getServerSessionData().assetProfileId || undefined;
242-
attributes.sessionId = session?.getSessionId() || undefined;
243-
attributes.sourceLedgerAssetId =
244-
session?.getServerSessionData().sourceLedgerAssetId || undefined;
245-
attributes.recipientLedgerAssetId =
246-
session?.getServerSessionData().recipientLedgerAssetId || undefined;
247-
attributes.satp_phase = 0;
248-
attributes.operation = "preSATPTransfer";
223+
attributes = collectSessionAttributes(session, "server");
249224

250225
const startTimestamp =
251226
session.getServerSessionData().receivedTimestamps?.stage0
@@ -259,7 +234,11 @@ export class Stage0SATPHandler implements SATPHandler {
259234
await this.monitorService.recordHistogram(
260235
"operation_duration",
261236
duration,
262-
attributes,
237+
{ ...attributes, satp_phase: 0, operation: "preSATPTransfer" },
238+
);
239+
} else {
240+
this.Log.warn(
241+
`${fnTag}, Missing timestamps for operation duration calculation`,
263242
);
264243
}
265244

packages/cactus-plugin-satp-hermes/src/main/typescript/core/stage-handlers/stage1-handler.ts

Lines changed: 38 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ import { PreSATPTransferResponse } from "../../generated/proto/cacti/satp/v02/se
2727
import { stringify as safeStableStringify } from "safe-stable-stringify";
2828
import { getMessageTypeName } from "../satp-utils";
2929
import { MessageType } from "../../generated/proto/cacti/satp/v02/common/message_pb";
30-
import { saveMessageInSessionData, setError } from "../session-utils";
30+
import {
31+
collectSessionAttributes,
32+
saveMessageInSessionData,
33+
setError,
34+
} from "../session-utils";
3135
import { BridgeManagerClientInterface } from "../../cross-chain-mechanisms/bridge/interfaces/bridge-manager-client-interface";
3236
import { MonitorService } from "../../services/monitoring/monitor";
3337
import { context, SpanStatusCode } from "@opentelemetry/api";
@@ -77,10 +81,6 @@ export class Stage1SATPHandler implements SATPHandler {
7781
const fnTag = `${this.getHandlerIdentifier()}#${stepTag}`;
7882
const { span, context: ctx } = this.monitorService.startSpan(fnTag);
7983
return context.with(ctx, async () => {
80-
const attributes: Record<
81-
string,
82-
undefined | string | number | boolean | string[] | number[] | boolean[]
83-
> = {};
8484
let session: SATPSession | undefined;
8585
try {
8686
this.Log.debug(`${fnTag}, Transfer Proposal...`);
@@ -124,33 +124,23 @@ export class Stage1SATPHandler implements SATPHandler {
124124
span.setAttribute("sessionId", session.getSessionId());
125125
span.setAttribute(
126126
"senderNetworkId",
127-
session?.getServerSessionData().senderAsset?.networkId?.id ?? "",
127+
session.getServerSessionData().senderAsset?.networkId?.id ?? "",
128128
);
129129
span.setAttribute(
130130
"receiverNetworkId",
131-
session?.getServerSessionData().receiverAsset?.networkId?.id ?? "",
131+
session.getServerSessionData().receiverAsset?.networkId?.id ?? "",
132132
);
133133

134-
attributes.senderNetworkId =
135-
session?.getServerSessionData().senderAsset?.networkId?.id ||
136-
undefined;
137-
attributes.receiverNetworkId =
138-
session?.getServerSessionData().receiverAsset?.networkId?.id ||
139-
undefined;
140-
attributes.senderGatewayNetworkId =
141-
session?.getServerSessionData().senderGatewayNetworkId || undefined;
142-
attributes.receiverGatewayNetworkId =
143-
session?.getServerSessionData().recipientGatewayNetworkId ||
144-
undefined;
145-
attributes.assetProfileId =
146-
session?.getServerSessionData().assetProfileId || undefined;
147-
attributes.sessionId = session?.getSessionId() || undefined;
148-
attributes.sourceLedgerAssetId =
149-
session?.getServerSessionData().sourceLedgerAssetId || undefined;
150-
attributes.recipientLedgerAssetId =
151-
session?.getServerSessionData().recipientLedgerAssetId || undefined;
152-
attributes.satp_phase = 1;
153-
attributes.operation = "transferProposal";
134+
const attributes: Record<
135+
string,
136+
| undefined
137+
| string
138+
| number
139+
| boolean
140+
| string[]
141+
| number[]
142+
| boolean[]
143+
> = collectSessionAttributes(session, "server");
154144

155145
const startTimestamp =
156146
session.getServerSessionData().receivedTimestamps?.stage1
@@ -164,7 +154,11 @@ export class Stage1SATPHandler implements SATPHandler {
164154
await this.monitorService.recordHistogram(
165155
"operation_duration",
166156
duration,
167-
attributes,
157+
{ ...attributes, satp_phase: 1, operation: "transferProposal" },
158+
);
159+
} else {
160+
this.Log.warn(
161+
`${fnTag}, Missing timestamps for operation duration calculation`,
168162
);
169163
}
170164

@@ -198,7 +192,7 @@ export class Stage1SATPHandler implements SATPHandler {
198192
const fnTag = `${this.getHandlerIdentifier()}#${stepTag}`;
199193
const { span, context: ctx } = this.monitorService.startSpan(fnTag);
200194
return context.with(ctx, async () => {
201-
const attributes: Record<
195+
let attributes: Record<
202196
string,
203197
undefined | string | number | boolean | string[] | number[] | boolean[]
204198
> = {};
@@ -215,31 +209,14 @@ export class Stage1SATPHandler implements SATPHandler {
215209
span.setAttribute("sessionId", session.getSessionId());
216210
span.setAttribute(
217211
"senderNetworkId",
218-
session?.getServerSessionData().senderAsset?.networkId?.id ?? "",
212+
session.getServerSessionData().senderAsset?.networkId?.id ?? "",
219213
);
220214
span.setAttribute(
221215
"receiverNetworkId",
222-
session?.getServerSessionData().receiverAsset?.networkId?.id ?? "",
216+
session.getServerSessionData().receiverAsset?.networkId?.id ?? "",
223217
);
224218

225-
attributes.senderNetworkId =
226-
session?.getServerSessionData().senderAsset?.networkId?.id ||
227-
undefined;
228-
attributes.receiverNetworkId =
229-
session?.getServerSessionData().receiverAsset?.networkId?.id ||
230-
undefined;
231-
attributes.senderGatewayNetworkId =
232-
session?.getServerSessionData().senderGatewayNetworkId || undefined;
233-
attributes.receiverGatewayNetworkId =
234-
session?.getServerSessionData().recipientGatewayNetworkId ||
235-
undefined;
236-
attributes.assetProfileId =
237-
session?.getServerSessionData().assetProfileId || undefined;
238-
attributes.sessionId = session?.getSessionId() || undefined;
239-
attributes.sourceLedgerAssetId =
240-
session?.getServerSessionData().sourceLedgerAssetId || undefined;
241-
attributes.recipientLedgerAssetId =
242-
session?.getServerSessionData().recipientLedgerAssetId || undefined;
219+
attributes = collectSessionAttributes(session, "server");
243220

244221
this.monitorService.updateCounter(
245222
"initiated_transactions",
@@ -277,28 +254,6 @@ export class Stage1SATPHandler implements SATPHandler {
277254
}
278255

279256
saveMessageInSessionData(session.getServerSessionData(), message);
280-
281-
attributes.senderNetworkId =
282-
session?.getServerSessionData().senderAsset?.networkId?.id ||
283-
undefined;
284-
attributes.receiverNetworkId =
285-
session?.getServerSessionData().receiverAsset?.networkId?.id ||
286-
undefined;
287-
attributes.senderGatewayNetworkId =
288-
session?.getServerSessionData().senderGatewayNetworkId || undefined;
289-
attributes.receiverGatewayNetworkId =
290-
session?.getServerSessionData().recipientGatewayNetworkId ||
291-
undefined;
292-
attributes.assetProfileId =
293-
session?.getServerSessionData().assetProfileId || undefined;
294-
attributes.sessionId = session?.getSessionId() || undefined;
295-
attributes.sourceLedgerAssetId =
296-
session?.getServerSessionData().sourceLedgerAssetId || undefined;
297-
attributes.recipientLedgerAssetId =
298-
session?.getServerSessionData().recipientLedgerAssetId || undefined;
299-
attributes.satp_phase = 1;
300-
attributes.operation = "transferCommence";
301-
302257
const startTimestamp =
303258
session.getServerSessionData().receivedTimestamps?.stage1
304259
?.transferCommenceRequestMessageTimestamp;
@@ -311,7 +266,11 @@ export class Stage1SATPHandler implements SATPHandler {
311266
await this.monitorService.recordHistogram(
312267
"operation_duration",
313268
duration,
314-
attributes,
269+
{ ...attributes, satp_phase: 1, operation: "transferCommence" },
270+
);
271+
} else {
272+
this.Log.warn(
273+
`${fnTag}, Missing timestamps for operation duration calculation`,
315274
);
316275
}
317276

@@ -325,26 +284,9 @@ export class Stage1SATPHandler implements SATPHandler {
325284
)}`,
326285
);
327286
setError(session, MessageType.TRANSFER_COMMENCE_RESPONSE, error);
328-
329-
attributes.senderNetworkId =
330-
session?.getServerSessionData().senderAsset?.networkId?.id ||
331-
undefined;
332-
attributes.receiverNetworkId =
333-
session?.getServerSessionData().receiverAsset?.networkId?.id ||
334-
undefined;
335-
attributes.senderGatewayNetworkId =
336-
session?.getServerSessionData().senderGatewayNetworkId || undefined;
337-
attributes.receiverGatewayNetworkId =
338-
session?.getServerSessionData().recipientGatewayNetworkId ||
339-
undefined;
340-
attributes.assetProfileId =
341-
session?.getServerSessionData().assetProfileId || undefined;
342-
attributes.sessionId = session?.getSessionId() || undefined;
343-
attributes.sourceLedgerAssetId =
344-
session?.getServerSessionData().sourceLedgerAssetId || undefined;
345-
attributes.recipientLedgerAssetId =
346-
session?.getServerSessionData().recipientLedgerAssetId || undefined;
347-
attributes.satp_phase = 1;
287+
if (session) {
288+
attributes = collectSessionAttributes(session, "server");
289+
}
348290

349291
this.monitorService.updateCounter(
350292
"ongoing_transactions",
@@ -468,7 +410,7 @@ export class Stage1SATPHandler implements SATPHandler {
468410
const fnTag = `${this.getHandlerIdentifier()}#${stepTag}`;
469411
const { span, context: ctx } = this.monitorService.startSpan(fnTag);
470412
return context.with(ctx, async () => {
471-
const attributes: Record<
413+
let attributes: Record<
472414
string,
473415
undefined | string | number | boolean | string[] | number[] | boolean[]
474416
> = {};
@@ -522,26 +464,9 @@ export class Stage1SATPHandler implements SATPHandler {
522464
);
523465
setError(session, MessageType.TRANSFER_COMMENCE_REQUEST, error);
524466

525-
attributes.senderNetworkId =
526-
session?.getClientSessionData()?.senderAsset?.networkId?.id ||
527-
undefined;
528-
attributes.receiverNetworkId =
529-
session?.getClientSessionData()?.receiverAsset?.networkId?.id ||
530-
undefined;
531-
attributes.senderGatewayNetworkId =
532-
session?.getClientSessionData()?.senderGatewayNetworkId ||
533-
undefined;
534-
attributes.receiverGatewayNetworkId =
535-
session?.getClientSessionData()?.recipientGatewayNetworkId ||
536-
undefined;
537-
attributes.assetProfileId =
538-
session?.getClientSessionData()?.assetProfileId || undefined;
539-
attributes.sessionId = session?.getSessionId() || undefined;
540-
attributes.sourceLedgerAssetId =
541-
session?.getClientSessionData()?.sourceLedgerAssetId || undefined;
542-
attributes.recipientLedgerAssetId =
543-
session?.getClientSessionData()?.recipientLedgerAssetId ||
544-
undefined;
467+
if (session) {
468+
attributes = collectSessionAttributes(session, "client");
469+
}
545470
attributes.satp_phase = 1;
546471

547472
this.monitorService.updateCounter(

0 commit comments

Comments
 (0)