Instrument a Front-End App

The SeaLights Node.js agent can be used to instrument the Front-end code as well.

See 'Generating an Agent token' for instructions on how to generate a token prior to executing the commands.

See Generating a session ID for instructions on how to generate a session ID

Prerequisites

  • The front-end application was scanned

Instrumentation command

First, the front-end code must be scanned and instrumented to provide the needed information to the SeaLights server.

npx slnodejs instrument [--labid <Lab ID>] \
    --tokenfile /path/to/sltoken.txt --buildsessionidfile buildSessionId \
    --workspacepath dist --outputpath sl_dist \
    --splitPreambleIntoFile [--failbuild true]
  • Most scans should include the following flags:

    • --failbuild true (if scan fails, then it will throw an error that will allow the pipeline to fail, rather than fail silently)

  • In order to ignore specific files or folders during the Scan operation, please refer to Ignoring Files or Folders

This will generate a new folder ('sl_dist' in the command above) with the instrumented code: a few Sealights-specific API calls will be added to your JS file to report coverage dynamically as illustrated in the picture below. The content of this Sealights' generated folder needs to be deployed instead of your original code - on the application server - for the client browser to work against during your testing stages, but not in your production environment.

Note that if Sealights inline scripts are being blocked by CSP (Content-Security-Policy) headers then our dashboard will need to be whitelisted. See Content-Security-Policy (CSP) Header Quick Reference as a good resource to how to handle this In some cases, its possible to add a meta tag to do this as well, see: Content-Security-Policy Meta http-equiv Example

Deploying Sealights instrumented folder

After generating a new folder with the instrumented code of your Front-End application, the content of this folder needs to be deployed instead of your original code on the server for the client browser to work against. Below, you’ll find several Best Practices in use among Sealights customers:

Deploying the folder containing the instrumented code

For example, if your original folder was named 'dist' and the new folder with the instrumented code is 'sl_dist’, you can follow the steps below:

  1. Rename the original folder from ‘dist’ to ‘dist_original

  2. Use the instrumented folder instead of using one of the following options by renaming the ‘sl_dist’ to ‘dist

On Unix/Linux system you also have the ability to define a ‘dist’ soft link to the ‘sl_dist’ folder.

ln -s sl_dist dist

Deploying Instrumentation for several environments (Lab IDs)

If you need to instrument your front-end application for several environments and provide accordingly different LabIds (resp. to every context), additional commands are required to manage LabID parameters in your deployment steps. We have described below the most common solutions in use

Using slnodejs agent

You have the ability to split the single slnodejs scan command into a build scan command and several “instrumentations only” commands. For example, below, we’ll instrument the application for two different LabIDs (MyFirstLab and My-2nd-Lab)

Using a Search and Replace operation

Another solution is to execute the slnodejs scan --instrumentForBrowsers command with a generic labid that is replaced in the JS files (string operation) prior to the artifact deployment in each environment

Validating the Instrumentation and build number in the JS source from the Browser

In your browser, open the source code of your JS page: is there the Sealights' instrumentation code and the right build number at the beginning of the page ? If Yes, all is set.

Last updated

Was this helpful?