# @elastic/opamp-client-node 中文文档

规范地址：https://npmcn.com/package/%40elastic/opamp-client-node/

版本：0\.5\.1
npm 元数据更新时间：2026-10-07T16:49:47.150Z
中文译文更新时间：2026-11-13T10:40:45.953Z
软件包许可：Apache-2\.0

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

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

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

- 包体积（解压后）：196 kB，18 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：^18\.19\.0 \|\| \>=20\.6\.0
- 运行依赖：3 个
- 首次发布：2025-06-05T19:52:12.978Z
- 最新版本发布：2026-10-07T16:49:46.679Z
- 近 30 天下载量：138295（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 2 个）

## 中文简介

一个用于节点.js的 OpAMP 客户端

## 安装

```sh
npm install @elastic/opamp-client-node
```

也可以使用：`pnpm add @elastic/opamp-client-node`、`yarn add @elastic/opamp-client-node`、`bun add @elastic/opamp-client-node`

## README

# @ 弹性/ opamp- 客户端- 节点

一个用于Node.js的开放代理管理协议(OpAMP)客户端.

## 使用量

这个OpAMP客户端的最小用途就是这样:

```js
import {createOpAMPClient} from '@elastic/opamp-client-node';

const client = createOpAMPClient({endpoint: 'http://localhost:4320/v1/opamp'});
client.setAgentDescription({
    identifyingAttributes: { 'service.name': 'minimal-example' },
});
client.start();

process.on('beforeExit', async () => {
    await client.shutdown();
});

// ...
```

### 使用远程配置

以下是如何使用可选的 OpAMP 客户端 [OpAMP 代理配置支持](https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#configuration)数字 :

```js
import {
    createOpAMPClient,
    AgentCapabilities,
    RemoteConfigStatuses
} from '@elastic/opamp-client-node';

const client = createOpAMPClient({
    endpoint: 'http://localhost:4320/v1/opamp',
    capabilities:
        AgentCapabilities.AgentCapabilities_AcceptsRemoteConfig |
        AgentCapabilities.AgentCapabilities_ReportsRemoteConfig,
    onMessage: ({remoteConfig}) => {
        if (remoteConfig) {
            console.log('Got remote config:', remoteConfig);

            // Apply the remote config.
            // ...

            // Report the remote config status.
            client.setRemoteConfigStatus({
                status: RemoteConfigStatuses.RemoteConfigStatuses_APPLIED,
                lastRemoteConfigHash: remoteConfig.configHash,
            });
        }
    },
});
client.setAgentDescription({
    identifyingAttributes: { 'service.name': 'minimal-example' },
});
client.start();

process.on('beforeExit', async () => {
    await client.shutdown();
});

// ...
```

### 使用 Agent 的 OpenTeleometry 资源Description

OpAMP 客户端需要 [`AgentDescription`](https://github.com/open-telemetry/opamp-spec/blob/main/specification.md#agentdescription-message) 运行。 此描述中包含哪些数据由用户决定, 但 OpAMP spec对什么语义公约有一些建议 [资源属性](https://opentelemetry.io/docs/concepts/resources/) 包括"SHOULD".

这里有一些代码,从一个 [打开远程 JavaScript `Resource`](https://opentelemetry.io/docs/languages/js/resources/)数字 :

```js
/**
 * Copy properties from `fromObj` to `toObj` that match the given name
 * patterns (`namePats`). This is a shallow copy.
 *
 * `namePats` supports an extremely limited subset of glob matching: if a
 * pattern ends with an asterisk (`*`), then the rest of string is compared
 * with `.startsWith()`. For example `cloud.*` matches a 'cloud.foo' property
 * of `fromObj`. Otherwise the pattern uses an exact match.
 */
function pickProps(toObj, fromObj, namePats) {
    const fromKeys = Object.keys(fromObj)
    for (let namePat of namePats) {
        if (namePat.endsWith('*')) {
            const prefix = namePat.slice(0, -1);
            for (let k of fromKeys) {
                if (k.startsWith(prefix)) {
                    toObj[k] = fromObj[k];
                }
            }
        } else {
            if (namePat in fromObj) {
                toObj[namePat] = fromObj[namePat];
            }
        }
    }
    return toObj;
};

const resource = ...;
const client = createOpAMPClient(...);

await resource?.waitForAsyncAttributes();
client.setAgentDescription({
    identifyingAttributes: pickProps({}, resource.attributes, [
        'service.name', 'service.namespace', 'service.version',
        'service.instance.id',
    ]
    nonIdentifyingAttributes: pickProps({}, resource.attributes, [
        'os.type', 'os.version',
        'host.*',
        'cloud.*',
    ]
});

// ...
```

## 参考文献

TODO: 大部分参考文件

### 新闻

注:如果 `ReportsRemoteConfig` 建立了能力,由用户决定 调用 `client.setRemoteConfigStatus(...)` 视情况而定. (TODO:这方面的例子)

TODO: 关于使用远程配置的更多细节 - 在Message不应该扔 - 应使用dupe数据处理多个呼叫:例如使用远程Config.configHash 减少工作。 应该处理 _序列_节点 配置Hash 检查应当同步 . - 提到如何设置状态 APLIED和失败 如果惊讶于配置 - 这需要建立报告检索能力 - 也许JSON配置有用.

### 可分割

TODO: doc this, 标记为实验性未承诺的 msg 结构

## 限制

-   只支持HTTP运输(没有WebSocket运输).
-   仅支持 spec 的子集: 没有Beta 或 Development 特性 除非另有说明。 TODO: 请具体说明

## 常见问题

### @elastic/opamp-client-node 是什么？

一个用于节点\.js的 OpAMP 客户端

### 如何安装 @elastic/opamp-client-node？

在项目目录运行 npm install @elastic/opamp-client-node，或使用 pnpm add / yarn add / bun add。

### @elastic/opamp-client-node 支持 TypeScript 吗？

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

### @elastic/opamp-client-node 支持 ESM 还是 CommonJS？

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

### @elastic/opamp-client-node 需要什么版本的 Node\.js？

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

### @elastic/opamp-client-node 可以商用吗？

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

### @elastic/opamp-client-node 的下载量有多少？

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

### @elastic/opamp-client-node 还在维护吗？

最新版本 0\.5\.1 发布于 2026/10/08（1 个月前）；至今共发布 6 个版本，其中过去 12 个月发布了 2 个；首次发布于 2025/06/06。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

