TL;DR Use VSCode to test out javascript "code" function locally before attempting it in Cribl Stream/Edge.
I wanted to first give a shout out to @Jon Rust for pointing me in this direction. Paying it forward to other Cribl users who would like to give the code function a shot.
I am using the example from the following page
https://docs.cribl.io/stream/usecase-code-function/#building-a-new-array
I had a pipeline with about 40 functions which was used to take a bunch of metrics together and publish it as a multi-metric into splunk. I wrote this with David Maislins help about 3-4 years ago, and it was getting to be a bear to maintain and upate. The code function was the perfect candidate for this. I took 36 functions and consolidated into 1 code function which was much easier to debug. Please note I have NO experience with JS and this is just something I came up over 2 days of grinding.
Make sure you have VSCode + nodejs installed in your computer. Further take the full payload of your input and use the following site to JSON stringify your json object.
I used this string
{"cpus": [{"number": 1, "name": "CPU1", "value": 2.3},{"number": 2, "name": "CPU2", "value": 3.1},{"number": 3, "name": "CPU3", "value": 5.1},{"number": 4, "name": "CPU4", "value": 1.3}],"arch": "Intel x64"}
in this page and converted it into
https://jsonlint.com/json-stringify
"{"cpus":[{"number":1,"name":"CPU1","value":2.3},{"number":2,"name":"CPU2","value":3.1},{"number":3,"name":"CPU3","value":5.1},{"number":4,"name":"CPU4","value":1.3}],"arch":"Intel x64"}"
At this point you have the basic string ready to play around.
Here's the blob of code which will be able to test out. Uncomment the console.log when needed to look at your results.
const json = "{\"cpus\":[{\"number\":1,\"name\":\"CPU1\",\"value\":2.3},{\"number\":2,\"name\":\"CPU2\",\"value\":3.1},{\"number\":3,\"name\":\"CPU3\",\"value\":5.1},{\"number\":4,\"name\":\"CPU4\",\"value\":1.3}],\"arch\":\"Intel x64\"}"var __e = JSON.parse(json);// console.log(__e)__e['test'] = 'Hello, Goats!'// console.log(__e)__e['cpus_filtered'] = __e['cpus'].filter(entry => entry.value >= 3)// console.log(__e)__e['cpus_reduce'] = __e['cpus'].reduce((accumulator, entry) => accumulator + entry.value, 0)// console.log(__e)__e['cpus_some'] = __e['cpus'].some(entry => entry.value >= 3)// console.log(__e)__e['cpus_every'] = __e['cpus'].every(entry => entry.value >= 10)// console.log(__e)__e['cpus'] = __e['cpus'].map(entry => Object.assign(entry, {'name': entry.name.toLowerCase()}))// console.log(__e)let test = {};__e['cpus'].forEach((entry, index) => test[entry.name] = entry.value);__e['cpus_foreach'] = test;// console.log(__e)__e['cpus'].map(item => test[item.name] = item.value);// console.log(__e)__e['cpus_new'] = __e['cpus'].map(entry => { const container = {}; for (const [key, value] of Object.entries(entry)) { container[key] = value; } container['arch'] = __e['arch']; delete container.name; return container;})// console.log(__e)
You can now execute this code in the command line
node cpu.js ## and you will see the output.
Before you publish this into Cribl Stream make sure you have no console.log open, also comment out the const before you publish it. Do save the commented json payload for future debugging/testing. I hope somebody finds this useful.