Merge pull request #12637 from Budibase/track-run-js
This commit is contained in:
commit
d3b7e51f39
|
@ -30,7 +30,7 @@ export class ExecutionTimeTracker {
|
||||||
return new ExecutionTimeTracker(limitMs)
|
return new ExecutionTimeTracker(limitMs)
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(private limitMs: number) {}
|
constructor(readonly limitMs: number) {}
|
||||||
|
|
||||||
private totalTimeMs = 0
|
private totalTimeMs = 0
|
||||||
|
|
||||||
|
@ -46,6 +46,10 @@ export class ExecutionTimeTracker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get elapsedMS() {
|
||||||
|
return this.totalTimeMs
|
||||||
|
}
|
||||||
|
|
||||||
private checkLimit() {
|
private checkLimit() {
|
||||||
if (this.totalTimeMs > this.limitMs) {
|
if (this.totalTimeMs > this.limitMs) {
|
||||||
throw new ExecutionTimeoutError(
|
throw new ExecutionTimeoutError(
|
||||||
|
|
|
@ -2,35 +2,44 @@ import vm from "vm"
|
||||||
import env from "./environment"
|
import env from "./environment"
|
||||||
import { setJSRunner } from "@budibase/string-templates"
|
import { setJSRunner } from "@budibase/string-templates"
|
||||||
import { context, timers } from "@budibase/backend-core"
|
import { context, timers } from "@budibase/backend-core"
|
||||||
|
import tracer from "dd-trace"
|
||||||
|
|
||||||
type TrackerFn = <T>(f: () => T) => T
|
type TrackerFn = <T>(f: () => T) => T
|
||||||
|
|
||||||
export function init() {
|
export function init() {
|
||||||
setJSRunner((js: string, ctx: vm.Context) => {
|
setJSRunner((js: string, ctx: vm.Context) => {
|
||||||
const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS
|
return tracer.trace("runJS", {}, span => {
|
||||||
let track: TrackerFn = f => f()
|
const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS
|
||||||
if (perRequestLimit) {
|
let track: TrackerFn = f => f()
|
||||||
const bbCtx = context.getCurrentContext()
|
if (perRequestLimit) {
|
||||||
if (bbCtx) {
|
const bbCtx = context.getCurrentContext()
|
||||||
if (!bbCtx.jsExecutionTracker) {
|
if (bbCtx) {
|
||||||
bbCtx.jsExecutionTracker =
|
if (!bbCtx.jsExecutionTracker) {
|
||||||
timers.ExecutionTimeTracker.withLimit(perRequestLimit)
|
bbCtx.jsExecutionTracker =
|
||||||
|
timers.ExecutionTimeTracker.withLimit(perRequestLimit)
|
||||||
|
}
|
||||||
|
track = bbCtx.jsExecutionTracker.track.bind(bbCtx.jsExecutionTracker)
|
||||||
|
span?.addTags({
|
||||||
|
js: {
|
||||||
|
limitMS: bbCtx.jsExecutionTracker.limitMs,
|
||||||
|
elapsedMS: bbCtx.jsExecutionTracker.elapsedMS,
|
||||||
|
},
|
||||||
|
})
|
||||||
}
|
}
|
||||||
track = bbCtx.jsExecutionTracker.track.bind(bbCtx.jsExecutionTracker)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
ctx = {
|
ctx = {
|
||||||
...ctx,
|
...ctx,
|
||||||
alert: undefined,
|
alert: undefined,
|
||||||
setInterval: undefined,
|
setInterval: undefined,
|
||||||
setTimeout: undefined,
|
setTimeout: undefined,
|
||||||
}
|
}
|
||||||
vm.createContext(ctx)
|
vm.createContext(ctx)
|
||||||
return track(() =>
|
return track(() =>
|
||||||
vm.runInNewContext(js, ctx, {
|
vm.runInNewContext(js, ctx, {
|
||||||
timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS,
|
timeout: env.JS_PER_EXECUTION_TIME_LIMIT_MS,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue