@elastic/opentelemetry-instrumentation-openai

中文文档
0.5.1发布于 2025/07/15Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 9489

OpenTelemetry instrumentation for the openai OpenAI client library

此软件包已被标记为弃用

This instrumentation has moved to @opentelemetry/instrumentation-openai.

原始使用文档

Elastic's OpenTelemetry instrumentation for openai

This module, @elastic/opentelemetry-instrumentation-openai, provides automatic instrumentation of openai, the OpenAI Node.js client library.

It attempts to track the GenAI semantic conventions.

Status

Instrumented OpenAI API endpoints:

Supported versions

  • This instruments the openai package in the range: >=4.19.0 <5.
  • This supports Node.js 18 and later. (openai@4 currently only tests with Node.js v18.)

Semantic Conventions

This instrumentation currently implements version 1.29.0 of the GenAI semantic-conventions: https://opentelemetry.io/docs/specs/semconv/gen-ai/

Installation

npm install @elastic/opentelemetry-instrumentation-openai

Usage

First install the packages used in the example:

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

Save this to a file, say "example.js". (This example shows the OTel setup code and app code in the same file. Typically, the OTel setup code would be in a separate file and run via node -r .... See a more complete OTel setup example here.)

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

Then run it:

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

By default, the NodeSDK will export telemetry via OTLP. As a first example to see the telemetry on the console use:

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

Examples

In the "examples/" directory, use-chat.js is a simple script using the OpenAI Chat Completion API. First, run the script without instrumentation.

Using OpenAI:

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

Using Azure OpenAI (this assumes your Azure OpenAI endpoint has a model deployment with the name 'gpt-4o-mini'):

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

Using Ollama (a tool for running LLMs locally, it exposes an OpenAI-compatible API):

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

Now, to run with instrumentation, you can use examples/telemetry.js to bootstrap the OpenTelemetry SDK using this instrumentation. Add the Node.js -r ./telemetry.js option to bootstrap before the script runs. For example:

# 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

Configuration

OptionTypeDescription
captureMessageContentbooleanEnable capture of content data, such as prompt and completion content. Default false to avoid possible exposure of sensitive data. OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment variable overrides.

For example:

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

ESM

OpenTelemetry instrumentation of ECMAScript Module (ESM) code -- code using import ... rather than require(...) -- is experimental and very limited. This section shows that it is possible to get instrumentation of openai working with ESM code.

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

See the comments in examples/telemetry.mjs for limitations with this. The limitations are with OpenTelemetry JS, not with this instrumentation.

(TODO: Create and point to a follow-up issue(s) for necessary OTel JS work for this support.)

版本与发布记录

@elastic/opentelemetry-instrumentation-openai 共发布 5 个版本,首次发布于 2024/12/13;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2025/07/151.3 年前
0.5.02025/04/011.6 年前
0.4.12024/12/191.9 年前
0.4.02024/12/131.9 年前
0.3.02024/12/131.9 年前

关于 @elastic/opentelemetry-instrumentation-openai 的常见问题

@elastic/opentelemetry-instrumentation-openai 是什么?

OpenTelemetry instrumentation for the openai OpenAI client library注意:该包已被作者标记为弃用——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。作者已将其标记为弃用,新项目建议寻找替代方案。