Preconfigured templates and sample code let you build synthetic monitors in
Node.js and TypeScript using frameworks such as Puppeteer, Selenium WebDriver,
and Mocha. You can use these samples—available in the
synthetics-sdk-nodejs GitHub repository—to query
URLs, run browser-based user journeys, test API endpoints, or check for broken
links.
Guidelines for using samples and writing custom tests
Before you deploy a sample or write a custom test, review the following requirements:
- Update the target URL: Before you deploy any sample code, update the
urlfield to point to your target endpoint. Don't set this field tohttps://www.google.com. - Throw an
Erroron test failure: If you write custom tests instead of using a template, ensure that your test passes unless anErroris thrown. We recommend that you use theAssertlibrary so that any failures are attributed to the correct line of code.
Generic templates
The generic templates are configured to collect trace and log data for outbound HTTP requests. The solution leverages the OpenTelemetry auto-instrumentation-node module and the winston logger. Due to the dependency on open source products, you should expect changes in the structure of trace and log data. Therefore, the collected trace and log data should be used only for debugging purposes.
You can implement your own approach to collect trace and log data for outbound
HTTP requests. For an example of a custom approach,
see the class SyntheticAutoInstrumentation.
Generic Node.js sample
The generic-synthetic-nodejs sample illustrates
how to query a URL. This sample contains the same code as the default function
displayed by the Google Cloud console. To view the full sample, in the sample's toolbar,
select the GitHub logo.
TypeScript sample
The generic-synthetic-typescript sample
illustrates how to query a URL. To view the full sample, in the sample's toolbar,
select the GitHub logo.
Puppeteer template
If you use Puppeteer, then consider starting with the
generic-puppeteer-nodejs sample.
Required Puppeteer setup
To use Puppeteer, ensure that you complete the following steps:
Include
.puppeteerrc.cjsin the source directory of your Cloud Run function:Add the following script to the
package.jsonfile of your Cloud Run function:"scripts": { "gcp-build": "node node_modules/puppeteer/install.mjs" },
Puppeteer sample
The generic-puppeteer-nodejs sample illustrates how
to use Puppeteer with your Cloud Run function. To view the full sample, in the sample's toolbar,
select the GitHub logo.
Selenium WebDriver template
If you use Selenium WebDriver, then consider starting with the
generic-selenium-nodejs sample. The sample,
which is available on GitHub, includes index.js and package.json files.
To view the full sample, in the sample's toolbar, select the GitHub logo.
Mocha template
If you write tests that rely on the Mocha template,
then consider whether a sequence of tests should continue or stop when a
failure occurs. To stop a sequence of tests after a failure, you must set the
bail flag.
For an end-to-end example that includes deploying an API, writing a sample Mocha test suite for the API endpoints, and configuring the synthetic monitor, see the blog Google Cloud Synthetic Monitoring Tutorial.
The mocha-url-ok sample illustrates how
a Cloud Run function can invoke a
Mocha test suite, and it provides a sample test suite.
To view the full sample, in the sample's toolbar,
select the GitHub logo.
Broken-link template
The broken-links-ok
sample illustrates how to configure a broken-link checker.
For this template, you only specify the values of the options object. This
object specifies the URI to be tested and the parameters of the test.
If you use Puppeteer, then ensure that you complete the Required Puppeteer setup steps.
To view the full sample, in the sample's toolbar, select the GitHub logo.
What's next
- Create a synthetic monitor
- Create a broken-link checker
- Manage synthetic monitors
- Explore synthetic monitor results