Merge pull request #12750 from Budibase/narrow-down-run-js-time

Move traces around in runJS to further pinpoint where time is spent.
This commit is contained in:
Sam Rose 2024-01-10 10:52:38 +00:00 committed by GitHub
commit ba96b5695e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 37 deletions

View File

@ -12,15 +12,19 @@ export function init() {
const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS
let track: TrackerFn = f => f() let track: TrackerFn = f => f()
if (perRequestLimit) { if (perRequestLimit) {
tracer.trace<any>("runJS.setupTracker", {}, span => { const bbCtx = tracer.trace("runJS.getCurrentContext", {}, span =>
const bbCtx = context.getCurrentContext() context.getCurrentContext()
)
if (bbCtx) { if (bbCtx) {
if (!bbCtx.jsExecutionTracker) { if (!bbCtx.jsExecutionTracker) {
span?.addTags({ span?.addTags({
createdExecutionTracker: true, createdExecutionTracker: true,
}) })
bbCtx.jsExecutionTracker = bbCtx.jsExecutionTracker = tracer.trace(
timers.ExecutionTimeTracker.withLimit(perRequestLimit) "runJS.createExecutionTimeTracker",
{},
span => timers.ExecutionTimeTracker.withLimit(perRequestLimit)
)
} }
span?.addTags({ span?.addTags({
js: { js: {
@ -30,34 +34,28 @@ export function init() {
}) })
// We call checkLimit() here to prevent paying the cost of creating // We call checkLimit() here to prevent paying the cost of creating
// a new VM context below when we don't need to. // a new VM context below when we don't need to.
bbCtx.jsExecutionTracker.checkLimit() tracer.trace("runJS.checkLimitAndBind", {}, span => {
track = bbCtx.jsExecutionTracker.track.bind( bbCtx.jsExecutionTracker!.checkLimit()
track = bbCtx.jsExecutionTracker!.track.bind(
bbCtx.jsExecutionTracker bbCtx.jsExecutionTracker
) )
}
}) })
} }
}
ctx = tracer.trace("runJS.ctxClone", {}, span => { ctx = {
return {
...ctx, ...ctx,
alert: undefined, alert: undefined,
setInterval: undefined, setInterval: undefined,
setTimeout: undefined, setTimeout: undefined,
} }
})
tracer.trace("runJS.vm.createContext", {}, span => {
vm.createContext(ctx) vm.createContext(ctx)
})
return track(() => return track(() =>
tracer.trace("runJS.vm.runInNewContext", {}, span =>
vm.runInNewContext(js, ctx, { vm.runInNewContext(js, ctx, {
timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS, timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS,
}) })
) )
)
}) })
}) })
} }