Amostras de monitores sintéticos

Neste documento, descrevemos os modelos e o exemplo de código disponíveis para ajudar você a criar monitores sintéticos. As funções de amostra estão disponíveis no repositório Google Cloud/synthetics-sdk-nodjs (link em inglês).

Se você programar testes e não depender de um modelo, verifique se o teste é aprovado, a menos que uma Error seja gerada. Recomendamos usar a biblioteca Assert para garantir que, quando ocorrerem falhas, elas sejam atribuídas à linha de código adequada.

Modelos genéricos

Os modelos genéricos são configurados para coletar dados de rastreamento e registro para solicitações HTTP de saída. A solução aproveita o módulo auto-instrumentation-node do OpenTelemetry e o winston logger. Devido à dependência de produtos de código aberto, é possível esperar alterações na estrutura dos dados de rastreamento e registro. Portanto, os dados de registros e traces coletados devem ser usados apenas para fins de depuração.

É possível implementar sua própria abordagem para coletar dados de rastreamento e registro para solicitações HTTP de saída. Para conferir um exemplo de abordagem personalizada, consulte a classe SyntheticAutoInstrumentation.

Exemplo genérico de Node.js

O exemplo generic-synthetic-nodejs ilustra como consultar um URL. Essa amostra contém a mesma função padrão exibida pelo console do Google Cloud.

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));
}));

Exemplo do TypeScript

A amostra generic-synthetic-typescript ilustra como consultar um URL.

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));
}));

Modelo Puppeteer

Se você usa o Puppeteer, comece com a amostra generic-puppeteer-nodejs.

Configuração necessária do Puppeteer

Para usar o Puppeteer, conclua as seguintes etapas:

  1. Inclua .puppeteerrc.cjs no diretório de origem da sua função do Cloud:

    const { join } = require('path');
    
    /**
     * @type {import("puppeteer").Configuration}
     */
    module.exports = {
      cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
    };
  2. Adicione o script a seguir ao arquivo package.json da função do Cloud:

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

Amostra do Puppeteer

A amostra generic-puppeteer-nodejs ilustra como usar o Puppeteer com a função do Cloud:

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();
}));

Modelo mocha

Se você criar testes que dependem do modelo Mocha, considere se uma sequência de testes precisa continuar ou ser interrompida quando ocorrer uma falha. Para interromper uma sequência de testes após uma falha, defina a flag bail.

Para ver um exemplo completo que inclui a implantação de uma API, um exemplo de pacote de testes Mocha para os endpoints da API e como configurar o monitor sintético, consulte o blog Tutorial do monitoramento sintético do Google Cloud.

O exemplo mocha-url-ok ilustra como uma função do Cloud pode invocar um conjunto de testes Mocha (link em inglês) e fornece um exemplo desse pacote.


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;
});

O exemplo broken-links-ok (link em inglês) ilustra como configurar um verificador de links corrompidos. Para este modelo, você especifica apenas os valores do objeto options. Esse objeto especifica o URI a ser testado e os parâmetros do teste. Para mais detalhes, consulte este código:

Se você usar o Puppeteer, certifique-se de concluir as etapas de configuração necessária do Puppeteer.


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));

A seguir