1
0
Fork 0
mirror of synced 2024-10-03 02:27:06 +13:00

Add traces to track running arbitrary JS.

This commit is contained in:
Sam Rose 2023-12-19 18:20:13 +00:00
parent 8072d9c2a7
commit 2e58f2cdde
No known key found for this signature in database
2 changed files with 36 additions and 23 deletions

View file

@ -30,7 +30,7 @@ export class ExecutionTimeTracker {
return new ExecutionTimeTracker(limitMs)
}
constructor(private limitMs: number) {}
constructor(readonly limitMs: number) {}
private totalTimeMs = 0
@ -46,6 +46,10 @@ export class ExecutionTimeTracker {
}
}
get elapsedMS() {
return this.totalTimeMs
}
private checkLimit() {
if (this.totalTimeMs > this.limitMs) {
throw new ExecutionTimeoutError(

View file

@ -2,11 +2,13 @@ import vm from "vm"
import env from "./environment"
import { setJSRunner } from "@budibase/string-templates"
import { context, timers } from "@budibase/backend-core"
import tracer from "dd-trace"
type TrackerFn = <T>(f: () => T) => T
export function init() {
setJSRunner((js: string, ctx: vm.Context) => {
return tracer.trace("runJS", {}, span => {
const perRequestLimit = env.JS_PER_REQUEST_TIME_LIMIT_MS
let track: TrackerFn = f => f()
if (perRequestLimit) {
@ -17,6 +19,12 @@ export function init() {
timers.ExecutionTimeTracker.withLimit(perRequestLimit)
}
track = bbCtx.jsExecutionTracker.track.bind(bbCtx.jsExecutionTracker)
span?.addTags({
js: {
limitMS: bbCtx.jsExecutionTracker.limitMs,
elapsedMS: bbCtx.jsExecutionTracker.elapsedMS,
},
})
}
}
@ -33,4 +41,5 @@ export function init() {
})
)
})
})
}