@fastly/compute-js-opentelemetry

中文文档
0.4.4发布于 2026/02/12MITTypeScriptES Module构建来源已验证近 30 天下载 7163

OpenTelemetry SDK for Fastly Compute

原始使用文档
文档目录10 节
  1. New!
  2. Modules
  3. Examples
  4. Use with TypeScript
  5. Notes
  6. Environment Variables
  7. Compatibility
  8. Issues
  9. Security issues
  10. License

OpenTelemetry for JavaScript on Fastly Compute

An implementation of the OpenTelemetry JavaScript API for Fastly Compute.

Generate traces like this one to follow activity and time in your Compute applications:

Sample Trace

index.js:

/// <reference types="@fastly/js-compute" />

import './telemetry.js'
import { context, trace } from "@opentelemetry/api";

addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
async function handleRequest(event) {
  const tracer = trace.getTracerProvider()
    .getTracer('my-tracer');

  const mySpan = tracer.startSpan('my-task');
  context.with(trace.setSpan(context.active(), mySpan), () => {
    doTask();
  });
  mySpan.end();

  return new Response('OK', {
    status: 200,
    headers: new Headers({"Content-Type": "text/plain"}),
  });
}

telemetry.js:

import { context, trace } from "@opentelemetry/api";
import { Resource } from "@opentelemetry/resources";
import { SEMRESATTRS_SERVICE_NAME } from "@opentelemetry/semantic-conventions";

import { FastlySDK } from "@fastly/compute-js-opentelemetry/sdk-fastly";
import { OTLPTraceExporter } from "@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-backend";
import { getComputeJsAutoInstrumentations } from "@fastly/compute-js-opentelemetry/auto-instrumentations-compute-js";

const sdk = new FastlySDK({
  traceExporter: new OTLPTraceExporter({ backend: 'otlp-collector' }),
  instrumentations: [ getComputeJsAutoInstrumentations(), ],
  resource: new Resource({ [SEMRESATTRS_SERVICE_NAME]: 'readme-demo', }),
});
await sdk.start();

This implementation extends the standard interfaces and objects provided by the OpenTelemetry JavaScript API and SDK, adapting them for use on the Fastly Compute platform.

New!

As of v0.3.0, @fastly/compute-js-opentelemetry no longer requires Webpack.

Additionally, if you're using the FastlySDK module, you will need @fastly/js-compute v3.11.0 or newer and enable Top Level Await. Find where you call the binary js-compute-runtime and add the flag like so js-compute-runtime --experimental-enable-top-level-await (This is usually in either package.json or fastly.toml).

Look at the demos included in this package for working examples.

Modules

Whereas opentelemetry-js would separate each concern into its own npm package, we provide our components as a single package with multiple exports.

Module (links to documentation)Export NameDescription
Fastly OpenTelemetry SDK@fastly/compute-js-opentelemetry/opentelemetry-sdk-fastlyA utility library that simplifies the initialization and coordination of OpenTelemetry components in use with a Compute JavaScript application.
Fastly Compute JavaScript Lifecycle instrumentation@fastly/compute-js-opentelemetry/opentelemetry-instrumentation-fastly-compute-jsAn instrumentation library that generates traces for the Compute application lifecycle.
Fastly Backend Fetch instrumentation@fastly/compute-js-opentelemetry/opentelemetry-instrumentation-fastly-backend-fetchAn instrumentation library that generates traces for fetch() calls to Fastly backends.
Fastly Compute JavaScript auto-instrumentations@fastly/compute-js-opentelemetry/auto-instrumentations-compute-jsA meta package that automatically loads instrumentations for Compute JavaScript.
Trace SDK for Fastly@fastly/compute-js-opentelemetry/opentelemetry-sdk-trace-fastlyProvides Span Processor, Tracer Provider, and Context Manager implementations for use with a Compute JavaScript application.
Trace Exporter for Fastly backend@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-backendA Span Exporter implementation that exports traces using the OTLP/HTTP JSON format over a Fastly backend.
Trace Exporter for Fastly named log provider@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-loggerA Span Exporter implementation that exports traces using the OTLP/HTTP JSON format over a Fastly named log provider.
Metrics SDK for Fastly@fastly/compute-js-opentelemetry/opentelemetry-sdk-metrics-fastlyProvides a Metric Reader implementation for use with a Compute JavaScript application.
Metrics Exporter for Fastly backend@fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-backendA Metric Exporter implementation that exports metrics using the OTLP/HTTP JSON format over a Fastly backend.
Metrics Exporter for Fastly named log provider@fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-loggerA Metric Exporter implementation that exports metrics using the OTLP/HTTP JSON format over a Fastly named log provider.
Exporter Base Classes@fastly/compute-js-opentelemetry/otlp-exporter-fastly-baseBase classes for exporters, containing common code used by the exporter classes listed above.
Diagnostic Logger for Fastly named log provider@fastly/compute-js-opentelemetry/diag-fastly-loggerA DiagLogger implementation that logs to a Fastly named log provider.
Core Utilities (Internal Use)(Not exported)Utilities intended to be used internally by the library and not from user code.

Examples

See the examples in the /examples directory.

Example DirectoryDescription
readme-demoExample demo from the beginning of this README
readme-demo-tsExample demo from the beginning of this README, ported to TypeScript
basic-tracing-exampleBasic Tracing Example
basic-tracing-example-tsBasic Tracing Example in TypeScript
basic-metrics-exampleBasic Metrics Example
otel-demoExample that demonstrates OpenTelemetry traces that start at the Edge and nest into an operation at the backend.
otel-http-proxyA sample application designed to collect traces as an HTTPS log endpoint for a Fastly service, sending them to an OpenTelemetry collector.

Use with TypeScript

This library is designed to work with both JavaScript and TypeScript applications written for Fastly Compute.

Check out readme-demo-ts and basic-tracing-example-ts in the examples directory for working sample programs using TypeScript.

Notes

Environment Variables

OpenTelemetry defines a well-documented set of environment variables that are designed to allow you to configure defaults for its libraries. However, JavaScript applications built to run on Fastly's Compute platform perform their upfront initialization at build time ("build-time initialization") rather than during each invocation (that's how they start up so fast). This means that the platform cannot provide a way to read from the environment during build-time initialization, when most OpenTelemetry libraries are initializing.

Therefore, when using this library, the getEnv() function will always return the default values defined by OpenTelemetry. Any changes you require to these defaults need to be made programmatically.

Compatibility

Some opentelemetry-js modules are not currently compatible with Fastly Compute. The table below is a non-comprehensive list of such components.

ComponentPackageReason / Workaround
OTLPTraceExporter@opentelemetry/exporter-trace-otlp-httpRelies on XmlHttpRequest and navigator.beacon, which are not available in Compute. Use OTLPTraceExporter from @fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-backend or @fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-logger instead.
OTLPMetricExporter@opentelemetry/exporter-metrics-otlp-httpRelies on XmlHttpRequest and navigator.beacon, which are not available in Compute. Use OTLPTraceExporter from @fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-backend or @fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-logger instead.
ZoneContextManager@opentelemetry/context-zone
@opentelemetry/context-zone-peer-dep
Relies on zone.js, which is incompatible with Compute. Use FastlyStackContextManager.
AsyncHooksContextManager
AsyncLocalStorageContextManager
@opentelemetry/context-async-hooksRelies on async_hooks, which is not available in Compute. Use FastlyStackContextManager.
NodeSDK@opentelemetry/sdk-nodeThis SDK is for Node, which is not compatible with Compute. Use FastlySDK.
Instrumentations included in opentelemetry-js@opentelemetry/instrumentation-*
@opentelemetry/auto-instrumentations-node
@opentelemetry/auto-instrumentations-web
These rely on other frameworks and modules that are not compatible with Compute. Use FastlyComputeJsInstrumentation and FastlyBackendFetchInstrumentation.

Issues

If you encounter any non-security-related bug or unexpected behavior, please file an issue using the bug report template.

Security issues

Please see our SECURITY.md for guidance on reporting security-related issues.

License

MIT.

版本与发布记录

@fastly/compute-js-opentelemetry 共发布 18 个版本,首次发布于 2022/06/07;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.4latest2026/02/129 个月前
0.4.32025/09/261.1 年前
0.4.22025/01/061.9 年前
0.4.12024/05/152 年前
0.4.02024/03/152 年前
0.3.12024/03/152 年前

其他发布通道:alpha → 0.2.0-alpha.3。

关于 @fastly/compute-js-opentelemetry 的常见问题

@fastly/compute-js-opentelemetry 是什么?

OpenTelemetry SDK for Fastly Compute

如何安装 @fastly/compute-js-opentelemetry?

在项目目录运行 npm install @fastly/compute-js-opentelemetry,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@fastly/js-compute(^2.0.0 || ^3.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fastly/compute-js-opentelemetry 支持 TypeScript 吗?

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

@fastly/compute-js-opentelemetry 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@fastly/compute-js-opentelemetry 可以商用吗?

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

@fastly/compute-js-opentelemetry 的下载量有多少?

根据 npm 官方下载统计,@fastly/compute-js-opentelemetry 在 2026/10/15 – 2026/11/13 这 30 天内被下载 7,163 次,日均约 239 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fastly/compute-js-opentelemetry 还在维护吗?

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