Skip to content

Commit 228640c

Browse files
committed
queue latency metrics
1 parent 102cd3b commit 228640c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/server/routes/system/queue.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4+
import { TransactionDB } from "../../../db/transactions/db";
5+
import { MinedTransaction } from "../../../utils/transaction/types";
46
import { MineTransactionQueue } from "../../../worker/queues/mineTransactionQueue";
57
import { SendTransactionQueue } from "../../../worker/queues/sendTransactionQueue";
68
import { standardResponseSchema } from "../../schemas/sharedApiSchemas";
@@ -9,6 +11,16 @@ const responseBodySchema = Type.Object({
911
result: Type.Object({
1012
queued: Type.Number(),
1113
pending: Type.Number(),
14+
latency: Type.Object({
15+
msToSend: Type.Object({
16+
p50: Type.Number(),
17+
p90: Type.Number(),
18+
}),
19+
msToMine: Type.Object({
20+
p50: Type.Number(),
21+
p90: Type.Number(),
22+
}),
23+
}),
1224
}),
1325
});
1426

@@ -30,13 +42,45 @@ export async function queueStatus(fastify: FastifyInstance) {
3042
},
3143
},
3244
handler: async (req, res) => {
45+
// Get # queued and sent transactions.
3346
const queued = await SendTransactionQueue.length();
3447
const pending = await MineTransactionQueue.length();
3548

49+
// Get last 1k mined transactions.
50+
const minedTransactionsList =
51+
await TransactionDB.getTransactionListByStatus({
52+
status: "mined",
53+
page: 1,
54+
limit: 1_000,
55+
});
56+
const minedTransactions =
57+
minedTransactionsList.transactions as MinedTransaction[];
58+
59+
// Get "queue -> send" and "queue -> mine" times.
60+
const msToSendArr: number[] = [];
61+
const msToMineArr: number[] = [];
62+
for (const transaction of minedTransactions) {
63+
const queuedAt = transaction.queuedAt.getTime();
64+
const sentAt = transaction.sentAt.getTime();
65+
const minedAt = transaction.minedAt.getTime();
66+
msToSendArr.push(sentAt - queuedAt);
67+
msToMineArr.push(minedAt - queuedAt);
68+
}
69+
3670
res.status(StatusCodes.OK).send({
3771
result: {
3872
queued,
3973
pending,
74+
latency: {
75+
msToSend: {
76+
p50: getPercentile(msToSendArr, 50),
77+
p90: getPercentile(msToSendArr, 90),
78+
},
79+
msToMine: {
80+
p50: getPercentile(msToMineArr, 50),
81+
p90: getPercentile(msToMineArr, 90),
82+
},
83+
},
4084
},
4185
});
4286
},

0 commit comments

Comments
 (0)