Merge pull request #12637 from Budibase/track-run-js

This commit is contained in:
Sam Rose 2023-12-20 09:49:01 +00:00 committed by GitHub
commit d3b7e51f39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 23 deletions

View File

@ -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(

View File

@ -2,11 +2,13 @@ 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) => {
return tracer.trace("runJS", {}, span => {
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) {
@ -17,6 +19,12 @@ export function init() {
timers.ExecutionTimeTracker.withLimit(perRequestLimit) timers.ExecutionTimeTracker.withLimit(perRequestLimit)
} }
track = bbCtx.jsExecutionTracker.track.bind(bbCtx.jsExecutionTracker) 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() {
}) })
) )
}) })
})
} }