Create custom Synthetics shared library

Is it possible to create and upload shared javascript library for Synthetic monitors?

I have several standard functions that are used across Synthetic monitors. Is there a way to upload a shared library where these could be reused with a statement such as below?

var myLib = require('mySharedLibrary');

At this point, I am looking into generating the scripts locally from reusable code snippets and using the api to uploaded the generated code.

Regards,
Lee

2 Likes

Hi, @lee.blackburn: That is not possible with public monitoring locations, but it may be possible with private minions. You might also try the workaround described here:

While pulling in a script from a Github git is interesting, and a bit terrifying, I am not sure it solves the function reuse problem. The gist would still need to include a copy of all the functions without any way to include each function into a different script.

I created a script to parse a template file and include function files to create the full scripts to be uploaded. This solves the copy/paste problem to some degree.

I already had scripts to download the monitor settings and scripts so we can save them in source control to see revision history. There are also scripts to upload the monitor script from local changes. I keep a copy of the full script to allow for and detect any changes via the editor in the New Relic Synthetics portal.

To give a little more context, I am reusing functions within the script across different testing scenarios. The script logic is not the same between the different scripts.

We also have the same issue that each script is used in multiple environments. For this case, we have a strategy to clone from existing scripts and create or update the monitor using the API. A simple diff locally can tell us if these different environments script are out of sync.

Hi @lee.blackburn as far as I know the only way to do this currently would be using a Private Minion, with the functions contained in a Custom Module that you could pull into the script

1 Like

Hey, I’ve managed to solve this by using webpack to bundle the tests before uploading them on NewRelicSynthetics.

It can work with a simple config file like this

webpack.config.js

const entryPlus = require('webpack-entry-plus');
const glob = require('glob');

// If all the tests are under tests/ folder, use webpack-entry-plus to create a bundled file per test 
const entryFiles = [
  {
    entryFiles: glob.sync('./tests/*.js'),
    outputName(item) {
      return item.replace('tests/', '');
    }
  }
];

module.exports = {
  entry: entryPlus(entryFiles),
  target: 'node',
  // Do not bundle chai as it is included in NewRelic
  externals: {
    'chai': 'commonjs2 chai'
  },
  output: {
    filename: '[name]',
  },
  // Allow the output code to be readable
  optimization: {
    minimize: false,
  },
};

More on webpack setup can be found here.

Thanks for sharing @olia.kremmyda! @lee.blackburn feel free to let us know if this is helpful to you!