@elastic/synthetics

中文文档
1.30.0发布于 2026/11/13MITTypeScriptCommonJS构建来源已验证近 30 天下载 11万

Elastic synthetic monitoring agent

原始使用文档
文档目录5 节
  1. Usage
  2. Browser journeys
  3. API journeys (no browser)
  4. Documentation
  5. Contributing

ci e2e

Synthetics Agent

Synthetic Monitoring with Real Browsers.

Usage

Install the package

npm install -g @elastic/synthetics

Run the suites via CLI

npx @elastic/synthetics [options] [dir] [files] file

Use --workdir /path/to/scratch to select a writable directory for runtime cache files, including screenshots, and temporary push bundles. The programmatic equivalent is run({ workdir: '/path/to/scratch' }). Relative paths resolve against the current working directory. The default is .synthetics in the current working directory. Synthetics creates and removes a process-specific subdirectory (<workdir>/<pid>), leaving the work directory itself intact. For pushes, use npx @elastic/synthetics push --workdir /path/to/scratch; bundles are created under <workdir>/bundles and that subdirectory is removed after successful bundling. Project discovery and configuration resolution are unaffected. The caller must provide write access for the user running Synthetics; Synthetics does not change ownership or permissions on existing directories.

Browser journeys

The default journey() DSL launches a real Chromium browser via Playwright and is the right choice when you want to verify user-facing flows.

import { journey, step, expect } from '@elastic/synthetics';

journey('homepage loads', ({ page, params }) => {
  step('open', async () => {
    await page.goto(params.url);
  });
  step('check title', async () => {
    expect(await page.title()).toContain('Welcome');
  });
});

API journeys (no browser)

For API-only checks, use apiJourney(). It runs without launching a browser, which makes it suitable for OAuth-protected endpoints, multi-step API flows, and high-frequency lightweight monitoring.

import { apiJourney, monitor, step, expect } from '@elastic/synthetics';

apiJourney('orders API health', ({ request, params }) => {
  monitor.use({ schedule: 1, locations: ['us_east'] });

  let token: string;
  step('obtain OAuth2 token', async () => {
    const r = await request.post(`${params.authUrl}/oauth2/token`, {
      form: {
        grant_type: 'client_credentials',
        client_id: params.clientId,
        client_secret: params.clientSecret,
      },
    });
    expect(r.status()).toBe(200);
    token = (await r.json()).access_token;
  });

  step('check /orders', async () => {
    const r = await request.get(`${params.apiUrl}/orders`, {
      headers: { Authorization: `Bearer ${token}` },
    });
    expect(r.status()).toBe(200);
  });
});

API journeys share most of the regular journey() surface — step(), expect, params, monitor.use(), apiJourney.skip / apiJourney.only — and integrate with the same push command. They are pushed to Kibana as api-type monitors.

Each API journey gets its own isolated APIRequestContext with an independent cookie jar; cookies set in one journey are not visible in another. All playwrightOptions (baseURL, extraHTTPHeaders, ignoreHTTPSErrors, httpCredentials, client certificates, proxy, etc.) flow through to the underlying Playwright API context.

Per-request network data — URL, method, status, headers, body sizes, timings, and (for HTTPS) the peer certificate's issuer / subject / validFrom / validTo / protocol — is captured and emitted in the same journey/network_info events used by browser journeys.

Documentation

Complete documentation is avaiable on elastic.co

Have questions? Want to leave feedback? Visit the Synthetics discussion forum.

Contributing

Contributions are welcome, but we recommend that you take a moment and read our contribution guide first.


Made with ♥️ and ☕️ by Elastic and our community.

版本与发布记录

@elastic/synthetics 共发布 115 个版本,首次发布于 2020/10/30;过去 12 个月发布了 18 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.30.0latest2026/11/13今天
1.29.02026/10/3013 天前
1.28.02026/10/2915 天前
1.27.02026/10/2024 天前
1.26.32026/09/181 个月前
1.26.22026/09/112 个月前

其他发布通道:alpha → 0.0.1-alpha.14,beta → 1.0.0-beta.46。

关于 @elastic/synthetics 的常见问题

@elastic/synthetics 是什么?

Elastic synthetic monitoring agent

如何安装 @elastic/synthetics?

在项目目录运行 npm install @elastic/synthetics,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 synthetics、elastic-synthetics,可以用 npx synthetics 直接运行。

@elastic/synthetics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elastic/synthetics 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@elastic/synthetics 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@elastic/synthetics 可以商用吗?

@elastic/synthetics 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elastic/synthetics 的下载量有多少?

根据 npm 官方下载统计,@elastic/synthetics 在 2026/10/14 – 2026/11/12 这 30 天内被下载 109,525 次,日均约 3,651 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elastic/synthetics 还在维护吗?

最新版本 1.30.0 发布于 2026/11/13(今天);至今共发布 115 个版本,其中过去 12 个月发布了 18 个;首次发布于 2020/10/30。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。