This document describes the templates and sample code that are available
to help you create synthetic monitors. The sample functions are available on the
Google Cloud/synthetics-sdk-nodjs
GitHub repository.
If you write tests and don't rely on a template, then ensure that your test
passes unless an Error
is thrown. We recommend
that you use the Assert
library to ensure that
when failures occur, they are attributed to the proper 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 as the default function
displayed by the Google Cloud console. To view the full sample, click more_vert More,
and then select View on GitHub.
TypeScript sample
The generic-synthetic-typescript
sample
illustrates how to query a URL. To view the full sample, click more_vert More,
and then select View on GitHub.
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.cjs
in the source directory of your Cloud Run function:Add the following script to the
package.json
file 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, click more_vert More,
and then select View on GitHub.
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 an index.js
and package.json
file.
To view the full sample, click more_vert More, and then select View on GitHub.
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 which includes deploying an API, a sample Mocha test suite for the API endpoints, and how to configure 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, click more_vert More,
and then select View on GitHub.
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, click more_vert More, and then select View on GitHub.
What's next
- Create a synthetic monitor
- Create a broken-link checker
- Manage synthetic monitors
- Explore synthetic monitor results