# @elastic/opentelemetry-instrumentation-openai 中文文档

规范地址：https://npmcn.com/package/%40elastic/opentelemetry-instrumentation-openai/

版本：0\.5\.1
npm 元数据更新时间：2026-09-25T13:38:41.350Z
中文译文更新时间：2026-11-13T10:50:23.248Z
软件包许可：Apache-2\.0

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

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

弃用说明：This instrumentation has moved to @opentelemetry/instrumentation-openai\.

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

- 包体积（解压后）：119 kB，26 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：^18\.19\.0 \|\| \>=20\.6\.0
- 运行依赖：3 个
- peer 依赖：@opentelemetry/api ^1\.3\.0
- 首次发布：2024-12-12T18:39:31.123Z
- 最新版本发布：2025-07-14T21:20:25.004Z
- 近 30 天下载量：9489（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 0 个）

## 中文简介

用于 `openai` OpenAI 客户端库

## 安装

```sh
npm install @elastic/opentelemetry-instrumentation-openai
```

也可以使用：`pnpm add @elastic/opentelemetry-instrumentation-openai`、`yarn add @elastic/opentelemetry-instrumentation-openai`、`bun add @elastic/opentelemetry-instrumentation-openai`

## README

# 弹性的 OpenTelemet 仪器 `openai`

这个模块, `@elastic/opentelemetry-instrumentation-openai`自动 文书 [`openai`](https://www.npmjs.com/package/openai),打开AI Node.js客户端库.

它试图追踪 [GenAI 语义公约](https://github.com/open-telemetry/semantic-conventions/tree/main/docs/gen-ai)。 。 。 .

# 状态

装有仪器的 OpenAI API 端点 :

-   : 白色 检查 标记 : [聊天](https://platform.openai.com/docs/api-reference/chat)
-   : 白色 检查 标记 : [嵌入层](https://platform.openai.com/docs/api-reference/embeddings)

# 支持的版本

-   这个工具是: `openai` 区域中的软件包 : `>=4.19.0 <5`。 。 。 .
-   这支持了Node.js 18及后. ()`openai@4` 目前仅使用Node.js v18进行测试.)

# 语义公约

该仪器目前执行GenAI 1.29.0版本 语义公约: [https://oponteleometry.io/docs/specs/semconv/gen-ai/ (中文(简体) )](https://opentelemetry.io/docs/specs/semconv/gen-ai/)

# 安装

```bash
npm install @elastic/opentelemetry-instrumentation-openai
```

# 使用量

首先安装示例中使用的软件包 :

```bash
npm install openai \
  @opentelemetry/sdk-node \
  @elastic/opentelemetry-instrumentation-openai
```

保存到一个文件,说"例.js". (此示例显示 OTel 设置代码) 和应用程序代码在同一文件。 一般情况下, OTel 设置代码是: 单独文件并通过 `node -r ...`。见 [更完整的 OTel 设置 示例](https://npmcn.com/package/%40elastic/opentelemetry-instrumentation-openai/test/fixtures/telemetry.js). ) (中文(简体) )

```js
const {NodeSDK} = require('@opentelemetry/sdk-node');
const {OpenAIInstrumentation} = require('@elastic/opentelemetry-instrumentation-openai');
const sdk = new NodeSDK({
    instrumentations: [
        new OpenAIInstrumentation({
            // See the "Configuration" section below.
            captureMessageContent: true,
        })
    ]
})
sdk.start();
process.once('beforeExit', async () => { await sdk.shutdown() });

const OpenAI = require('openai');
async function main() {
    const openai = new OpenAI();
    const result = await openai.chat.completions.create({
        model: 'gpt-4o-mini',
        messages: [
            {role: 'user', content: 'Say hello world.'}
        ]
    });
    console.log(result.choices[0]?.message?.content);
}
main();
```

然后运行它:

```bash
OPENAI_API_KEY=sk-... \
    node example.js
```

默认情况下 `NodeSDK` 将通过OTLP导出遥测。 举个例子 可以看到控制台上的遥测:

```bash
OTEL_TRACES_EXPORTER=console \
    OTEL_LOGS_EXPORTER=console \
    OTEL_METRICS_EXPORTER=console \
    OPENAI_API_KEY=sk-... \
    node example.js
```

# 实例

在"实例/"目录中, [用户聊天](https://npmcn.com/package/%40elastic/opentelemetry-instrumentation-openai/examples/use-chat.js) 很简单 脚本使用 OpenAI Chat 补全 API. 首先运行脚本 **无 仪器**。 。 。 .

使用 OpenAI :

```bash
OPENAI_API_KEY=sk-... \
    node use-chat.js
```

使用 Azure OpenAI( 这假设您的 Azure OpenAI 端点有一个模型) 使用“ gpt-4o- mini” 名称的部署 :

```bash
AZURE_OPENAI_ENDPOINT=https://YOUR-ENDPOINT-NAME.openai.azure.com \
    AZURE_OPENAI_API_KEY=... \
    OPENAI_API_VERSION=2024-10-01-preview \
    node use-chat.js
```

使用 [奥拉玛](https://ollama.com/) 本地操作 LLMS 的工具,它暴露 一个 OpenAI 兼容的 API :

```bash
ollama serve

# When using Ollama, we default to qwen2.5:0.5b, which is a small model. You
# can choose a larger one, or a different tool capable model like mistral-nemo.
export CHAT_MODEL=qwen2.5
ollama pull $CHAT_MODEL

OPENAI_BASE_URL=http://localhost:11434/v1 \
    node use-chat.js
```

现在,跑 **有仪器**,您可以使用 [实例/遥测.js](https://npmcn.com/package/%40elastic/opentelemetry-instrumentation-openai/test/fixtures/telemetry.js) 使用此仪器将 OpenTeleometry SDK 绑起 。 添加节点.js `-r ./telemetry.js` 选择脚本运行前的拖曳。 例如:

```bash
# Configure the OTel SDK as appropriate for your setup:
export OTEL_EXPORTER_OTLP_ENDPOINT=https://{your-otlp-endpoint.example.com}
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=..."
export OTEL_SERVICE_NAME=my-service

OPENAI_API_KEY=sk-... \
    node -r ./telemetry.js use-chat.js
```

# 配置

| 选项 | 类型 | 说明 |
| --- | --- | --- |
| `captureMessageContent` | `boolean` | 允许获取内容数据, 如快速和完成内容 。 默认 `false` 避免敏感数据可能暴露. `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` 环境变量覆盖. |

例如:

```bash
cd examples
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true \
    OPENAI_API_KEY=sk-... \
    node -r ./telemetry.js use-chat.js
```

# 无害环境管理

ECMAScript 模块(ESM)代码的 OpenTeleomet 仪器 -- -- 使用代码 `import ...` 而不是说 `require(...)` -是实验性的,而且非常有限. 本节显示,有可能获得以下仪器: `openai` 使用无害环境管理编码.

```bash
npm install
npm run compile
cd examples
node --import ./telemetry.mjs use-chat-esm.mjs
```

见注解 [实例/遥测.mjs](https://npmcn.com/package/%40elastic/opentelemetry-instrumentation-openai/examples/telemetry.mjs) 页:1 限制这一点。 限制在于OpenTeleometry JS,而不是这个 仪器.

(TODO:为OTel JS为这一支持开展的必要工作创建并指明一个后续问题。 )

## 常见问题

### @elastic/opentelemetry-instrumentation-openai 是什么？

用于 openai OpenAI 客户端库注意：该包已被作者标记为弃用——This instrumentation has moved to @opentelemetry/instrumentation-openai\.

### 如何安装 @elastic/opentelemetry-instrumentation-openai？

在项目目录运行 npm install @elastic/opentelemetry-instrumentation-openai，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：@opentelemetry/api（^1\.3\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @elastic/opentelemetry-instrumentation-openai 支持 TypeScript 吗？

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

### @elastic/opentelemetry-instrumentation-openai 支持 ESM 还是 CommonJS？

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

### @elastic/opentelemetry-instrumentation-openai 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js ^18\.19\.0 \|\| \>=20\.6\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @elastic/opentelemetry-instrumentation-openai 可以商用吗？

@elastic/opentelemetry-instrumentation-openai 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @elastic/opentelemetry-instrumentation-openai 的下载量有多少？

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

### @elastic/opentelemetry-instrumentation-openai 还在维护吗？

最新版本 0\.5\.1 发布于 2025/07/15（1\.3 年前）；至今共发布 5 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/12/13。作者已将其标记为弃用，新项目建议寻找替代方案。

