sensu-go icon indicating copy to clipboard operation
sensu-go copied to clipboard

Javascript mutator event object should allow deletes

Open calebhailey opened this issue 2 years ago • 1 comments

The event object seeded into the Javascript Mutator sandbox doesn't currently support deletes (e.g. delete event.metrics). The event object is not immutable (which would be ironic 😅 for a feature called "Mutator"), but because it is the actual Go Event object/struct we cannot delete fields, which is a valuable mutation strategy.

This can be worked around by cloning the event object (e.g. var e = JSON.parse(JSON.stringify(event));), however this is problematic from a memory consumption perspective and likely detrimental to performance at scale.

Possible implementation

The Javascript mutator documentation currently suggests cloning of events via JSON.parse(JSON.stringify(event));. Any change that yields an identical object from JSON.parse(JSON.stringify(event)); before/after the change should suffice.

NOTE: the current implementation supports access to event object fields using the Go struct field names (e.g. event.Metrics) or the configured JSON equivalent field name as defined in the struct field tag (e.g. event.metrics). However, such support is not documented and unintended, so I would not consider breaking mutators that reference event fields via the Go struct field names as a breaking change. As long as the documented pattern of event cloning (i.e. JSON.parse(JSON.stringify(event));) yields an identical object, I would consider the change safe for a minor or patch release.

calebhailey avatar May 11 '22 21:05 calebhailey

To clarify, the event object is a Go object (with all that entails) and not a Javascript object, so it's impossible to modify its layout. However, it would be possible to set the metrics to an empty data structure, just awkward.

echlebek avatar May 11 '22 21:05 echlebek