From 06b986145a0d1526abd5b017dc8218e88179f092 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Fri, 13 Sep 2024 10:53:09 +0300 Subject: [PATCH] usage: report usage of `global` As part of https://github.com/grafana/k6/issues/3864 it will be nice to know how much this is used if at all. --- js/bundle.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/js/bundle.go b/js/bundle.go index 969c69f59fc..c84f165c241 100644 --- a/js/bundle.go +++ b/js/bundle.go @@ -387,7 +387,17 @@ func (b *Bundle) setupJSRuntime(rt *sobek.Runtime, vuID int64, logger logrus.Fie } if b.CompatibilityMode == lib.CompatibilityModeExtended { - err = rt.Set("global", rt.GlobalObject()) + globalThis := rt.GlobalObject() + err = globalThis.DefineAccessorProperty("global", + rt.ToValue(func() sobek.Value { + if err := b.preInitState.Usage.Uint64("usage/global", 1); err != nil { + b.preInitState.Logger.WithError(err).Warn("couldn't report usage") + } + return globalThis + }), rt.ToValue(func(newGlobal *sobek.Object) { // probably not a thing that will happen but still + globalThis = newGlobal + }), + sobek.FLAG_TRUE, sobek.FLAG_TRUE) if err != nil { return err }