Muestras de monitores sintéticos

En este documento, se describen las plantillas y el código de muestra disponibles para ayudarte a crear monitores sintéticos. Las funciones de muestra están disponibles en el repositorio Google Cloud/synthetics-sdk-nodjs de GitHub.

Si escribes pruebas y no dependes de una plantilla, asegúrate de que la prueba sea exitosa, a menos que se arroje una Error. Te recomendamos que uses la biblioteca Assert para asegurarte de que, cuando se produzcan fallas, se atribuyan a la línea de código adecuada.

Plantillas genéricas

Las plantillas genéricas están configuradas para recopilar datos de registro y seguimiento de las solicitudes HTTP salientes. La solución aprovecha el módulo auto-instrumentation-node de OpenTelemetry y el registrador de winston. Debido a la dependencia de los productos de código abierto, es posible que haya cambios en la estructura de los datos de seguimiento y registro. Por lo tanto, los datos de registro y de seguimiento recopilados solo deben usarse con fines de depuración.

Puedes implementar tu propio enfoque para recopilar datos de seguimiento y de registro de las solicitudes HTTP salientes. Para ver un ejemplo de un enfoque personalizado, consulta la clase SyntheticAutoInstrumentation.

Muestra genérica de Node.js

En el ejemplo generic-synthetic-nodejs, se muestra cómo consultar una URL. Esta muestra contiene lo mismo que la función predeterminada que muestra la consola de Google Cloud.

Para ver la muestra completa de generic-synthetic-nodejs, haz clic en Más y, luego, selecciona Ver en 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));
}));

Muestra de TypeScript

En el ejemplo generic-synthetic-typescript, se muestra cómo consultar una URL.

Para ver la muestra completa de generic-synthetic-typescript, haz clic en Más y, luego, selecciona Ver en 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));
}));

Plantilla de Puppeteer

Si usas Puppeteer, te recomendamos comenzar con la muestra de generic-puppeteer-nodejs.

Configuración obligatoria de Puppeteer

Para utilizar Puppeteer, asegúrate de completar los siguientes pasos:

  1. Incluye .puppeteerrc.cjs en el directorio del código fuente de tu Cloud Function:

    const { join } = require('path');
    
    /**
     * @type {import("puppeteer").Configuration}
     */
    module.exports = {
      cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
    };
  2. Agrega la siguiente secuencia de comandos al archivo package.json de la Cloud Function:

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

Muestra de Puppeteer

En el ejemplo generic-puppeteer-nodejs, se muestra cómo usar Puppeteer con tu Cloud Function.

Para ver la muestra completa de generic-puppeteer-nodejs, haz clic en Más y, luego, selecciona Ver en 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();
}));

Plantilla de Selenium WebDriver

Si usas Selenium WebDriver, considera comenzar con la muestra de generic-selenium-nodejs. La muestra, que está disponible en GitHub, incluye un archivo index.js y package.json.

Para ver la muestra completa de generic-selenium-nodejs, haz clic en Más y, luego, selecciona Ver en 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();
  })
);

Plantilla Mocha

Si escribes pruebas que se basan en la plantilla Mocha, considera si una secuencia de pruebas debe continuar o detenerse cuando se produce una falla. Para detener una secuencia de pruebas después de una falla, debes configurar la marca bail.

Para ver un ejemplo de extremo a extremo que incluya la implementación de una API, un paquete de pruebas de Mocha de muestra para los extremos de la API y cómo configurar el monitor sintético, consulta el blog Instructivo de supervisión sintética de Google Cloud.

En el ejemplo de mocha-url-ok, se muestra cómo una Cloud Function puede invocar un paquete de pruebas Mocha y se proporciona un paquete de pruebas de muestra.

Para ver la muestra completa de mocha-url-ok, haz clic en Más y, luego, selecciona Ver en 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;
});

En el ejemplo broken-links-ok, se muestra cómo configurar un verificador de vínculos rotos. Para esta plantilla, solo debes especificar los valores del objeto options. Este objeto especifica el URI que se probará y los parámetros de la prueba. Para obtener más detalles, consulta el siguiente código:

Si usas Puppeteer, asegúrate de completar los pasos de Configuración obligatoria de Puppeteer.

Para ver la muestra completa de broken-links-ok, haz clic en Más y, luego, selecciona Ver en 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));

¿Qué sigue?