@aidra/pmx-io 9.44.0 → 9.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -3
- package/bin/pmx-io-node.js +1 -1
- package/lib/envname.js +84 -0
- package/lib/otel/env.js +5 -4
- package/lib/pmx.js +7 -4
- package/lib/standalone/activate.js +5 -3
- package/lib/standalone/transport.js +9 -7
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -31,11 +31,17 @@ Set the bucket keys (prodman: *Bucket settings → Connect servers*) and an app
|
|
|
31
31
|
|
|
32
32
|
```sh
|
|
33
33
|
PRODMAN_URL=https://prodman.example.com
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
PMX_PUBLIC_KEY=<public key>
|
|
35
|
+
PMX_SECRET_KEY=<secret key>
|
|
36
|
+
PMX_APP_NAME=orders-api
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
+
The pm2 names (`PM2_PUBLIC_KEY`, `PM2_SECRET_KEY`, `PM2_APP_NAME`, `PM2_MACHINE_NAME`) are still
|
|
40
|
+
accepted for pm2 compatibility. When both forms are set, the `PMX_` name wins (one warning names
|
|
41
|
+
both, never the values). The `PMX_` forms need @aidra/pmx-io 9.33 or later (pmx 9.33+); the
|
|
42
|
+
standalone root removes the keys under both names from its environment, so children never see
|
|
43
|
+
them. See [the environment variable reference](../../docs/env_variables.md).
|
|
44
|
+
|
|
39
45
|
Then load the library without changing code:
|
|
40
46
|
|
|
41
47
|
```sh
|
package/bin/pmx-io-node.js
CHANGED
|
@@ -23,7 +23,7 @@ const MJS = path.join(PKG, 'standalone.mjs')
|
|
|
23
23
|
const FORWARDED = ['SIGTERM', 'SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2']
|
|
24
24
|
const USAGE = 'usage: pmx-io-node run [--] <command> [args...]\n' +
|
|
25
25
|
' Runs <command> with @aidra/pmx-io in standalone mode (reports to prodman without pmx).\n' +
|
|
26
|
-
' Needs
|
|
26
|
+
' Needs PMX_PUBLIC_KEY, PMX_SECRET_KEY and PMX_APP_NAME (or the PM2_ names; and PRODMAN_URL) in the environment.\n'
|
|
27
27
|
|
|
28
28
|
function quote (p) {
|
|
29
29
|
return '"' + p.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'
|
package/lib/envname.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) 2026 Mike Warriner. All rights reserved. Proprietary — see LICENSE.
|
|
2
|
+
'use strict'
|
|
3
|
+
|
|
4
|
+
// PMX_/PM2_ environment variable names (pmx 02 D-86). Every setting that pm2 reads as
|
|
5
|
+
// `PM2_<X>` is also accepted as `PMX_<X>`; the PMX_ name wins, then the PM2_ name, then any
|
|
6
|
+
// legacy alias in its existing order. The first non-empty value wins; when every name is unset
|
|
7
|
+
// or empty, the first one that is set (empty) is returned so presence checks keep working.
|
|
8
|
+
// When PMX_<X> and PM2_<X> are both non-empty and differ, one warning names both (never the
|
|
9
|
+
// values). Code reads through `lookup(env, 'PUBLIC_KEY')` (bare suffix) or `get(env, …)`.
|
|
10
|
+
|
|
11
|
+
const warned = new Set()
|
|
12
|
+
let warnSink = (line) => {
|
|
13
|
+
try {
|
|
14
|
+
process.stderr.write(line + '\n')
|
|
15
|
+
} catch (_) {}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The suffix of `name` without a PMX_/PM2_ prefix (`PUBLIC_KEY` for `PM2_PUBLIC_KEY`). */
|
|
19
|
+
function suffix (name) {
|
|
20
|
+
return name.startsWith('PMX_') || name.startsWith('PM2_') ? name.slice(4) : name
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The accepted names in precedence order: PMX_<X>, PM2_<X>, then `legacy`. */
|
|
24
|
+
function names (name, legacy) {
|
|
25
|
+
const s = suffix(name)
|
|
26
|
+
return ['PMX_' + s, 'PM2_' + s].concat(legacy || [])
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** `{ value, name }` of the first accepted name (see the file comment), or null. */
|
|
30
|
+
function lookup (env, name, legacy) {
|
|
31
|
+
env = env || process.env
|
|
32
|
+
const all = names(name, legacy)
|
|
33
|
+
const pmx = env[all[0]]
|
|
34
|
+
const pm2 = env[all[1]]
|
|
35
|
+
if (pmx && pm2 && String(pmx) !== String(pm2)) {
|
|
36
|
+
const line = `[pmx] ${all[0]} and ${all[1]} are both set and differ; using ${all[0]}`
|
|
37
|
+
if (!warned.has(line)) {
|
|
38
|
+
warned.add(line)
|
|
39
|
+
warnSink(line)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let firstSet = null
|
|
43
|
+
for (const n of all) {
|
|
44
|
+
const v = env[n]
|
|
45
|
+
if (v === undefined || v === null) continue
|
|
46
|
+
if (v !== '') return { value: v, name: n }
|
|
47
|
+
if (!firstSet) firstSet = { value: v, name: n }
|
|
48
|
+
}
|
|
49
|
+
return firstSet
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The value of `lookup`, or undefined. */
|
|
53
|
+
function get (env, name, legacy) {
|
|
54
|
+
const h = lookup(env, name, legacy)
|
|
55
|
+
return h ? h.value : undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Sets `PMX_<X>` and `PM2_<X>` to the same value (for child processes). */
|
|
59
|
+
function setBoth (env, name, value) {
|
|
60
|
+
const s = suffix(name)
|
|
61
|
+
env['PMX_' + s] = value
|
|
62
|
+
env['PM2_' + s] = value
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Deletes `PMX_<X>` and `PM2_<X>`. */
|
|
66
|
+
function deleteBoth (env, name) {
|
|
67
|
+
const s = suffix(name)
|
|
68
|
+
delete env['PMX_' + s]
|
|
69
|
+
delete env['PM2_' + s]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The bucket keys: never passed on to descendants (NIO-262) and never logged. */
|
|
73
|
+
const SECRET_NAMES = ['PMX_SECRET_KEY', 'PM2_SECRET_KEY', 'KEYMETRICS_SECRET']
|
|
74
|
+
|
|
75
|
+
/** Every PMX_/PM2_ pair this package reads (bare suffixes). */
|
|
76
|
+
const PAIRED = ['PUBLIC_KEY', 'SECRET_KEY', 'APP_NAME', 'MACHINE_NAME']
|
|
77
|
+
|
|
78
|
+
/** Test seam: replaces the warning sink and forgets earlier warnings. */
|
|
79
|
+
function _setWarnSink (fn) {
|
|
80
|
+
warnSink = fn
|
|
81
|
+
warned.clear()
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = { suffix, names, lookup, get, setBoth, deleteBoth, SECRET_NAMES, PAIRED, _setWarnSink }
|
package/lib/otel/env.js
CHANGED
|
@@ -4,12 +4,13 @@
|
|
|
4
4
|
// `OTEL_*` wiring for Mode S (85 OTX-052, the rules of OTX-201…205 without the receiver and the
|
|
5
5
|
// pm_id key): `pmx-io-node run` with PMX_IO_OTEL_EXPORT=1 points an app's own OpenTelemetry SDK at
|
|
6
6
|
// prodman's OTLP endpoint (`<PRODMAN_URL>/otlp`, protocol http/protobuf, header
|
|
7
|
-
// `Authorization=Basic%20<base64(public:secret)>`), names the service after PM2_APP_NAME and adds
|
|
7
|
+
// `Authorization=Basic%20<base64(public:secret)>`), names the service after PMX_APP_NAME (or PM2_APP_NAME) and adds
|
|
8
8
|
// resource attributes — each only where the environment does not set it (OTX-208: nothing the
|
|
9
9
|
// app sets is replaced; merges append). Opt-in because it places the bucket secret in the
|
|
10
10
|
// environment of every child process (PYI-152 / B.15 otherwise avoid it). Loaded by the launcher
|
|
11
11
|
// only when the opt-in is set.
|
|
12
12
|
|
|
13
|
+
const envname = require('../envname')
|
|
13
14
|
const ENDPOINTS = ['OTEL_EXPORTER_OTLP_ENDPOINT', 'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', 'OTEL_EXPORTER_OTLP_LOGS_ENDPOINT', 'OTEL_EXPORTER_OTLP_METRICS_ENDPOINT']
|
|
14
15
|
const PROTOCOLS = ['OTEL_EXPORTER_OTLP_PROTOCOL', 'OTEL_EXPORTER_OTLP_TRACES_PROTOCOL', 'OTEL_EXPORTER_OTLP_LOGS_PROTOCOL', 'OTEL_EXPORTER_OTLP_METRICS_PROTOCOL']
|
|
15
16
|
const SIGNAL_HEADERS = ['OTEL_EXPORTER_OTLP_TRACES_HEADERS', 'OTEL_EXPORTER_OTLP_LOGS_HEADERS', 'OTEL_EXPORTER_OTLP_METRICS_HEADERS']
|
|
@@ -42,7 +43,7 @@ function appendPairs (current, pairs) {
|
|
|
42
43
|
|
|
43
44
|
/**
|
|
44
45
|
* Mutate `env` (the app's environment) per OTX-052. `info`: { url (normalised prodman URL),
|
|
45
|
-
* machine (the standalone server name, CTR-015), app (default PM2_APP_NAME), container:
|
|
46
|
+
* machine (the standalone server name, CTR-015), app (default PMX_APP_NAME, else PM2_APP_NAME), container:
|
|
46
47
|
* [[key, value]] }. Standalone has no pm_id: `service.instance.id` is the server name (as pmx-io).
|
|
47
48
|
* Returns the list of variables set or changed.
|
|
48
49
|
*/
|
|
@@ -57,12 +58,12 @@ function wire (env, info) {
|
|
|
57
58
|
if (!ENDPOINTS.some((k) => set(env, k))) {
|
|
58
59
|
put('OTEL_EXPORTER_OTLP_ENDPOINT', info.url.replace(/\/+$/, '') + '/otlp')
|
|
59
60
|
if (!PROTOCOLS.some((k) => set(env, k))) put('OTEL_EXPORTER_OTLP_PROTOCOL', 'http/protobuf')
|
|
60
|
-
const auth = 'Basic%20' + Buffer.from(`${env
|
|
61
|
+
const auth = 'Basic%20' + Buffer.from(`${envname.get(env, 'PUBLIC_KEY') || ''}:${envname.get(env, 'SECRET_KEY') || ''}`).toString('base64')
|
|
61
62
|
const withAuth = (v) => (names(v).has('authorization') ? v : (v ? v + ',' : '') + 'Authorization=' + auth)
|
|
62
63
|
put('OTEL_EXPORTER_OTLP_HEADERS', withAuth(env.OTEL_EXPORTER_OTLP_HEADERS || ''))
|
|
63
64
|
for (const k of SIGNAL_HEADERS) if (set(env, k)) put(k, withAuth(env[k]))
|
|
64
65
|
}
|
|
65
|
-
const app = info.app || env
|
|
66
|
+
const app = info.app || envname.get(env, 'APP_NAME')
|
|
66
67
|
const machine = info.machine
|
|
67
68
|
// pmx 97 PAP-051/052: `pmx.application` always, `service.namespace` when not `default`
|
|
68
69
|
const application = info.application || 'default'
|
package/lib/pmx.js
CHANGED
|
@@ -23,7 +23,10 @@ const { readPackageJsonSync, findPackageJson, isPlainObject, jsonCopy } = requir
|
|
|
23
23
|
const { VERSION } = require('./constants')
|
|
24
24
|
const debug = require('./debug')('axm:main')
|
|
25
25
|
|
|
26
|
-
const
|
|
26
|
+
const envname = require('./envname')
|
|
27
|
+
|
|
28
|
+
// standalone keys (bare suffixes: PMX_<X> first, then PM2_<X>, D-86)
|
|
29
|
+
const STANDALONE_KEYS = ['PUBLIC_KEY', 'SECRET_KEY', 'APP_NAME']
|
|
27
30
|
|
|
28
31
|
const printed = new Set()
|
|
29
32
|
function printOnce (line) {
|
|
@@ -162,11 +165,11 @@ class PMX {
|
|
|
162
165
|
}
|
|
163
166
|
const underDaemon = process.env.pm_id !== undefined && typeof process.send === 'function'
|
|
164
167
|
if (underDaemon) return new IPCTransport()
|
|
165
|
-
const present = STANDALONE_KEYS.filter((k) => process.env
|
|
168
|
+
const present = STANDALONE_KEYS.filter((k) => envname.get(process.env, k))
|
|
166
169
|
const wantStandalone = config.standalone === true || present.length === STANDALONE_KEYS.length || process.env.PMX_IO_STANDALONE === '1'
|
|
167
170
|
if (!wantStandalone && present.length > 0) {
|
|
168
|
-
const missing = STANDALONE_KEYS.filter((k) => !process.env
|
|
169
|
-
printOnce(`[@pm2/io] not reporting to prodman: ${missing.join(', ')} not set (standalone mode needs
|
|
171
|
+
const missing = STANDALONE_KEYS.filter((k) => !envname.get(process.env, k)).map((k) => 'PMX_' + k)
|
|
172
|
+
printOnce(`[@pm2/io] not reporting to prodman: ${missing.join(', ')} not set (standalone mode needs PMX_PUBLIC_KEY, PMX_SECRET_KEY and PMX_APP_NAME, or the PM2_ names)`)
|
|
170
173
|
}
|
|
171
174
|
if (wantStandalone) {
|
|
172
175
|
try {
|
|
@@ -9,8 +9,9 @@
|
|
|
9
9
|
const path = require('path')
|
|
10
10
|
const { PKG_DIR } = require('../util')
|
|
11
11
|
const debug = require('../debug')('axm:standalone')
|
|
12
|
+
const envname = require('../envname')
|
|
12
13
|
|
|
13
|
-
const MISSING_LINE = '[@pm2/io] not reporting to prodman: %s not set (standalone mode needs
|
|
14
|
+
const MISSING_LINE = '[@pm2/io] not reporting to prodman: %s not set (standalone mode needs PMX_PUBLIC_KEY, PMX_SECRET_KEY and PMX_APP_NAME, or the PM2_ names)'
|
|
14
15
|
const UNREACHABLE_LINE = '[@pm2/io] not reporting: the standalone root (pid %s) is not reachable; start the server with exec'
|
|
15
16
|
const TREE_ENV = ['PMX_IO_STANDALONE', 'PMX_IO_ROOT_PID', 'PMX_IO_ROOT_PPID', 'PMX_IO_HUB', 'PMX_IO_HUB_TOKEN', 'PMX_IO_CAPTURE_LOGS']
|
|
16
17
|
const STANDALONE_CJS = path.join(PKG_DIR, 'standalone.cjs')
|
|
@@ -115,8 +116,9 @@ function setupRoot (cfg, info, config) {
|
|
|
115
116
|
delete env.PMX_IO_ROOT_PPID
|
|
116
117
|
env.PMX_IO_STANDALONE = '1'
|
|
117
118
|
// NIO-262: descendants report through the hub and never need the bucket keys
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
// (under both names: PMX_SECRET_KEY / PMX_PUBLIC_KEY as well as the PM2_ ones)
|
|
120
|
+
envname.deleteBoth(env, 'SECRET_KEY')
|
|
121
|
+
envname.deleteBoth(env, 'PUBLIC_KEY')
|
|
120
122
|
const handle = {
|
|
121
123
|
transport: null,
|
|
122
124
|
hub: null,
|
|
@@ -11,6 +11,7 @@ const fs = require('fs')
|
|
|
11
11
|
const path = require('path')
|
|
12
12
|
const crypto = require('crypto')
|
|
13
13
|
const { VERSION } = require('../constants')
|
|
14
|
+
const envname = require('../envname')
|
|
14
15
|
const container = require('./container')
|
|
15
16
|
const debug = require('../debug')('axm:standalone')
|
|
16
17
|
|
|
@@ -49,7 +50,8 @@ const IPV4 = /^\d+\.\d+\.\d+\.\d+(:\d+)?(\/|$)/
|
|
|
49
50
|
function resolveUrl (configured, env) {
|
|
50
51
|
env = env || process.env
|
|
51
52
|
let raw = null
|
|
52
|
-
|
|
53
|
+
// KEYMETRICS_NODE is no longer read (pmx 9.33, 02 D-86)
|
|
54
|
+
for (const v of [configured, env.PRODMAN_URL]) {
|
|
53
55
|
if (typeof v === 'string' && v.trim()) {
|
|
54
56
|
raw = v.trim()
|
|
55
57
|
break
|
|
@@ -85,16 +87,16 @@ function resolveConfig (apmOptions, env, info, base) {
|
|
|
85
87
|
const v = opt(k)
|
|
86
88
|
if (v !== undefined) return v
|
|
87
89
|
if (b && b[k]) return b[k]
|
|
88
|
-
return env
|
|
90
|
+
return envname.get(env, envName) || null
|
|
89
91
|
}
|
|
90
92
|
const cfg = {}
|
|
91
93
|
cfg.url = resolveUrl(opt('url') || (b ? b.url : undefined), env)
|
|
92
|
-
cfg.publicKey = pick('publicKey', '
|
|
93
|
-
cfg.secretKey = pick('secretKey', '
|
|
94
|
-
cfg.appName = pick('appName', '
|
|
94
|
+
cfg.publicKey = pick('publicKey', 'PUBLIC_KEY')
|
|
95
|
+
cfg.secretKey = pick('secretKey', 'SECRET_KEY')
|
|
96
|
+
cfg.appName = pick('appName', 'APP_NAME')
|
|
95
97
|
let explicit = opt('serverName')
|
|
96
98
|
if (explicit === undefined && b) explicit = b.explicitServerName
|
|
97
|
-
if (explicit === undefined || explicit === null) explicit = env
|
|
99
|
+
if (explicit === undefined || explicit === null) explicit = envname.get(env, 'MACHINE_NAME') || null
|
|
98
100
|
cfg.explicitServerName = explicit
|
|
99
101
|
cfg.sendLogs = opt('sendLogs') !== undefined ? Boolean(o.sendLogs) : Boolean(b && b.sendLogs)
|
|
100
102
|
cfg.disableLogs = opt('disableLogs') !== undefined ? Boolean(o.disableLogs) : Boolean(b && b.disableLogs)
|
|
@@ -114,7 +116,7 @@ function resolveConfig (apmOptions, env, info, base) {
|
|
|
114
116
|
let proxy = opt('proxy')
|
|
115
117
|
if (proxy === undefined) proxy = b ? b.proxy : (env.PMX_IO_PROXY || null)
|
|
116
118
|
cfg.proxy = proxy || null
|
|
117
|
-
cfg.missing = [['
|
|
119
|
+
cfg.missing = [['PMX_PUBLIC_KEY', 'publicKey'], ['PMX_SECRET_KEY', 'secretKey'], ['PMX_APP_NAME', 'appName']].filter(([, k]) => !cfg[k]).map(([n]) => n)
|
|
118
120
|
for (const k of ['publicKey', 'secretKey', 'appName']) if (cfg[k] !== null) cfg[k] = String(cfg[k])
|
|
119
121
|
cfg.serverName = serverName(cfg, info)
|
|
120
122
|
return cfg
|
package/package.json
CHANGED