Campioni per monitor sintetici

Questo documento descrive i modelli e il codice campione disponibili per aiutarti a creare monitor sintetici. Le funzioni di esempio sono disponibili nel repository GitHub di Google Cloud/synthetics-sdk-nodjs.

Se scrivi test e non fai affidamento su un modello, assicurati che il test vada a buon fine, a meno che non venga generato un valore Error. Ti consigliamo di utilizzare la libreria Assert per assicurarti che, quando si verificano errori, vengano attribuiti alla riga di codice corretta.

Modelli generici

I modelli generici sono configurati per raccogliere dati di traccia e log per le richieste HTTP in uscita. La soluzione sfrutta il modulo auto-instrumentation-node di OpenTelemetry e il winston logger. A causa della dipendenza dai prodotti open source, dovresti aspettarti modifiche alla struttura dei dati di traccia e log. Pertanto, i dati di traccia e log raccolti devono essere utilizzati solo per scopi di debug.

Puoi implementare il tuo approccio per raccogliere dati di traccia e log per le richieste HTTP in uscita. Per un esempio di approccio personalizzato, vedi il corso SyntheticAutoInstrumentation.

Esempio generico Node.js

L'esempio generic-synthetic-nodejs mostra come eseguire query su un URL. Questo esempio contiene la funzione predefinita visualizzata dalla console Google Cloud.

Per visualizzare l'esempio completo di generic-synthetic-nodejs, fai clic su Altro, quindi seleziona Visualizza su GitHub:

const { instantiateAutoInstrumentation, runSyntheticHandler } = require('@google-cloud/synthetics-sdk-api');
// Run instantiateAutoInstrumentation before any other code runs, to get automatic logs and traces
instantiateAutoInstrumentation();
const functions = require('@google-cloud/functions-framework');
const axios = require('axios');
const assert = require('node:assert');

functions.http('SyntheticFunction', runSyntheticHandler(async ({logger, executionId}) => {
  /*
   * This function executes the synthetic code for testing purposes.
   * If the code runs without errors, the synthetic test is considered successful.
   * If an error is thrown during execution, the synthetic test is considered failed.
   */
  logger.info('Making an http request using synthetics, with execution id: ' + executionId);
  const url = 'https://www.google.com/'; // URL to send the request to
  return await assert.doesNotReject(axios.get(url));
}));

Esempio di TypeScript

L'esempio generic-synthetic-typescript illustra come eseguire query su un URL.

Per visualizzare l'esempio completo di generic-synthetic-typescript, fai clic su Altro, quindi seleziona Visualizza su GitHub:

import {runSyntheticHandler, instantiateAutoInstrumentation} from '@google-cloud/synthetics-sdk-api'
// Run instantiateAutoInstrumentation before any other code runs, to get automatic logs and traces
instantiateAutoInstrumentation();
import * as ff from '@google-cloud/functions-framework';
import axios from 'axios';
import assert from 'node:assert';
import {Logger} from 'winston';

ff.http('SyntheticFunction', runSyntheticHandler(async ({logger, executionId}: {logger: Logger, executionId: string|undefined}) => {
  /*
   * This function executes the synthetic code for testing purposes.
   * If the code runs without errors, the synthetic test is considered successful.
   * If an error is thrown during execution, the synthetic test is considered failed.
   */
  logger.info('Making an http request using synthetics, with execution id: ' + executionId);
  const url = 'https://www.google.com/'; // URL to send the request to
  return await assert.doesNotReject(axios.get(url));
}));

Modello burattinaio

Se utilizzi Puppeteer, ti consigliamo di iniziare con l'esempio di generic-puppeteer-nodejs.

Configurazione richiesta per Puppeteer

Per utilizzare Puppeteer, assicurati di completare i seguenti passaggi:

  1. Includi .puppeteerrc.cjs nella directory di origine della tua Cloud Function:

    const { join } = require('path');
    
    /**
     * @type {import("puppeteer").Configuration}
     */
    module.exports = {
      cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
    };
  2. Aggiungi lo script seguente al file package.json della Cloud Function:

    "scripts": {
         "gcp-build": "node node_modules/puppeteer/install.mjs"
    },
    

Campione di burattini

L'esempio generic-puppeteer-nodejs illustra come utilizzare Puppeteer con la Cloud Function.

Per visualizzare l'esempio completo di generic-puppeteer-nodejs, fai clic su Altro, quindi seleziona Visualizza su GitHub:

const { instantiateAutoInstrumentation, runSyntheticHandler } = require('@google-cloud/synthetics-sdk-api');
// Run instantiateAutoInstrumentation before any other code runs, to get automatic logs and traces
instantiateAutoInstrumentation();
const functions = require('@google-cloud/functions-framework');
const axios = require('axios');
const assert = require('node:assert');
const puppeteer = require('puppeteer');


functions.http('CustomPuppeteerSynthetic', runSyntheticHandler(async ({logger, executionId}) => {
 /*
  * This function executes the synthetic code for testing purposes.
  * If the code runs without errors, the synthetic test is considered successful.
  * If an error is thrown during execution, the synthetic test is considered failed.
  */

 // Launch a headless Chrome browser and open a new page
 const browser = await puppeteer.launch({ headless: 'new', timeout: 0});
 const page = await browser.newPage();

 // Navigate to the target URL
 const result = await page.goto('https://www.example.com', {waitUntil: 'load'});

 // Confirm successful navigation
 await assert.equal(result.status(), 200);

 // Print the page title to the console
 const title = await page.title();
 logger.info(`My Page title: ${title} ` + executionId);

 // Close the browser
 await browser.close();
}));

Modello Selenium WebDriver

Se utilizzi Selenium WebDriver, valuta la possibilità di iniziare con l'esempio di generic-selenium-nodejs. L'esempio, disponibile su GitHub, include un file index.js e package.json.

Per visualizzare l'esempio completo di generic-selenium-nodejs, fai clic su Altro, quindi seleziona Visualizza su GitHub:

const {
  instantiateAutoInstrumentation,
  runSyntheticHandler,
} = require('@google-cloud/synthetics-sdk-api');
// Run instantiateAutoInstrumentation before any other code runs, to get automatic logs and traces
instantiateAutoInstrumentation();
const functions = require('@google-cloud/functions-framework');
const assert = require('node:assert');

const { Builder, Browser, By } = require('selenium-webdriver');
const chrome = require('selenium-webdriver/chrome');

/*
 * This function executes the synthetic code for testing purposes.
 * If the code runs without errors, the synthetic test is considered successful.
 * If an error is thrown during execution, the synthetic test is considered failed.
 */
functions.http(
  'CustomSeleniumSynthetic',
  runSyntheticHandler(async ({ logger, executionId }) => {
    /*
     * Construct chrome options
     * Note: `setChromeBinaryPath` must be set to '/srv/bin/chromium' when running in
     *   GCF (but will need to be changed if running on local machine).
     */
    const options = new chrome.Options();
    options.setChromeBinaryPath('/srv/bin/chromium');
    options.addArguments('--headless', '--disable-gpu', '--no-sandbox');

    // Launch headless chrome webdriver with options
    const driver = await new Builder()
      .forBrowser(Browser.CHROME)
      .setChromeOptions(options)
      .build();

    // Navigate to the target URL
    await driver.get('https://example.com');

    // Retrieve title and `a` tag of page
    const title = await driver.getTitle();
    const aTag = await driver.findElement(By.css('a')).getText();

    // assert title is as expected and print to console
    await assert.equal(title, 'Example Domain');
    logger.info(`My URL title is: ${title} ` + executionId);

    await driver.quit();
  })
);

Modello Mocha

Se scrivi test che si basano sul modello Mocha, valuta se una sequenza di test deve continuare, o interrompersi, in caso di errore. Per interrompere una sequenza di test dopo un errore, devi impostare il flag bail.

Per un esempio end-to-end che include il deployment di un'API, una suite di test Mocha di esempio per gli endpoint API e come configurare il monitoraggio sintetico, consulta il blog Tutorial sul monitoraggio sintetico di Google Cloud.

L'esempio di mocha-url-ok illustra come una Cloud Function può richiamare una suite di test Mocha e fornisce una suite di test di esempio.

Per visualizzare l'esempio completo di mocha-url-ok, fai clic su Altro, quindi seleziona Visualizza su GitHub:


const functions = require('@google-cloud/functions-framework');
const GcmSynthetics = require('@google-cloud/synthetics-sdk-mocha');

/*
 * This is the server template that is required to run a synthetic monitor in
 * Google Cloud Functions.
 */

functions.http('SyntheticMochaSuite', GcmSynthetics.runMochaHandler({
  spec: `${__dirname}/mocha_tests.spec.js`
}));

/*
 * This is the file may be interacted with to author mocha tests. To interact
 * with other GCP products or services, users should add dependencies to the
 * package.json file, and require those dependencies here A few examples:
 *  - @google-cloud/secret-manager:
 *        https://www.npmjs.com/package/@google-cloud/secret-manager
 *  - @google-cloud/spanner: https://www.npmjs.com/package/@google-cloud/spanner
 *  - Supertest: https://www.npmjs.com/package/supertest
 */

const {expect} = require('chai');
const fetch = require('node-fetch');

it('pings my website', async () => {
  const url = 'https://google.com/'; // URL to send the request to
  const externalRes = await fetch(url);
  expect(externalRes.ok).to.be.true;
});

L'esempio di broken-links-ok illustra come configurare uno strumento di verifica dei link inaccessibili. Per questo modello, specifichi solo i valori dell'oggetto options. Questo oggetto specifica l'URI da testare e i parametri del test. Per maggiori dettagli, vedi il seguente codice:

Se utilizzi Puppeteer, assicurati di completare i passaggi per la configurazione obbligatoria di Puppeteer.

Per visualizzare l'esempio completo di broken-links-ok, fai clic su Altro, quindi seleziona Visualizza su GitHub:


const functions = require('@google-cloud/functions-framework');
const GcmSynthetics = require('@google-cloud/synthetics-sdk-broken-links');

const options = {
  origin_uri: "https://example.com",
  // link_limit: 10,
  // query_selector_all: "a", // https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll
  // get_attributes: ['href'], // https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute
  // link_order: "FIRST_N", // "FIRST_N" or "RANDOM"
  // link_timeout_millis: 30000, // timeout per link
  // max_retries: 0, // number of retries per link if it failed for any reason
  // wait_for_selector: '', // https://pptr.dev/api/puppeteer.page.waitforselector
  // per_link_options: {},
    /*
    // example:
      per_link_options: {
        'http://fake-link1': { expected_status_code: "STATUS_CLASS_4XX" },
        'http://fake-link2': { expected_status_code: 304 },
        'http://fake-link3': { link_timeout_millis: 10000 },
        'http://fake-link4': {
          expected_status_code: "STATUS_CLASS_3XX",
          link_timeout_millis: 10,
        },
      },
    */
  // total_synthetic_timeout_millis: 60000 // Timeout set for the entire Synthetic Monitor
  // screenshot_options: { capture_condition: 'FAILING', storage_location: '' }
};

functions.http('BrokenLinkChecker', GcmSynthetics.runBrokenLinksHandler(options));

Passaggi successivi