@elastic/opamp-client-node

中文文档
0.5.1发布于 2026/10/08Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 13.8万

an OpAMP client for Node.js

原始使用文档
文档目录7 节
  1. Usage
  2. Usage with remote config
  3. Using an OpenTelemetry resource for AgentDescription
  4. Reference
  5. onMessage
  6. diagEnabled
  7. Limitations

@elastic/opamp-client-node

An Open Agent Management Protocol (OpAMP) client for Node.js.

Usage

The minimal usage of this OpAMP client is something like this:

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

// ...

Usage with remote config

Here is how to use the OpAMP client with the optional OpAMP agent configuration support:

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

// ...

Using an OpenTelemetry resource for AgentDescription

The OpAMP client requires an AgentDescription to run. What data is included in this description is up to the user, but the OpAMP spec has some recommendations on what Semantic Conventions resource attributes "SHOULD" be included.

Here is some code that gathers those recommended attributes from an OpenTelemetry JavaScript Resource:

/**
 * 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.*',
    ]
});

// ...

Reference

TODO: most of the reference docs

onMessage

Note: If the ReportsRemoteConfig capability was set, it is up to user to call client.setRemoteConfigStatus(...) as appropriate. (TODO: example of this)

TODO: more detail on using remote config - onMessage shouldn't throw - should handle multiple calls with dupe data: e.g. use remoteConfig.configHash to reduce work. They should be processed serially. I.e. for node.js the configHash check should be sync. - mention how to set status APPLIED and FAILED if surprised by the config - this requires setting the ReportsRemoteConfig capability - perhaps utility for single JSON config.

diagEnabled

TODO: doc this, mark as experimental unpromised msg structure

Limitations

  • Only supports HTTP transport (no WebSocket transport).
  • Supports only a subset of the spec: none of the Beta or Development features unless otherwise shown. TODO: be specific

版本与发布记录

@elastic/opamp-client-node 共发布 6 个版本,首次发布于 2025/06/06;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.5.1latest2026/10/081 个月前
0.5.02026/06/174 个月前
0.4.02025/11/111 年前
0.3.02025/11/061 年前
0.2.02025/06/121.4 年前
0.1.02025/06/061.4 年前

关于 @elastic/opamp-client-node 的常见问题

@elastic/opamp-client-node 是什么?

an OpAMP client for Node.js

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