# @fastly/compute-js-opentelemetry 中文文档

规范地址：https://npmcn.com/package/%40fastly/compute-js-opentelemetry/

版本：0\.4\.4
npm 元数据更新时间：2026-10-27T05:44:40.943Z
中文译文更新时间：2026-11-14T11:27:09.020Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fastly/compute-js-opentelemetry
原文对照：https://npmcn.com/package/%40fastly/compute-js-opentelemetry/?lang=original
项目主页：https://github.com/fastly/compute-js-opentelemetry#readme
源码仓库：https://github.com/fastly/compute-js-opentelemetry

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：107 kB，92 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：14 个
- peer 依赖：@fastly/js-compute ^2\.0\.0 \|\| ^3\.0\.0
- 首次发布：2022-06-07T06:02:48.568Z
- 最新版本发布：2026-02-12T06:21:19.135Z
- 近 30 天下载量：7163（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：18（近 12 个月 1 个）

## 中文简介

快速计算打开遥测 SDK

## 安装

```sh
npm install @fastly/compute-js-opentelemetry
```

也可以使用：`pnpm add @fastly/compute-js-opentelemetry`、`yarn add @fastly/compute-js-opentelemetry`、`bun add @fastly/compute-js-opentelemetry`

## README

# 快速计算 JavaScript 的打开远程

执行 [打开远程 JavaScript API](https://opentelemetry.io/docs/instrumentation/js/) 页:1 [快速计算](https://developer.fastly.com/learning/compute/)。 。 。 .

生成像这样的痕迹, 以跟踪您计算应用程序中的活动和时间 :

![Sample Trace](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/assets/readme-demo-trace.png "Sample Trace")

**指数.js**数字 :

```javascript
/// <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"}),
  });
}
```

**遥测.js**数字 :

```javascript
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();
```

此执行扩展了该程序提供的标准接口和对象 打开几何 [Java 脚本 API](https://github.com/open-telemetry/opentelemetry-js/tree/main/api) 财务报告和审定财务报表 [SDK 软件](https://github.com/open-telemetry/opentelemetry-js),将其改造后用于快速计算平台.

## 新来的!

截止 `v0.3.0`, (中文(简体) ), `@fastly/compute-js-opentelemetry` 不再需要Webpack了.

此外,如果你使用 `FastlySDK` 模块,您需要 `@fastly/js-compute` v3.11.0 或更新并启用顶级等待 。 找到您称之为二进制的位置 `js-compute-runtime` 财务报告和审定财务报表 添加这样的旗帜 `js-compute-runtime --experimental-enable-top-level-await` (通常在其中之一) `package.json` 或者说 `fastly.toml`) (中文(简体) ).

查看本套件中包含的工作示例的演示.

## 模块

不过 `opentelemetry-js` 将每个关注事项分开 `npm` 软件包, 我们提供我们的组件作为单一的一揽子,有多种出口.

| **模块** (链接到文档) | **导出名称** | 说明 |
| --- | --- | --- |
| [快速打开遥测 SDK](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-sdk-fastly) | `@fastly/compute-js-opentelemetry/opentelemetry-sdk-fastly` | 一个功能库,简化了正在使用的与计算 JavaScript 应用程序的 OpenTelection 组件的初始化和协调. |
| [快速计算 JavaScript 寿命周期仪器](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-instrumentation-fastly-compute-js) | `@fastly/compute-js-opentelemetry/opentelemetry-instrumentation-fastly-compute-js` | 一个 [仪器库](https://opentelemetry.io/docs/reference/specification/glossary/#instrumentation-library) 生成计算应用程序生命周期的痕迹. |
| [快速后端获取仪器](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-instrumentation-fastly-backend-fetch) | `@fastly/compute-js-opentelemetry/opentelemetry-instrumentation-fastly-backend-fetch` | 生成跟踪的仪器库 `fetch()` 呼叫快速后端. |
| [快速计算 JavaScript 自动仪器](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/auto-instrumentations-compute-js) | `@fastly/compute-js-opentelemetry/auto-instrumentations-compute-js` | 一个自动为计算JavaScript装入仪器的元包. |
| [快速追踪 SDK](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-sdk-trace-fastly) | `@fastly/compute-js-opentelemetry/opentelemetry-sdk-trace-fastly` | 提供 [宽处理器](https://opentelemetry.io/docs/reference/specification/trace/sdk/#span-processor), (中文(简体) ), [追踪提供者](https://opentelemetry.io/docs/reference/specification/trace/sdk/#tracer-provider),以及 [背景管理器](https://opentelemetry.io/docs/instrumentation/js/api/context/#context-manager) 用于计算 JavaScript 应用程序的执行. |
| [快速后端追踪导出者](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/exporter-trace-otlp-fastly-backend) | `@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-backend` | A 类 [Span 导出器](https://opentelemetry.io/docs/reference/specification/trace/sdk/#span-exporter) 使用 [OTLP/HTTP JSON(英语:](https://github.com/open-telemetry/opentelemetry-proto/blob/main/docs/specification.md#otlphttp) 格式 [一个快速后端](https://developer.fastly.com/learning/compute/javascript/#communicating-with-backend-servers-and-the-fastly-cache)。 。 。 . |
| [快速命名日志提供者的追踪导出者](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/exporter-trace-otlp-fastly-logger) | `@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-logger` | 使用 OTLP/HTTP JSON 格式输出痕迹的 Span Exporters 执行 [在快速命名的日志提供者之上](https://developer.fastly.com/learning/integrations/logging)。 。 。 . |
| [快速计量 SDK](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-sdk-metrics-fastly) | `@fastly/compute-js-opentelemetry/opentelemetry-sdk-metrics-fastly` | 规定 a [量子阅读器](https://opentelemetry.io/docs/reference/specification/metrics/sdk/#metricreader) 用于计算 JavaScript 应用程序. |
| [快速后端的计量导出器](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-exporter-metrics-otlp-fastly-backend) | `@fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-backend` | A 类 [计量导出器](https://opentelemetry.io/docs/reference/specification/metrics/sdk/#metricexporter) 使用 OTLP/HTTP JSON 格式通过快速后端输出度量衡的执行. |
| [快速命名日志提供者的计量导出器](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/opentelemetry-exporter-metrics-otlp-fastly-logger) | `@fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-logger` | 使用OTLP/HTTP JSON格式在快速命名的日志提供商上输出度量衡的计量输出器执行. |
| [出口基地类别](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/otlp-exporter-fastly-base) | `@fastly/compute-js-opentelemetry/otlp-exporter-fastly-base` | 出口商基本类别,包含上述出口商类别使用的通用代码. |
| [快速命名日志提供者的诊断记录器](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/diag-fastly-logger) | `@fastly/compute-js-opentelemetry/diag-fastly-logger` | A 类 [对话框](https://opentelemetry.io/blog/2022/troubleshooting-nodejs/#enable-logging) 将日志记录到一个快速命名的日志提供者. |
| [核心公用事业](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/src/core) (内部使用) | (未出口) | 供图书馆内部使用的公用事业,而不是用户代码. |

## 实例

参见 [`/examples`](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples) 目录.

| **示例目录** | 说明 |
| --- | --- |
| [读取](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/readme-demo) | 从此 README 开始的示例演示 |
| [读取](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/readme-demo-ts) | 从此 README 开头的示例演示, 移植到 TypeScript |
| [基本追踪-实例](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/basic-tracing-example) | 基本追踪示例 |
| [基本追踪-实例](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/basic-tracing-example-ts) | 类型脚本中的基本追踪示例 |
| [基本计量实例](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/basic-metrics-example) | 基本计量示例 |
| [发音](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/otel-demo) | 实例可以显示 OpenTeleometry 的痕迹,这些痕迹从边缘开始,从后端进入一个操作. |
| [otel- http- 代用](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/otel-http-proxy) | 一个样本应用程序旨在作为快速服务的HTTPS日志端点来收集痕迹,将其发送到一个OpenTeleometry收集器. |

## 使用类型脚本

此库旨在与 JavaScript 和 [类型脚本](https://www.typescriptlang.org/) 编写申请 快速计算.

检查出来 [读取](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/readme-demo-ts) 财务报告和审定财务报表 [基本追踪-实例](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/examples/basic-tracing-example-ts) 输入 `examples` 使用 TypeScript 的工作样本程序目录.

## 页:1

### 环境变量

OpenTeleometry 定义了一套有详细记录的 [环境变量](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md) 用于配置其库的默认值。 然而, JavaScript 应用程序可以运行于 快速的计算平台在 _构建时间 ("构建时间初始化")_ 相当 而不是每次引用时(他们如此快速的开始). 这意味着平台不能提供阅读方式 在构建-时间初始化期间从环境开始的,当大多数OpenTelemotry库正在初始化时.

因此,当使用此库时 `getEnv()` 函数总是返回由 OpenTeleometry 定义的默认值. 对这些缺省需要的任何修改,都必须按程序进行.

### 兼容性

有点 `opentelemetry-js` 模块目前不兼容快速计算. 下表列出了这些组成部分的非全面清单.

| **构成部分** | 软件包 | 原因/工作 |
| --- | --- | --- |
| `OTLPTraceExporter` | `@opentelemetry/exporter-trace-otlp-http` | 继续使用 `XmlHttpRequest` 财务报告和审定财务报表 `navigator.beacon`,在计算中不可用。 使用 `OTLPTraceExporter` 从 `@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-backend` 或者说 `@fastly/compute-js-opentelemetry/exporter-trace-otlp-fastly-logger` 换句话说. |
| `OTLPMetricExporter` | `@opentelemetry/exporter-metrics-otlp-http` | 继续使用 `XmlHttpRequest` 财务报告和审定财务报表 `navigator.beacon`,在计算中不可用。 使用 `OTLPTraceExporter` 从 `@fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-backend` 或者说 `@fastly/compute-js-opentelemetry/exporter-metrics-otlp-fastly-logger` 换句话说. |
| `ZoneContextManager` | `@opentelemetry/context-zone`  
`@opentelemetry/context-zone-peer-dep` | 继续使用 `zone.js`,这与计算不一致。 使用 `FastlyStackContextManager`。 。 。 . |
| `AsyncHooksContextManager`  
`AsyncLocalStorageContextManager` | `@opentelemetry/context-async-hooks` | 继续使用 `async_hooks`,在计算中不可用。 使用 `FastlyStackContextManager`。 。 。 . |
| `NodeSDK` | `@opentelemetry/sdk-node` | 这个SDK是用于节点,与计算不相容. 使用 `FastlySDK`。 。 。 . |
| 列入的仪器 `opentelemetry-js` | `@opentelemetry/instrumentation-*`  
`@opentelemetry/auto-instrumentations-node`  
`@opentelemetry/auto-instrumentations-web` | 这些都依赖于其他与计算不相容的框架和模块. 使用 `FastlyComputeJsInstrumentation` 财务报告和审定财务报表 `FastlyBackendFetchInstrumentation`。 。 。 . |

## 问题

如果你遇到任何与安全无关的错误或意外行为,请 [提交问题](https://github.com/fastly/compute-js-opentelemetry/issues/new?labels=bug) 使用错误报告模板.

### 安全问题

请看我们的 [警卫](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/SECURITY.md) 指导如何报告与安全有关的问题.

## 许可证

[麻省理工学院](https://npmcn.com/package/%40fastly/compute-js-opentelemetry/LICENSE)。 。 。 .

## 常见问题

### @fastly/compute-js-opentelemetry 是什么？

快速计算打开遥测 SDK

### 如何安装 @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 和提交记录判断。

