diff --git a/gojaRuntime/gojaRuntime.go b/gojaRuntime/gojaRuntime.go index 6c78035..fb7c453 100644 --- a/gojaRuntime/gojaRuntime.go +++ b/gojaRuntime/gojaRuntime.go @@ -24,10 +24,11 @@ type ( } actionResult struct { - ConsoleLog []interface{} `json:"console_log"` - ConsoleError []interface{} `json:"console_error"` - Context *jsContext `json:"context"` - ExitResult interface{} `json:"exit_result"` + ConsoleLog []interface{} `json:"console_log"` + ConsoleError []interface{} `json:"console_error"` + Context *jsContext `json:"context"` + ExitResult interface{} `json:"exit_result"` + RunMetadata *runtimesRegistry.ExecutionMetadata `json:"run_metadata"` } introspectedExport struct { value interface{} @@ -46,6 +47,11 @@ type ( } ) +// ExecutionMetadata implements runtime_registry.ExecutionResult. +func (a *actionResult) ExecutionMetadata() runtimesRegistry.ExecutionMetadata { + return *a.RunMetadata +} + // BindingsFrom implements runtime_registry.IntrospectedExport. func (i introspectedExport) BindingsFrom(exportName string) map[string]runtimesRegistry.BindingSettings { return i.bindings @@ -162,14 +168,16 @@ func init() { registry.RegisterNativeModule("url", urlModule.Require) } +var __nativeModules = nativeModules{ + registered: map[string]*NativeModule{}, +} + func newGojaRunner() runtimesRegistry.Runner { runner := GojaRunnerV1{ cache: &gojaCache{ cache: map[string]*goja.Program{}, }, - nativeModules: nativeModules{ - registered: map[string]*NativeModule{}, - }, + nativeModules: __nativeModules, } return &runner } @@ -228,13 +236,13 @@ type NativeModule struct { name string } -func (runner GojaRunnerV1) RegisterNativeAPI(name string) *NativeModule { +func RegisterNativeAPI(name string) *NativeModule { result := &NativeModule{ functions: map[string]func(binding runtimesRegistry.BindingSettings, jsContext JsContext, args ...interface{}) (interface{}, error){}, modules: map[string]*NativeModule{}, name: name, } - runner.nativeModules.registered[name] = result + __nativeModules.registered[name] = result return result } @@ -344,6 +352,7 @@ func (e *GojaRunnerV1) Execute(ctx context.Context, workflow runtimesRegistry.Wo } executionResult.ExitResult = promise.Result().Export() + executionResult.RunMetadata.ExecutionDuration = time.Since(executionResult.RunMetadata.StartedAt) return executionResult, nil } @@ -360,6 +369,9 @@ func (runner *GojaRunnerV1) setupVM(ctx context.Context, vm *goja.Runtime, workf Context: &jsContext{ data: map[string]interface{}{}, }, + RunMetadata: &runtimesRegistry.ExecutionMetadata{ + StartedAt: time.Now(), + }, } for name, binding := range workflow.RequestedBindings { diff --git a/registry/runtimeRegistry.go b/registry/runtimeRegistry.go index 2a3a30d..df78524 100644 --- a/registry/runtimeRegistry.go +++ b/registry/runtimeRegistry.go @@ -32,11 +32,6 @@ type ( Settings map[string]interface{} `json:"settings"` } - // Bindings struct { - // Global map[string]ModuleBinding `json:"global"` - // Native map[string]ModuleBinding `json:"native"` - // } - RuntimeLimits struct { MaxExecutionDuration time.Duration `json:"max_execution_duration"` } @@ -52,7 +47,12 @@ type ( GetValueAsMap(key string) (map[string]interface{}, error) } + ExecutionMetadata struct { + StartedAt time.Time `json:"started_at"` + ExecutionDuration time.Duration `json:"execution_duration"` + } ExecutionResult interface { + ExecutionMetadata() ExecutionMetadata GetExitResult() interface{} GetConsoleLog() []interface{} GetConsoleError() []interface{} diff --git a/runtime_test.go b/runtime_test.go index 3d9d618..dcd5b32 100644 --- a/runtime_test.go +++ b/runtime_test.go @@ -50,6 +50,8 @@ func Test_GojaPrecompiledRuntime(t *testing.T) { idTokenMap, err := result.GetContext().GetValueAsMap("idToken") assert.Nil(err) assert.Equal("bbb", idTokenMap["aaa"]) + assert.Greater(result.ExecutionMetadata().ExecutionDuration.Nanoseconds(), int64(1)) + assert.False(result.ExecutionMetadata().StartedAt.IsZero()) } } @@ -113,7 +115,7 @@ func testExecution(workflow projectBundler.KindeWorkflow, assert *assert.Asserti func getGojaRunner() registry.Runner { runtime, _ := GetRuntime("goja") - kindeAPI := runtime.(*gojaRuntime.GojaRunnerV1).RegisterNativeAPI("kinde") + kindeAPI := gojaRuntime.RegisterNativeAPI("kinde") kindeAPI.RegisterNativeFunction("fetch", func(binding registry.BindingSettings, jsContext gojaRuntime.JsContext, args ...interface{}) (interface{}, error) { return "fetch response", nil })