We have updated our Terms of Service, Code of Conduct, and Addendum.

Using VSCode + nodejs to write/debug your code function

Govardhanen Gopal
Govardhanen Gopal Posts: 19
edited October 18 in General Discussions

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.

Comments

  • Anson VanDoren
    Anson VanDoren Posts: 17 ✭✭

    Thanks for sharing! It's perhaps useful to share that you don't even necessarily need all those console.log() statements, and since you're already in VSCode you can just use the debugger to watch your variables change as you step through:

    1. Ctrl-Shift-P (or Cmd-Shift-P for MacFolk) to open the Command Palette, then type/select Debug: JavaScript Debug Terminal. This will open a new terminal window at the bottom of VSCode
    2. Set one or more breakpoints by clicking in the gutter next to the line you're interested in
    3. From the debug terminal you opened, run something like node cpu.js as mentioned above. You should see the debugger toolbar pop up somewhere and execution of your script will halt at the first breakpoint it hits
    4. From left to right, these buttons will:
      1. Resume execution until the next breakpoint or the end of the script
      2. Step over the current line (execute the line, but if the line is a function call it will not enter that function, just evaluate its result)
      3. Step into the line (if the line makes a function call, it will enter the function and pause)
      4. Step out of the function (if inside a function, stepping out will go back to the call-site and pause)
      5. Restart execution of the script
      6. Disconnect the debugger
    5. While the debugger is active, you'll see some new panes on the left side. If you don't see them, click on the Debugger icon on the left menubar
      1. Variables will show you the assigned value of all variables in scope. Note that when running in VSCode this will show you some global variables ( require, module, etc) that your Code Function in Stream won't have access to.
      2. Watch lets you enter a specific variable (or nested object path, array element, etc) that you want to monitor closely. The value will update as you step through the script
      3. Breakpoints shows you the current breakpoints you have set. Clicking on one will take you to the relevant line
      4. Call stack shows you what function calls have brought you to the current line. Probably not super useful in the context of developing a Code Function

  • Wow that is great, thank you so much for your pointers, I learnt something more new from you today.