@ohos-ports/pm2 0.0.0-stage → 7.1.2-beta.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/CHANGELOG.md +2607 -0
- package/CONTRIBUTING.md +124 -0
- package/GNU-AGPL-3.0.txt +665 -0
- package/LICENSE +1 -0
- package/README.md +255 -2
- package/SECURITY.md +41 -0
- package/bin/pm2 +3 -0
- package/bin/pm2-dev +3 -0
- package/bin/pm2-docker +3 -0
- package/bin/pm2-runtime +3 -0
- package/bin/pm2.ps1 +3 -0
- package/constants.js +113 -0
- package/index.js +12 -0
- package/lib/API/Configuration.js +212 -0
- package/lib/API/Containerizer.js +335 -0
- package/lib/API/Dashboard.js +726 -0
- package/lib/API/Deploy.js +117 -0
- package/lib/API/Extra.js +798 -0
- package/lib/API/ExtraMgmt/Docker.js +30 -0
- package/lib/API/Log.js +388 -0
- package/lib/API/LogManagement.js +351 -0
- package/lib/API/Modules/LOCAL.js +122 -0
- package/lib/API/Modules/Modularizer.js +148 -0
- package/lib/API/Modules/NPM.js +443 -0
- package/lib/API/Modules/TAR.js +368 -0
- package/lib/API/Modules/flagExt.js +46 -0
- package/lib/API/Modules/index.js +120 -0
- package/lib/API/Monit.js +247 -0
- package/lib/API/Serve.js +497 -0
- package/lib/API/Startup.js +630 -0
- package/lib/API/UX/helpers.js +213 -0
- package/lib/API/UX/index.js +9 -0
- package/lib/API/UX/pm2-describe.js +197 -0
- package/lib/API/UX/pm2-ls-minimal.js +31 -0
- package/lib/API/UX/pm2-ls.js +581 -0
- package/lib/API/Version.js +383 -0
- package/lib/API/interpreter.json +12 -0
- package/lib/API/pm2-plus/PM2IO.js +381 -0
- package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
- package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
- package/lib/API/pm2-plus/helpers.js +97 -0
- package/lib/API/pm2-plus/link.js +126 -0
- package/lib/API/pm2-plus/pres/motd +16 -0
- package/lib/API/pm2-plus/pres/motd.update +26 -0
- package/lib/API/pm2-plus/pres/welcome +28 -0
- package/lib/API/pm2-plus/process-selector.js +52 -0
- package/lib/API/schema.json +375 -0
- package/lib/API.js +1980 -0
- package/lib/Client.js +793 -0
- package/lib/Common.js +949 -0
- package/lib/Configuration.js +309 -0
- package/lib/Daemon.js +468 -0
- package/lib/Event.js +37 -0
- package/lib/God/ActionMethods.js +955 -0
- package/lib/God/ClusterMode.js +192 -0
- package/lib/God/ForkMode.js +205 -0
- package/lib/God/Methods.js +292 -0
- package/lib/God/Reload.js +300 -0
- package/lib/God/StdioLogger.js +239 -0
- package/lib/God.js +653 -0
- package/lib/HttpInterface.js +75 -0
- package/lib/OtelManager.js +74 -0
- package/lib/ProcessContainer.js +321 -0
- package/lib/ProcessContainerBun.js +355 -0
- package/lib/ProcessContainerFork.js +42 -0
- package/lib/ProcessContainerForkBun.js +31 -0
- package/lib/ProcessUtils.js +74 -0
- package/lib/TreeKill.js +111 -0
- package/lib/Utility.js +304 -0
- package/lib/VersionCheck.js +46 -0
- package/lib/Watcher.js +117 -0
- package/lib/Worker.js +200 -0
- package/lib/binaries/CLI.js +1092 -0
- package/lib/binaries/DevCLI.js +183 -0
- package/lib/binaries/Runtime.js +101 -0
- package/lib/binaries/Runtime4Docker.js +192 -0
- package/lib/completion.js +229 -0
- package/lib/completion.sh +40 -0
- package/lib/motd +36 -0
- package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
- package/lib/templates/ecosystem-es.tpl +24 -0
- package/lib/templates/ecosystem-simple-es.tpl +8 -0
- package/lib/templates/ecosystem-simple.tpl +6 -0
- package/lib/templates/ecosystem.tpl +22 -0
- package/lib/templates/init-scripts/launchd.tpl +35 -0
- package/lib/templates/init-scripts/openrc.tpl +52 -0
- package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
- package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
- package/lib/templates/init-scripts/rcd.tpl +44 -0
- package/lib/templates/init-scripts/smf.tpl +43 -0
- package/lib/templates/init-scripts/systemd-online.tpl +22 -0
- package/lib/templates/init-scripts/systemd.tpl +22 -0
- package/lib/templates/init-scripts/upstart.tpl +103 -0
- package/lib/templates/logrotate.d/pm2 +10 -0
- package/lib/templates/sample-apps/http-server/README.md +14 -0
- package/lib/templates/sample-apps/http-server/api.js +9 -0
- package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
- package/lib/templates/sample-apps/http-server/package.json +11 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
- package/lib/templates/sample-apps/python-app/README.md +4 -0
- package/lib/templates/sample-apps/python-app/echo.py +7 -0
- package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
- package/lib/templates/sample-apps/python-app/package.json +11 -0
- package/lib/tools/Config.js +251 -0
- package/lib/tools/SysMetrics.js +375 -0
- package/lib/tools/copydirSync.js +101 -0
- package/lib/tools/deleteFolderRecursive.js +19 -0
- package/lib/tools/find-package-json.js +74 -0
- package/lib/tools/fmt.js +72 -0
- package/lib/tools/interpreter.js +88 -0
- package/lib/tools/isbinaryfile.js +94 -0
- package/lib/tools/json5.js +752 -0
- package/lib/tools/multimeter/bar.js +111 -0
- package/lib/tools/multimeter/charm/encode.js +18 -0
- package/lib/tools/multimeter/charm/index.js +305 -0
- package/lib/tools/multimeter/index.js +76 -0
- package/lib/tools/open.js +56 -0
- package/lib/tools/passwd.js +58 -0
- package/lib/tools/pm2-config-keys.js +165 -0
- package/lib/tools/prompt.js +76 -0
- package/lib/tools/serviceStop.js +149 -0
- package/lib/tools/sexec.js +55 -0
- package/lib/tools/treeify.js +113 -0
- package/lib/tools/typestrip.js +55 -0
- package/lib/tools/which.js +120 -0
- package/lib/tools/xdg-open +861 -0
- package/modules/blessed/LICENSE +20 -0
- package/modules/blessed/README.md +34 -0
- package/modules/blessed/index.js +1 -0
- package/modules/blessed/lib/alias.js +526 -0
- package/modules/blessed/lib/blessed.js +32 -0
- package/modules/blessed/lib/colors.js +530 -0
- package/modules/blessed/lib/events.js +189 -0
- package/modules/blessed/lib/gpmclient.js +221 -0
- package/modules/blessed/lib/helpers.js +165 -0
- package/modules/blessed/lib/keys.js +339 -0
- package/modules/blessed/lib/program.js +4297 -0
- package/modules/blessed/lib/tput.js +3022 -0
- package/modules/blessed/lib/unicode.js +885 -0
- package/modules/blessed/lib/widget.js +32 -0
- package/modules/blessed/lib/widgets/box.js +34 -0
- package/modules/blessed/lib/widgets/element.js +2570 -0
- package/modules/blessed/lib/widgets/list.js +599 -0
- package/modules/blessed/lib/widgets/log.js +83 -0
- package/modules/blessed/lib/widgets/node.js +282 -0
- package/modules/blessed/lib/widgets/screen.js +2298 -0
- package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
- package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
- package/modules/blessed/lib/widgets/text.js +35 -0
- package/modules/blessed/usr/linux +0 -0
- package/modules/blessed/usr/windows-ansi +0 -0
- package/modules/blessed/usr/xterm +0 -0
- package/modules/blessed/usr/xterm-256color +0 -0
- package/modules/blessed/usr/xterm.termcap +243 -0
- package/modules/chokidar/LICENSE +21 -0
- package/modules/chokidar/VENDORED.md +76 -0
- package/modules/chokidar/index.js +979 -0
- package/modules/chokidar/lib/brace-expand.js +180 -0
- package/modules/chokidar/lib/constants.js +66 -0
- package/modules/chokidar/lib/fsevents-handler.js +526 -0
- package/modules/chokidar/lib/nodefs-handler.js +654 -0
- package/modules/fclone.js +49 -0
- package/modules/pm2-axon/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/History.md +5 -0
- package/modules/pm2-axon/lib/configurable/Makefile +7 -0
- package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
- package/modules/pm2-axon/lib/configurable/index.js +2 -0
- package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
- package/modules/pm2-axon/lib/configurable/package.json +68 -0
- package/modules/pm2-axon/lib/index.js +44 -0
- package/modules/pm2-axon/lib/plugins/queue.js +71 -0
- package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
- package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
- package/modules/pm2-axon/lib/sockets/pub.js +77 -0
- package/modules/pm2-axon/lib/sockets/pull.js +37 -0
- package/modules/pm2-axon/lib/sockets/push.js +32 -0
- package/modules/pm2-axon/lib/sockets/rep.js +73 -0
- package/modules/pm2-axon/lib/sockets/req.js +101 -0
- package/modules/pm2-axon/lib/sockets/sock.js +430 -0
- package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
- package/modules/pm2-axon/lib/sockets/sub.js +147 -0
- package/modules/pm2-axon/lib/utils.js +19 -0
- package/modules/pm2-axon-rpc/index.js +3 -0
- package/modules/pm2-axon-rpc/lib/client.js +63 -0
- package/modules/pm2-axon-rpc/lib/server.js +139 -0
- package/modules/pm2-io-agent/LICENSE +661 -0
- package/modules/pm2-io-agent/README.md +62 -0
- package/modules/pm2-io-agent/config.js +30 -0
- package/modules/pm2-io-agent/constants.js +118 -0
- package/modules/pm2-io-agent/index.js +2 -0
- package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
- package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
- package/modules/pm2-io-agent/src/PM2Client.js +112 -0
- package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
- package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
- package/modules/pm2-io-agent/src/Utility.js +392 -0
- package/modules/pm2-io-agent/src/WatchDog.js +70 -0
- package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
- package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
- package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
- package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
- package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
- package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
- package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
- package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
- package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
- package/modules/pm2-io-agent/src/utils/units.js +10 -0
- package/modules/pm2-io-bpm/configuration.js +141 -0
- package/modules/pm2-io-bpm/constants.js +10 -0
- package/modules/pm2-io-bpm/featureManager.js +110 -0
- package/modules/pm2-io-bpm/features/dependencies.js +48 -0
- package/modules/pm2-io-bpm/features/events.js +50 -0
- package/modules/pm2-io-bpm/features/metrics.js +102 -0
- package/modules/pm2-io-bpm/features/notify.js +238 -0
- package/modules/pm2-io-bpm/features/profiling.js +70 -0
- package/modules/pm2-io-bpm/features/tracing.js +174 -0
- package/modules/pm2-io-bpm/index.js +11 -0
- package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
- package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
- package/modules/pm2-io-bpm/metrics/network.js +132 -0
- package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
- package/modules/pm2-io-bpm/metrics/v8.js +104 -0
- package/modules/pm2-io-bpm/otel/constants.js +7 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
- package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
- package/modules/pm2-io-bpm/pmx.js +380 -0
- package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
- package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
- package/modules/pm2-io-bpm/serviceManager.js +21 -0
- package/modules/pm2-io-bpm/services/actions.js +159 -0
- package/modules/pm2-io-bpm/services/inspector.js +43 -0
- package/modules/pm2-io-bpm/services/metrics.js +186 -0
- package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
- package/modules/pm2-io-bpm/services/transport.js +11 -0
- package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
- package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
- package/modules/pm2-io-bpm/utils/EDS.js +113 -0
- package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
- package/modules/pm2-io-bpm/utils/autocast.js +61 -0
- package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
- package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
- package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
- package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
- package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
- package/modules/pm2-io-bpm/utils/module.js +67 -0
- package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
- package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
- package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
- package/modules/pm2-io-bpm/utils/units.js +16 -0
- package/modules/vizion/LICENSE +13 -0
- package/modules/vizion/README.md +133 -0
- package/modules/vizion/index.js +1 -0
- package/modules/vizion/lib/git/git.js +247 -0
- package/modules/vizion/lib/vizion.js +52 -0
- package/package.json +170 -4
- package/paths.js +121 -0
- package/pm2 +11 -0
- package/types/index.d.ts +732 -0
- package/types/tsconfig.json +14 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Meter = require('../utils/metrics/meter')
|
|
4
|
+
const Counter = require('../utils/metrics/counter')
|
|
5
|
+
const Histogram = require('../utils/metrics/histogram')
|
|
6
|
+
const { ServiceManager } = require('../serviceManager')
|
|
7
|
+
const constants = require('../constants')
|
|
8
|
+
const Debug = require('debug')
|
|
9
|
+
const Gauge = require('../utils/metrics/gauge')
|
|
10
|
+
|
|
11
|
+
const MetricType = {
|
|
12
|
+
'meter': 'meter',
|
|
13
|
+
'histogram': 'histogram',
|
|
14
|
+
'counter': 'counter',
|
|
15
|
+
'gauge': 'gauge',
|
|
16
|
+
'metric': 'metric'
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const MetricMeasurements = {
|
|
20
|
+
'min': 'min',
|
|
21
|
+
'max': 'max',
|
|
22
|
+
'sum': 'sum',
|
|
23
|
+
'count': 'count',
|
|
24
|
+
'variance': 'variance',
|
|
25
|
+
'mean': 'mean',
|
|
26
|
+
'stddev': 'stddev',
|
|
27
|
+
'median': 'median',
|
|
28
|
+
'p75': 'p75',
|
|
29
|
+
'p95': 'p95',
|
|
30
|
+
'p99': 'p99',
|
|
31
|
+
'p999': 'p999'
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class MetricService {
|
|
35
|
+
constructor () {
|
|
36
|
+
this.metrics = new Map()
|
|
37
|
+
this.timer = null
|
|
38
|
+
this.transport = null
|
|
39
|
+
this.logger = Debug('axm:services:metrics')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
init () {
|
|
43
|
+
this.transport = ServiceManager.get('transport')
|
|
44
|
+
if (this.transport === null) return this.logger('Failed to init metrics service cause no transporter')
|
|
45
|
+
|
|
46
|
+
this.logger('init')
|
|
47
|
+
this.timer = setInterval(() => {
|
|
48
|
+
if (this.transport === null) return this.logger('Abort metrics update since transport is not available')
|
|
49
|
+
this.logger('refreshing metrics value')
|
|
50
|
+
for (let metric of this.metrics.values()) {
|
|
51
|
+
metric.value = metric.handler()
|
|
52
|
+
}
|
|
53
|
+
this.logger('sending update metrics value to transporter')
|
|
54
|
+
const metricsToSend = Array.from(this.metrics.values())
|
|
55
|
+
.filter(metric => {
|
|
56
|
+
if (metric === null || metric === undefined) return false
|
|
57
|
+
if (metric.value === undefined || metric.value === null) return false
|
|
58
|
+
|
|
59
|
+
const isNumber = typeof metric.value === 'number'
|
|
60
|
+
const isString = typeof metric.value === 'string'
|
|
61
|
+
const isBoolean = typeof metric.value === 'boolean'
|
|
62
|
+
const isValidNumber = !isNaN(metric.value)
|
|
63
|
+
return isString || isBoolean || (isNumber && isValidNumber)
|
|
64
|
+
})
|
|
65
|
+
this.transport.setMetrics(metricsToSend)
|
|
66
|
+
}, constants.METRIC_INTERVAL)
|
|
67
|
+
this.timer.unref()
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
registerMetric (metric) {
|
|
71
|
+
if (typeof metric.name !== 'string') {
|
|
72
|
+
console.error(`Invalid metric name declared: ${metric.name}`)
|
|
73
|
+
return console.trace()
|
|
74
|
+
} else if (typeof metric.type !== 'string') {
|
|
75
|
+
console.error(`Invalid metric type declared: ${metric.type}`)
|
|
76
|
+
return console.trace()
|
|
77
|
+
} else if (typeof metric.handler !== 'function') {
|
|
78
|
+
console.error(`Invalid metric handler declared: ${metric.handler}`)
|
|
79
|
+
return console.trace()
|
|
80
|
+
}
|
|
81
|
+
if (typeof metric.historic !== 'boolean') {
|
|
82
|
+
metric.historic = true
|
|
83
|
+
}
|
|
84
|
+
this.logger(`Registering new metric: ${metric.name}`)
|
|
85
|
+
this.metrics.set(metric.name, metric)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
meter (opts) {
|
|
89
|
+
const metric = {
|
|
90
|
+
name: opts.name,
|
|
91
|
+
type: MetricType.meter,
|
|
92
|
+
id: opts.id,
|
|
93
|
+
historic: opts.historic,
|
|
94
|
+
implementation: new Meter(opts),
|
|
95
|
+
unit: opts.unit,
|
|
96
|
+
handler: function () {
|
|
97
|
+
return this.implementation.isUsed() ? this.implementation.val() : NaN
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this.registerMetric(metric)
|
|
101
|
+
|
|
102
|
+
return metric.implementation
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
counter (opts) {
|
|
106
|
+
const metric = {
|
|
107
|
+
name: opts.name,
|
|
108
|
+
type: MetricType.counter,
|
|
109
|
+
id: opts.id,
|
|
110
|
+
historic: opts.historic,
|
|
111
|
+
implementation: new Counter(opts),
|
|
112
|
+
unit: opts.unit,
|
|
113
|
+
handler: function () {
|
|
114
|
+
return this.implementation.isUsed() ? this.implementation.val() : NaN
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
this.registerMetric(metric)
|
|
118
|
+
|
|
119
|
+
return metric.implementation
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
histogram (opts) {
|
|
123
|
+
if (opts.measurement === undefined || opts.measurement === null) {
|
|
124
|
+
opts.measurement = MetricMeasurements.mean
|
|
125
|
+
}
|
|
126
|
+
const metric = {
|
|
127
|
+
name: opts.name,
|
|
128
|
+
type: MetricType.histogram,
|
|
129
|
+
id: opts.id,
|
|
130
|
+
historic: opts.historic,
|
|
131
|
+
implementation: new Histogram(opts),
|
|
132
|
+
unit: opts.unit,
|
|
133
|
+
handler: function () {
|
|
134
|
+
return this.implementation.isUsed()
|
|
135
|
+
? (Math.round(this.implementation.val() * 100) / 100) : NaN
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.registerMetric(metric)
|
|
139
|
+
|
|
140
|
+
return metric.implementation
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
metric (opts) {
|
|
144
|
+
let metric
|
|
145
|
+
if (typeof opts.value === 'function') {
|
|
146
|
+
metric = {
|
|
147
|
+
name: opts.name,
|
|
148
|
+
type: MetricType.gauge,
|
|
149
|
+
id: opts.id,
|
|
150
|
+
implementation: undefined,
|
|
151
|
+
historic: opts.historic,
|
|
152
|
+
unit: opts.unit,
|
|
153
|
+
handler: opts.value
|
|
154
|
+
}
|
|
155
|
+
} else {
|
|
156
|
+
metric = {
|
|
157
|
+
name: opts.name,
|
|
158
|
+
type: MetricType.gauge,
|
|
159
|
+
id: opts.id,
|
|
160
|
+
historic: opts.historic,
|
|
161
|
+
implementation: new Gauge(),
|
|
162
|
+
unit: opts.unit,
|
|
163
|
+
handler: function () {
|
|
164
|
+
return this.implementation.isUsed() ? this.implementation.val() : NaN
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this.registerMetric(metric)
|
|
170
|
+
|
|
171
|
+
return metric.implementation
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
deleteMetric (name) {
|
|
175
|
+
return this.metrics.delete(name)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
destroy () {
|
|
179
|
+
if (this.timer !== null) {
|
|
180
|
+
clearInterval(this.timer)
|
|
181
|
+
}
|
|
182
|
+
this.metrics.clear()
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
module.exports = { MetricType, MetricMeasurements, MetricService }
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const Debug = require('debug')
|
|
4
|
+
const utils = require('../utils/module')
|
|
5
|
+
const EventEmitter = require('events').EventEmitter
|
|
6
|
+
|
|
7
|
+
class RuntimeStatsService extends EventEmitter {
|
|
8
|
+
constructor () {
|
|
9
|
+
super()
|
|
10
|
+
this.logger = Debug('axm:services:runtimeStats')
|
|
11
|
+
this.handle = undefined
|
|
12
|
+
this.noduleInstance = undefined
|
|
13
|
+
this.enabled = false
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
init () {
|
|
17
|
+
this.logger('init')
|
|
18
|
+
if (process.env.PM2_APM_DISABLE_RUNTIME_STATS === 'true') {
|
|
19
|
+
return this.logger('disabling service because of the environment flag')
|
|
20
|
+
}
|
|
21
|
+
const modulePath = utils.detectModule('@pm2/node-runtime-stats')
|
|
22
|
+
if (typeof modulePath !== 'string') return
|
|
23
|
+
const RuntimeStats = utils.loadModule(modulePath)
|
|
24
|
+
if (RuntimeStats instanceof Error) {
|
|
25
|
+
return this.logger(`Failed to require module @pm2/node-runtime-stats: ${RuntimeStats.message}`)
|
|
26
|
+
}
|
|
27
|
+
this.noduleInstance = new RuntimeStats({
|
|
28
|
+
delay: 1000
|
|
29
|
+
})
|
|
30
|
+
this.logger('starting runtime stats')
|
|
31
|
+
this.noduleInstance.start()
|
|
32
|
+
this.handle = (data) => {
|
|
33
|
+
this.logger('received runtime stats', data)
|
|
34
|
+
this.emit('data', data)
|
|
35
|
+
}
|
|
36
|
+
this.noduleInstance.on('sense', this.handle)
|
|
37
|
+
this.enabled = true
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
isEnabled () {
|
|
41
|
+
return this.enabled
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
destroy () {
|
|
45
|
+
if (this.noduleInstance !== undefined && this.noduleInstance !== null) {
|
|
46
|
+
this.logger('removing listener on runtime stats service')
|
|
47
|
+
this.noduleInstance.removeListener('sense', this.handle)
|
|
48
|
+
this.noduleInstance.stop()
|
|
49
|
+
}
|
|
50
|
+
this.logger('destroy')
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
module.exports = { RuntimeStatsService }
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const cluster = require('cluster')
|
|
4
|
+
const Debug = require('debug')
|
|
5
|
+
const EventEmitter = require('events').EventEmitter
|
|
6
|
+
|
|
7
|
+
class IPCTransport extends EventEmitter {
|
|
8
|
+
constructor () {
|
|
9
|
+
super()
|
|
10
|
+
this.initiated = false
|
|
11
|
+
this.logger = Debug('axm:transport:ipc')
|
|
12
|
+
this.onMessage = undefined
|
|
13
|
+
this.autoExitHandle = undefined
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
init (config) {
|
|
17
|
+
this.logger('Init new transport service')
|
|
18
|
+
if (this.initiated === true) {
|
|
19
|
+
console.error('Trying to re-init the transport, please avoid')
|
|
20
|
+
return this
|
|
21
|
+
}
|
|
22
|
+
this.initiated = true
|
|
23
|
+
this.logger('Agent launched')
|
|
24
|
+
this.onMessage = (data) => {
|
|
25
|
+
this.logger('Received reverse message from IPC')
|
|
26
|
+
this.emit('data', data)
|
|
27
|
+
}
|
|
28
|
+
process.on('message', this.onMessage)
|
|
29
|
+
|
|
30
|
+
if (cluster.isWorker === false) {
|
|
31
|
+
this.autoExitHook()
|
|
32
|
+
}
|
|
33
|
+
return this
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
autoExitHook () {
|
|
37
|
+
this.autoExitHandle = setInterval(() => {
|
|
38
|
+
const currentProcess = (cluster.isWorker) ? cluster.worker.process : process
|
|
39
|
+
|
|
40
|
+
if (currentProcess._getActiveHandles().length === 3) {
|
|
41
|
+
const handlers = currentProcess._getActiveHandles().map(h => h.constructor.name)
|
|
42
|
+
|
|
43
|
+
if (handlers.includes('Pipe') === true &&
|
|
44
|
+
handlers.includes('Socket') === true) {
|
|
45
|
+
process.removeListener('message', this.onMessage)
|
|
46
|
+
const tmp = setTimeout(_ => {
|
|
47
|
+
this.logger('Still alive, listen back to IPC')
|
|
48
|
+
process.on('message', this.onMessage)
|
|
49
|
+
}, 200)
|
|
50
|
+
tmp.unref()
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}, 3000)
|
|
54
|
+
|
|
55
|
+
this.autoExitHandle.unref()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
setMetrics (metrics) {
|
|
59
|
+
const serializedMetric = metrics.reduce((object, metric) => {
|
|
60
|
+
if (typeof metric.name !== 'string') return object
|
|
61
|
+
object[metric.name] = {
|
|
62
|
+
historic: metric.historic,
|
|
63
|
+
unit: metric.unit,
|
|
64
|
+
type: metric.id,
|
|
65
|
+
value: metric.value
|
|
66
|
+
}
|
|
67
|
+
return object
|
|
68
|
+
}, {})
|
|
69
|
+
this.send('axm:monitor', serializedMetric)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
addAction (action) {
|
|
73
|
+
this.logger(`Add action: ${action.name}:${action.type}`)
|
|
74
|
+
this.send('axm:action', {
|
|
75
|
+
action_name: action.name,
|
|
76
|
+
action_type: action.type,
|
|
77
|
+
arity: action.arity,
|
|
78
|
+
opts: action.opts
|
|
79
|
+
})
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
setOptions (options) {
|
|
83
|
+
this.logger(`Set options: [${Object.keys(options).join(',')}]`)
|
|
84
|
+
return this.send('axm:option:configuration', options)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
send (channel, payload) {
|
|
88
|
+
if (typeof process.send !== 'function') return -1
|
|
89
|
+
if (process.connected === false) {
|
|
90
|
+
console.error('Process disconnected from parent! (not connected)')
|
|
91
|
+
return process.exit(1)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
this.logger(`Send on channel ${channel}`)
|
|
96
|
+
process.send({ type: channel, data: payload })
|
|
97
|
+
} catch (err) {
|
|
98
|
+
this.logger('Process disconnected from parent !')
|
|
99
|
+
this.logger(err)
|
|
100
|
+
return process.exit(1)
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
destroy () {
|
|
105
|
+
if (this.onMessage !== undefined) {
|
|
106
|
+
process.removeListener('message', this.onMessage)
|
|
107
|
+
}
|
|
108
|
+
if (this.autoExitHandle !== undefined) {
|
|
109
|
+
clearInterval(this.autoExitHandle)
|
|
110
|
+
}
|
|
111
|
+
this.logger('destroy')
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = { IPCTransport }
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
class BinaryHeap {
|
|
4
|
+
constructor (options) {
|
|
5
|
+
options = options || {}
|
|
6
|
+
|
|
7
|
+
this._elements = options.elements || []
|
|
8
|
+
this._score = options.score || this._score
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
add () {
|
|
12
|
+
for (let i = 0; i < arguments.length; i++) {
|
|
13
|
+
const element = arguments[i]
|
|
14
|
+
|
|
15
|
+
this._elements.push(element)
|
|
16
|
+
this._bubble(this._elements.length - 1)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
first () {
|
|
21
|
+
return this._elements[0]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
removeFirst () {
|
|
25
|
+
const root = this._elements[0]
|
|
26
|
+
const last = this._elements.pop()
|
|
27
|
+
|
|
28
|
+
if (this._elements.length > 0) {
|
|
29
|
+
this._elements[0] = last
|
|
30
|
+
this._sink(0)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return root
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
clone () {
|
|
37
|
+
return new BinaryHeap({
|
|
38
|
+
elements: this.toArray(),
|
|
39
|
+
score: this._score
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
toSortedArray () {
|
|
44
|
+
const array = []
|
|
45
|
+
const clone = this.clone()
|
|
46
|
+
|
|
47
|
+
while (true) {
|
|
48
|
+
const element = clone.removeFirst()
|
|
49
|
+
if (element === undefined) break
|
|
50
|
+
|
|
51
|
+
array.push(element)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return array
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
toArray () {
|
|
58
|
+
return [].concat(this._elements)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
size () {
|
|
62
|
+
return this._elements.length
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
_bubble (bubbleIndex) {
|
|
66
|
+
const bubbleElement = this._elements[bubbleIndex]
|
|
67
|
+
const bubbleScore = this._score(bubbleElement)
|
|
68
|
+
|
|
69
|
+
while (bubbleIndex > 0) {
|
|
70
|
+
const parentIndex = this._parentIndex(bubbleIndex)
|
|
71
|
+
const parentElement = this._elements[parentIndex]
|
|
72
|
+
const parentScore = this._score(parentElement)
|
|
73
|
+
|
|
74
|
+
if (bubbleScore <= parentScore) break
|
|
75
|
+
|
|
76
|
+
this._elements[parentIndex] = bubbleElement
|
|
77
|
+
this._elements[bubbleIndex] = parentElement
|
|
78
|
+
bubbleIndex = parentIndex
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
_sink (sinkIndex) {
|
|
83
|
+
const sinkElement = this._elements[sinkIndex]
|
|
84
|
+
const sinkScore = this._score(sinkElement)
|
|
85
|
+
const length = this._elements.length
|
|
86
|
+
|
|
87
|
+
while (true) {
|
|
88
|
+
let swapIndex
|
|
89
|
+
let swapScore
|
|
90
|
+
let swapElement = null
|
|
91
|
+
const childIndexes = this._childIndexes(sinkIndex)
|
|
92
|
+
|
|
93
|
+
for (let i = 0; i < childIndexes.length; i++) {
|
|
94
|
+
const childIndex = childIndexes[i]
|
|
95
|
+
|
|
96
|
+
if (childIndex >= length) break
|
|
97
|
+
|
|
98
|
+
const childElement = this._elements[childIndex]
|
|
99
|
+
const childScore = this._score(childElement)
|
|
100
|
+
|
|
101
|
+
if (childScore > sinkScore) {
|
|
102
|
+
if (swapScore === undefined || swapScore < childScore) {
|
|
103
|
+
swapIndex = childIndex
|
|
104
|
+
swapScore = childScore
|
|
105
|
+
swapElement = childElement
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (swapIndex === undefined) break
|
|
111
|
+
|
|
112
|
+
this._elements[swapIndex] = sinkElement
|
|
113
|
+
this._elements[sinkIndex] = swapElement
|
|
114
|
+
sinkIndex = swapIndex
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
_parentIndex (index) {
|
|
119
|
+
return Math.floor((index - 1) / 2)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
_childIndexes (index) {
|
|
123
|
+
return [
|
|
124
|
+
2 * index + 1,
|
|
125
|
+
2 * index + 2
|
|
126
|
+
]
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
_score (element) {
|
|
130
|
+
return element.valueOf()
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = BinaryHeap
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const BinaryHeap = require('./BinaryHeap')
|
|
4
|
+
const units = require('./units')
|
|
5
|
+
|
|
6
|
+
class ExponentiallyDecayingSample {
|
|
7
|
+
constructor (options) {
|
|
8
|
+
this.RESCALE_INTERVAL = 1 * units.HOURS
|
|
9
|
+
this.ALPHA = 0.015
|
|
10
|
+
this.SIZE = 1028
|
|
11
|
+
|
|
12
|
+
options = options || {}
|
|
13
|
+
|
|
14
|
+
this._elements = new BinaryHeap({
|
|
15
|
+
score: function (element) {
|
|
16
|
+
return -element.priority
|
|
17
|
+
}
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
this._rescaleInterval = options.rescaleInterval || this.RESCALE_INTERVAL
|
|
21
|
+
this._alpha = options.alpha || this.ALPHA
|
|
22
|
+
this._size = options.size || this.SIZE
|
|
23
|
+
this._random = options.random || this._random
|
|
24
|
+
this._landmark = null
|
|
25
|
+
this._nextRescale = null
|
|
26
|
+
this._mean = null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
update (value, timestamp) {
|
|
30
|
+
const now = Date.now()
|
|
31
|
+
if (!this._landmark) {
|
|
32
|
+
this._landmark = now
|
|
33
|
+
this._nextRescale = this._landmark + this._rescaleInterval
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
timestamp = timestamp || now
|
|
37
|
+
|
|
38
|
+
const newSize = this._elements.size() + 1
|
|
39
|
+
|
|
40
|
+
const element = {
|
|
41
|
+
priority: this._priority(timestamp - this._landmark),
|
|
42
|
+
value: value
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (newSize <= this._size) {
|
|
46
|
+
this._elements.add(element)
|
|
47
|
+
} else if (element.priority > this._elements.first().priority) {
|
|
48
|
+
this._elements.removeFirst()
|
|
49
|
+
this._elements.add(element)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (now >= this._nextRescale) this._rescale(now)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
toSortedArray () {
|
|
56
|
+
return this._elements
|
|
57
|
+
.toSortedArray()
|
|
58
|
+
.map(function (element) {
|
|
59
|
+
return element.value
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
toArray () {
|
|
64
|
+
return this._elements
|
|
65
|
+
.toArray()
|
|
66
|
+
.map(function (element) {
|
|
67
|
+
return element.value
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
_weight (age) {
|
|
72
|
+
// We divide by 1000 to not run into huge numbers before reaching a
|
|
73
|
+
// rescale event.
|
|
74
|
+
return Math.exp(this._alpha * (age / 1000))
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_priority (age) {
|
|
78
|
+
return this._weight(age) / this._random()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
_random () {
|
|
82
|
+
return Math.random()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
_rescale (now) {
|
|
86
|
+
now = now || Date.now()
|
|
87
|
+
|
|
88
|
+
const self = this
|
|
89
|
+
const oldLandmark = this._landmark
|
|
90
|
+
this._landmark = now || Date.now()
|
|
91
|
+
this._nextRescale = now + this._rescaleInterval
|
|
92
|
+
|
|
93
|
+
const factor = self._priority(-(self._landmark - oldLandmark))
|
|
94
|
+
|
|
95
|
+
this._elements
|
|
96
|
+
.toArray()
|
|
97
|
+
.forEach(function (element) {
|
|
98
|
+
element.priority *= factor
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
avg (now) {
|
|
103
|
+
let sum = 0
|
|
104
|
+
this._elements
|
|
105
|
+
.toArray()
|
|
106
|
+
.forEach(function (element) {
|
|
107
|
+
sum += element.value
|
|
108
|
+
})
|
|
109
|
+
return (sum / this._elements.size())
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = ExponentiallyDecayingSample
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const units = require('./units')
|
|
4
|
+
|
|
5
|
+
class ExponentiallyWeightedMovingAverage {
|
|
6
|
+
constructor (timePeriod, tickInterval) {
|
|
7
|
+
this.TICK_INTERVAL = 5 * units.SECONDS
|
|
8
|
+
this._count = 0
|
|
9
|
+
this._rate = 0
|
|
10
|
+
|
|
11
|
+
this._timePeriod = timePeriod || 1 * units.MINUTES
|
|
12
|
+
this._tickInterval = tickInterval || this.TICK_INTERVAL
|
|
13
|
+
this._alpha = 1 - Math.exp(-this._tickInterval / this._timePeriod)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
update (n) {
|
|
17
|
+
this._count += n
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
tick () {
|
|
21
|
+
const instantRate = this._count / this._tickInterval
|
|
22
|
+
this._count = 0
|
|
23
|
+
|
|
24
|
+
this._rate += (this._alpha * (instantRate - this._rate))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
rate (timeUnit) {
|
|
28
|
+
return (this._rate || 0) * timeUnit
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = ExponentiallyWeightedMovingAverage
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
class Autocast {
|
|
4
|
+
constructor () {
|
|
5
|
+
this.commonStrings = {
|
|
6
|
+
'true': true,
|
|
7
|
+
'false': false,
|
|
8
|
+
'undefined': undefined,
|
|
9
|
+
'null': null,
|
|
10
|
+
'NaN': NaN
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
process (key, value, o) {
|
|
15
|
+
if (typeof(value) === 'object') return
|
|
16
|
+
o[key] = this._cast(value)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
traverse (o, func) {
|
|
20
|
+
for (let i in o) {
|
|
21
|
+
func.apply(this, [i, o[i], o])
|
|
22
|
+
if (o[i] !== null && typeof(o[i]) === 'object') {
|
|
23
|
+
// going on step down in the object tree!!
|
|
24
|
+
this.traverse(o[i], func)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Given a value, try and cast it
|
|
31
|
+
*/
|
|
32
|
+
autocast (s) {
|
|
33
|
+
if (typeof(s) === 'object') {
|
|
34
|
+
this.traverse(s, this.process)
|
|
35
|
+
return s
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return this._cast(s)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_cast (s) {
|
|
42
|
+
let key
|
|
43
|
+
|
|
44
|
+
// Don't cast Date objects
|
|
45
|
+
if (s instanceof Date) return s
|
|
46
|
+
if (typeof s === 'boolean') return s
|
|
47
|
+
|
|
48
|
+
// Try to cast it to a number
|
|
49
|
+
if (!isNaN(s)) return Number(s)
|
|
50
|
+
|
|
51
|
+
// Try to make it a common string
|
|
52
|
+
for (key in this.commonStrings) {
|
|
53
|
+
if (s === key) return this.commonStrings[key]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Give up
|
|
57
|
+
return s
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = Autocast
|