Skip to content

Commit 543ecdb

Browse files
committed
add slow query logging
1 parent 38a1253 commit 543ecdb

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

src/swapService/config/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export const SWAP_DEFAULT_DEADLINE = 1800n
22
export const BINARY_SEARCH_TIMEOUT_SECONDS = 30
3+
export const LOG_SLOW_QUERY_TIMEOUT_SECONDS = 1

src/swapService/runner.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ export async function runPipeline(
6767
amountOutMin: finalResult.quotes[0].amountOutMin,
6868
route: finalResult.quotes[0].route,
6969
})
70-
console.log(
71-
finalResult.quotes
72-
.map((q) => q.route.map((r) => r.providerName).join(" "))
73-
.join(", "),
74-
)
70+
// console.log(
71+
// finalResult.quotes
72+
// .map((q) => q.route.map((r) => r.providerName).join(" "))
73+
// .join(", "),
74+
// )
7575

7676
// console.log('finalResult.quotes: ', JSON.stringify(finalResult.quotes, null, 2));
7777
return finalResult.quotes

src/swapService/strategies/balmySDK/customSourceList.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import type { IFetchService, IProviderService } from "@balmy/sdk"
1+
import { LOG_SLOW_QUERY_TIMEOUT_SECONDS } from "@/swapService/config/constants"
2+
import type {
3+
IFetchService,
4+
IProviderService,
5+
SourceId,
6+
SourceListQuoteRequest,
7+
SourceListQuoteResponse,
8+
} from "@balmy/sdk"
29
import { LocalSourceList } from "@balmy/sdk/dist/services/quotes/source-lists/local-source-list"
310
import { CustomKyberswapQuoteSource } from "./sources/kyberswapQuoteSource"
411
import { CustomLiFiQuoteSource } from "./sources/lifiQuoteSource"
@@ -45,5 +52,40 @@ export class CustomSourceList extends LocalSourceList {
4552
...customSources,
4653
}
4754
delete mutableThis.sources.balmy
55+
56+
// wrap getQuote in timer
57+
const getQuoteSuper = mutableThis.getQuote.bind(this)
58+
59+
mutableThis.getQuote = async (
60+
request: SourceListQuoteRequest,
61+
sourceId: SourceId,
62+
): Promise<SourceListQuoteResponse> => {
63+
const startTime = process.hrtime()
64+
const result = await getQuoteSuper(request, sourceId)
65+
const elapsedSeconds = parseHrtimeToSeconds(process.hrtime(startTime))
66+
if (elapsedSeconds > LOG_SLOW_QUERY_TIMEOUT_SECONDS) {
67+
const { chainId, sellToken, buyToken, order } = request
68+
const requestGist = {
69+
chainId,
70+
sellToken,
71+
buyToken,
72+
order,
73+
}
74+
console.log(
75+
`SLOW QUERY: ${sourceId} ${elapsedSeconds}s ${stringify(requestGist)}`,
76+
)
77+
}
78+
return result
79+
}
4880
}
4981
}
82+
83+
function parseHrtimeToSeconds(hrtime: [number, number]) {
84+
return Number((hrtime[0] + hrtime[1] / 1e9).toFixed(3))
85+
}
86+
87+
function stringify(obj: object) {
88+
return JSON.stringify(obj, (_, v) =>
89+
typeof v === "bigint" ? v.toString() : v,
90+
)
91+
}

0 commit comments

Comments
 (0)