@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.
Files changed (267) hide show
  1. package/CHANGELOG.md +2607 -0
  2. package/CONTRIBUTING.md +124 -0
  3. package/GNU-AGPL-3.0.txt +665 -0
  4. package/LICENSE +1 -0
  5. package/README.md +255 -2
  6. package/SECURITY.md +41 -0
  7. package/bin/pm2 +3 -0
  8. package/bin/pm2-dev +3 -0
  9. package/bin/pm2-docker +3 -0
  10. package/bin/pm2-runtime +3 -0
  11. package/bin/pm2.ps1 +3 -0
  12. package/constants.js +113 -0
  13. package/index.js +12 -0
  14. package/lib/API/Configuration.js +212 -0
  15. package/lib/API/Containerizer.js +335 -0
  16. package/lib/API/Dashboard.js +726 -0
  17. package/lib/API/Deploy.js +117 -0
  18. package/lib/API/Extra.js +798 -0
  19. package/lib/API/ExtraMgmt/Docker.js +30 -0
  20. package/lib/API/Log.js +388 -0
  21. package/lib/API/LogManagement.js +351 -0
  22. package/lib/API/Modules/LOCAL.js +122 -0
  23. package/lib/API/Modules/Modularizer.js +148 -0
  24. package/lib/API/Modules/NPM.js +443 -0
  25. package/lib/API/Modules/TAR.js +368 -0
  26. package/lib/API/Modules/flagExt.js +46 -0
  27. package/lib/API/Modules/index.js +120 -0
  28. package/lib/API/Monit.js +247 -0
  29. package/lib/API/Serve.js +497 -0
  30. package/lib/API/Startup.js +630 -0
  31. package/lib/API/UX/helpers.js +213 -0
  32. package/lib/API/UX/index.js +9 -0
  33. package/lib/API/UX/pm2-describe.js +197 -0
  34. package/lib/API/UX/pm2-ls-minimal.js +31 -0
  35. package/lib/API/UX/pm2-ls.js +581 -0
  36. package/lib/API/Version.js +383 -0
  37. package/lib/API/interpreter.json +12 -0
  38. package/lib/API/pm2-plus/PM2IO.js +381 -0
  39. package/lib/API/pm2-plus/auth-strategies/CliAuth.js +306 -0
  40. package/lib/API/pm2-plus/auth-strategies/WebAuth.js +195 -0
  41. package/lib/API/pm2-plus/helpers.js +97 -0
  42. package/lib/API/pm2-plus/link.js +126 -0
  43. package/lib/API/pm2-plus/pres/motd +16 -0
  44. package/lib/API/pm2-plus/pres/motd.update +26 -0
  45. package/lib/API/pm2-plus/pres/welcome +28 -0
  46. package/lib/API/pm2-plus/process-selector.js +52 -0
  47. package/lib/API/schema.json +375 -0
  48. package/lib/API.js +1980 -0
  49. package/lib/Client.js +793 -0
  50. package/lib/Common.js +949 -0
  51. package/lib/Configuration.js +309 -0
  52. package/lib/Daemon.js +468 -0
  53. package/lib/Event.js +37 -0
  54. package/lib/God/ActionMethods.js +955 -0
  55. package/lib/God/ClusterMode.js +192 -0
  56. package/lib/God/ForkMode.js +205 -0
  57. package/lib/God/Methods.js +292 -0
  58. package/lib/God/Reload.js +300 -0
  59. package/lib/God/StdioLogger.js +239 -0
  60. package/lib/God.js +653 -0
  61. package/lib/HttpInterface.js +75 -0
  62. package/lib/OtelManager.js +74 -0
  63. package/lib/ProcessContainer.js +321 -0
  64. package/lib/ProcessContainerBun.js +355 -0
  65. package/lib/ProcessContainerFork.js +42 -0
  66. package/lib/ProcessContainerForkBun.js +31 -0
  67. package/lib/ProcessUtils.js +74 -0
  68. package/lib/TreeKill.js +111 -0
  69. package/lib/Utility.js +304 -0
  70. package/lib/VersionCheck.js +46 -0
  71. package/lib/Watcher.js +117 -0
  72. package/lib/Worker.js +200 -0
  73. package/lib/binaries/CLI.js +1092 -0
  74. package/lib/binaries/DevCLI.js +183 -0
  75. package/lib/binaries/Runtime.js +101 -0
  76. package/lib/binaries/Runtime4Docker.js +192 -0
  77. package/lib/completion.js +229 -0
  78. package/lib/completion.sh +40 -0
  79. package/lib/motd +36 -0
  80. package/lib/templates/Dockerfiles/Dockerfile-nodejs.tpl +8 -0
  81. package/lib/templates/ecosystem-es.tpl +24 -0
  82. package/lib/templates/ecosystem-simple-es.tpl +8 -0
  83. package/lib/templates/ecosystem-simple.tpl +6 -0
  84. package/lib/templates/ecosystem.tpl +22 -0
  85. package/lib/templates/init-scripts/launchd.tpl +35 -0
  86. package/lib/templates/init-scripts/openrc.tpl +52 -0
  87. package/lib/templates/init-scripts/pm2-init-amazon.sh +86 -0
  88. package/lib/templates/init-scripts/rcd-openbsd.tpl +41 -0
  89. package/lib/templates/init-scripts/rcd.tpl +44 -0
  90. package/lib/templates/init-scripts/smf.tpl +43 -0
  91. package/lib/templates/init-scripts/systemd-online.tpl +22 -0
  92. package/lib/templates/init-scripts/systemd.tpl +22 -0
  93. package/lib/templates/init-scripts/upstart.tpl +103 -0
  94. package/lib/templates/logrotate.d/pm2 +10 -0
  95. package/lib/templates/sample-apps/http-server/README.md +14 -0
  96. package/lib/templates/sample-apps/http-server/api.js +9 -0
  97. package/lib/templates/sample-apps/http-server/ecosystem.config.js +14 -0
  98. package/lib/templates/sample-apps/http-server/package.json +11 -0
  99. package/lib/templates/sample-apps/pm2-plus-metrics-actions/README.md +45 -0
  100. package/lib/templates/sample-apps/pm2-plus-metrics-actions/custom-metrics.js +66 -0
  101. package/lib/templates/sample-apps/pm2-plus-metrics-actions/ecosystem.config.js +12 -0
  102. package/lib/templates/sample-apps/pm2-plus-metrics-actions/package.json +11 -0
  103. package/lib/templates/sample-apps/python-app/README.md +4 -0
  104. package/lib/templates/sample-apps/python-app/echo.py +7 -0
  105. package/lib/templates/sample-apps/python-app/ecosystem.config.js +12 -0
  106. package/lib/templates/sample-apps/python-app/package.json +11 -0
  107. package/lib/tools/Config.js +251 -0
  108. package/lib/tools/SysMetrics.js +375 -0
  109. package/lib/tools/copydirSync.js +101 -0
  110. package/lib/tools/deleteFolderRecursive.js +19 -0
  111. package/lib/tools/find-package-json.js +74 -0
  112. package/lib/tools/fmt.js +72 -0
  113. package/lib/tools/interpreter.js +88 -0
  114. package/lib/tools/isbinaryfile.js +94 -0
  115. package/lib/tools/json5.js +752 -0
  116. package/lib/tools/multimeter/bar.js +111 -0
  117. package/lib/tools/multimeter/charm/encode.js +18 -0
  118. package/lib/tools/multimeter/charm/index.js +305 -0
  119. package/lib/tools/multimeter/index.js +76 -0
  120. package/lib/tools/open.js +56 -0
  121. package/lib/tools/passwd.js +58 -0
  122. package/lib/tools/pm2-config-keys.js +165 -0
  123. package/lib/tools/prompt.js +76 -0
  124. package/lib/tools/serviceStop.js +149 -0
  125. package/lib/tools/sexec.js +55 -0
  126. package/lib/tools/treeify.js +113 -0
  127. package/lib/tools/typestrip.js +55 -0
  128. package/lib/tools/which.js +120 -0
  129. package/lib/tools/xdg-open +861 -0
  130. package/modules/blessed/LICENSE +20 -0
  131. package/modules/blessed/README.md +34 -0
  132. package/modules/blessed/index.js +1 -0
  133. package/modules/blessed/lib/alias.js +526 -0
  134. package/modules/blessed/lib/blessed.js +32 -0
  135. package/modules/blessed/lib/colors.js +530 -0
  136. package/modules/blessed/lib/events.js +189 -0
  137. package/modules/blessed/lib/gpmclient.js +221 -0
  138. package/modules/blessed/lib/helpers.js +165 -0
  139. package/modules/blessed/lib/keys.js +339 -0
  140. package/modules/blessed/lib/program.js +4297 -0
  141. package/modules/blessed/lib/tput.js +3022 -0
  142. package/modules/blessed/lib/unicode.js +885 -0
  143. package/modules/blessed/lib/widget.js +32 -0
  144. package/modules/blessed/lib/widgets/box.js +34 -0
  145. package/modules/blessed/lib/widgets/element.js +2570 -0
  146. package/modules/blessed/lib/widgets/list.js +599 -0
  147. package/modules/blessed/lib/widgets/log.js +83 -0
  148. package/modules/blessed/lib/widgets/node.js +282 -0
  149. package/modules/blessed/lib/widgets/screen.js +2298 -0
  150. package/modules/blessed/lib/widgets/scrollablebox.js +389 -0
  151. package/modules/blessed/lib/widgets/scrollabletext.js +35 -0
  152. package/modules/blessed/lib/widgets/text.js +35 -0
  153. package/modules/blessed/usr/linux +0 -0
  154. package/modules/blessed/usr/windows-ansi +0 -0
  155. package/modules/blessed/usr/xterm +0 -0
  156. package/modules/blessed/usr/xterm-256color +0 -0
  157. package/modules/blessed/usr/xterm.termcap +243 -0
  158. package/modules/chokidar/LICENSE +21 -0
  159. package/modules/chokidar/VENDORED.md +76 -0
  160. package/modules/chokidar/index.js +979 -0
  161. package/modules/chokidar/lib/brace-expand.js +180 -0
  162. package/modules/chokidar/lib/constants.js +66 -0
  163. package/modules/chokidar/lib/fsevents-handler.js +526 -0
  164. package/modules/chokidar/lib/nodefs-handler.js +654 -0
  165. package/modules/fclone.js +49 -0
  166. package/modules/pm2-axon/index.js +2 -0
  167. package/modules/pm2-axon/lib/configurable/History.md +5 -0
  168. package/modules/pm2-axon/lib/configurable/Makefile +7 -0
  169. package/modules/pm2-axon/lib/configurable/Readme.md +57 -0
  170. package/modules/pm2-axon/lib/configurable/index.js +2 -0
  171. package/modules/pm2-axon/lib/configurable/lib/configurable.js +47 -0
  172. package/modules/pm2-axon/lib/configurable/package.json +68 -0
  173. package/modules/pm2-axon/lib/index.js +44 -0
  174. package/modules/pm2-axon/lib/plugins/queue.js +71 -0
  175. package/modules/pm2-axon/lib/plugins/round-robin.js +55 -0
  176. package/modules/pm2-axon/lib/sockets/pub-emitter.js +26 -0
  177. package/modules/pm2-axon/lib/sockets/pub.js +77 -0
  178. package/modules/pm2-axon/lib/sockets/pull.js +37 -0
  179. package/modules/pm2-axon/lib/sockets/push.js +32 -0
  180. package/modules/pm2-axon/lib/sockets/rep.js +73 -0
  181. package/modules/pm2-axon/lib/sockets/req.js +101 -0
  182. package/modules/pm2-axon/lib/sockets/sock.js +430 -0
  183. package/modules/pm2-axon/lib/sockets/sub-emitter.js +92 -0
  184. package/modules/pm2-axon/lib/sockets/sub.js +147 -0
  185. package/modules/pm2-axon/lib/utils.js +19 -0
  186. package/modules/pm2-axon-rpc/index.js +3 -0
  187. package/modules/pm2-axon-rpc/lib/client.js +63 -0
  188. package/modules/pm2-axon-rpc/lib/server.js +139 -0
  189. package/modules/pm2-io-agent/LICENSE +661 -0
  190. package/modules/pm2-io-agent/README.md +62 -0
  191. package/modules/pm2-io-agent/config.js +30 -0
  192. package/modules/pm2-io-agent/constants.js +118 -0
  193. package/modules/pm2-io-agent/index.js +2 -0
  194. package/modules/pm2-io-agent/src/InteractorClient.js +513 -0
  195. package/modules/pm2-io-agent/src/InteractorDaemon.js +455 -0
  196. package/modules/pm2-io-agent/src/PM2Client.js +112 -0
  197. package/modules/pm2-io-agent/src/PM2Interface.js +180 -0
  198. package/modules/pm2-io-agent/src/TransporterInterface.js +173 -0
  199. package/modules/pm2-io-agent/src/Utility.js +392 -0
  200. package/modules/pm2-io-agent/src/WatchDog.js +70 -0
  201. package/modules/pm2-io-agent/src/push/DataRetriever.js +77 -0
  202. package/modules/pm2-io-agent/src/push/PushInteractor.js +206 -0
  203. package/modules/pm2-io-agent/src/push/TransactionAggregator.js +670 -0
  204. package/modules/pm2-io-agent/src/reverse/ReverseInteractor.js +128 -0
  205. package/modules/pm2-io-agent/src/transporters/Transporter.js +129 -0
  206. package/modules/pm2-io-agent/src/transporters/WebsocketTransport.js +202 -0
  207. package/modules/pm2-io-agent/src/utils/BinaryHeap.js +135 -0
  208. package/modules/pm2-io-agent/src/utils/EDS.js +111 -0
  209. package/modules/pm2-io-agent/src/utils/probes/Histogram.js +204 -0
  210. package/modules/pm2-io-agent/src/utils/units.js +10 -0
  211. package/modules/pm2-io-bpm/configuration.js +141 -0
  212. package/modules/pm2-io-bpm/constants.js +10 -0
  213. package/modules/pm2-io-bpm/featureManager.js +110 -0
  214. package/modules/pm2-io-bpm/features/dependencies.js +48 -0
  215. package/modules/pm2-io-bpm/features/events.js +50 -0
  216. package/modules/pm2-io-bpm/features/metrics.js +102 -0
  217. package/modules/pm2-io-bpm/features/notify.js +238 -0
  218. package/modules/pm2-io-bpm/features/profiling.js +70 -0
  219. package/modules/pm2-io-bpm/features/tracing.js +174 -0
  220. package/modules/pm2-io-bpm/index.js +11 -0
  221. package/modules/pm2-io-bpm/metrics/eventLoopMetrics.js +136 -0
  222. package/modules/pm2-io-bpm/metrics/httpMetrics.js +175 -0
  223. package/modules/pm2-io-bpm/metrics/network.js +132 -0
  224. package/modules/pm2-io-bpm/metrics/runtime.js +161 -0
  225. package/modules/pm2-io-bpm/metrics/v8.js +104 -0
  226. package/modules/pm2-io-bpm/otel/constants.js +7 -0
  227. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/index.js +17 -0
  228. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/index.js +17 -0
  229. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/platform/node/util.js +51 -0
  230. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/transform.js +137 -0
  231. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/types.js +24 -0
  232. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/utils.js +23 -0
  233. package/modules/pm2-io-bpm/otel/custom-zipkin-exporter/zipkin.js +149 -0
  234. package/modules/pm2-io-bpm/pmx.js +380 -0
  235. package/modules/pm2-io-bpm/profilers/addonProfiler.js +193 -0
  236. package/modules/pm2-io-bpm/profilers/inspectorProfiler.js +302 -0
  237. package/modules/pm2-io-bpm/serviceManager.js +21 -0
  238. package/modules/pm2-io-bpm/services/actions.js +159 -0
  239. package/modules/pm2-io-bpm/services/inspector.js +43 -0
  240. package/modules/pm2-io-bpm/services/metrics.js +186 -0
  241. package/modules/pm2-io-bpm/services/runtimeStats.js +54 -0
  242. package/modules/pm2-io-bpm/services/transport.js +11 -0
  243. package/modules/pm2-io-bpm/transports/IPCTransport.js +115 -0
  244. package/modules/pm2-io-bpm/utils/BinaryHeap.js +134 -0
  245. package/modules/pm2-io-bpm/utils/EDS.js +113 -0
  246. package/modules/pm2-io-bpm/utils/EWMA.js +32 -0
  247. package/modules/pm2-io-bpm/utils/autocast.js +61 -0
  248. package/modules/pm2-io-bpm/utils/metrics/counter.js +33 -0
  249. package/modules/pm2-io-bpm/utils/metrics/gauge.js +23 -0
  250. package/modules/pm2-io-bpm/utils/metrics/histogram.js +192 -0
  251. package/modules/pm2-io-bpm/utils/metrics/meter.js +46 -0
  252. package/modules/pm2-io-bpm/utils/miscellaneous.js +19 -0
  253. package/modules/pm2-io-bpm/utils/module.js +67 -0
  254. package/modules/pm2-io-bpm/utils/shimmer.js +19 -0
  255. package/modules/pm2-io-bpm/utils/stackParser.js +160 -0
  256. package/modules/pm2-io-bpm/utils/transactionAggregator.js +429 -0
  257. package/modules/pm2-io-bpm/utils/units.js +16 -0
  258. package/modules/vizion/LICENSE +13 -0
  259. package/modules/vizion/README.md +133 -0
  260. package/modules/vizion/index.js +1 -0
  261. package/modules/vizion/lib/git/git.js +247 -0
  262. package/modules/vizion/lib/vizion.js +52 -0
  263. package/package.json +170 -4
  264. package/paths.js +121 -0
  265. package/pm2 +11 -0
  266. package/types/index.d.ts +732 -0
  267. package/types/tsconfig.json +14 -0
@@ -0,0 +1,238 @@
1
+ 'use strict'
2
+
3
+ const Configuration = require('../configuration')
4
+ const { ServiceManager } = require('../serviceManager')
5
+ const Debug = require('debug')
6
+ const { Cache, StackTraceParser } = require('../utils/stackParser')
7
+ const fs = require('fs')
8
+ const path = require('path')
9
+
10
+ const optionsDefault = {
11
+ catchExceptions: true
12
+ }
13
+
14
+ class NotifyFeature {
15
+ constructor () {
16
+ this.logger = Debug('axm:features:notify')
17
+ this.transport = undefined
18
+ this.cache = null
19
+ this.stackParser = null
20
+ }
21
+
22
+ init (options) {
23
+ if (options === undefined) {
24
+ options = optionsDefault
25
+ }
26
+ this.logger('init')
27
+ this.transport = ServiceManager.get('transport')
28
+ if (this.transport === undefined) {
29
+ return this.logger('Failed to load transporter service')
30
+ }
31
+
32
+ Configuration.configureModule({
33
+ error: true
34
+ })
35
+ if (options.catchExceptions === false) return
36
+ this.logger('Registering hook to catch unhandled exception/rejection')
37
+ this.cache = new Cache({
38
+ miss: (key) => {
39
+ try {
40
+ const content = fs.readFileSync(path.resolve(key))
41
+ return content.toString().split(/\r?\n/)
42
+ } catch (err) {
43
+ this.logger('Error while trying to get file from FS : %s', err.message || err)
44
+ return null
45
+ }
46
+ },
47
+ ttl: 30 * 60
48
+ })
49
+ this.stackParser = new StackTraceParser({
50
+ cache: this.cache,
51
+ contextSize: 5
52
+ })
53
+ this.catchAll()
54
+ }
55
+
56
+ destroy () {
57
+ if (this._onUncaughtException) {
58
+ process.removeListener('uncaughtException', this._onUncaughtException)
59
+ }
60
+ if (this._onUnhandledRejection) {
61
+ process.removeListener('unhandledRejection', this._onUnhandledRejection)
62
+ }
63
+ this.logger('destroy')
64
+ }
65
+
66
+ getSafeError (err) {
67
+ if (err instanceof Error) return err
68
+
69
+ let message
70
+ try {
71
+ message = `Non-error value: ${JSON.stringify(err)}`
72
+ } catch (e) {
73
+ try {
74
+ message = `Unserializable non-error value: ${String(e)}`
75
+ } catch (e2) {
76
+ message = 'Unserializable non-error value that cannot be converted to a string'
77
+ }
78
+ }
79
+ if (message.length > 1000) message = message.substr(0, 1000) + '...'
80
+
81
+ return new Error(message)
82
+ }
83
+
84
+ notifyError (err, context) {
85
+ if (typeof context !== 'object') {
86
+ context = {}
87
+ }
88
+
89
+ if (this.transport === undefined) {
90
+ return this.logger('Tried to send error without having transporter available')
91
+ }
92
+
93
+ const safeError = this.getSafeError(err)
94
+ let stackContext = null
95
+ if (err instanceof Error) {
96
+ stackContext = this.stackParser.retrieveContext(err)
97
+ }
98
+
99
+ const payload = Object.assign({
100
+ message: safeError.message,
101
+ stack: safeError.stack,
102
+ name: safeError.name,
103
+ metadata: context
104
+ }, stackContext === null ? {} : stackContext)
105
+
106
+ return this.transport.send('process:exception', payload)
107
+ }
108
+
109
+ onUncaughtException (error) {
110
+ // Node 18+ always supports console.error(error) directly
111
+ console.error(error)
112
+
113
+ const safeError = this.getSafeError(error)
114
+ let stackContext = null
115
+ if (error instanceof Error) {
116
+ stackContext = this.stackParser.retrieveContext(error)
117
+ }
118
+
119
+ const payload = Object.assign({
120
+ message: safeError.message,
121
+ stack: safeError.stack,
122
+ name: safeError.name
123
+ }, stackContext === null ? {} : stackContext)
124
+
125
+ if (ServiceManager.get('transport')) {
126
+ ServiceManager.get('transport').send('process:exception', payload)
127
+ }
128
+ if (process.listeners('uncaughtException').length === 1) {
129
+ process.exit(1)
130
+ }
131
+ }
132
+
133
+ onUnhandledRejection (error) {
134
+ if (error === undefined) return
135
+
136
+ console.error(error)
137
+
138
+ const safeError = this.getSafeError(error)
139
+ let stackContext = null
140
+ if (error instanceof Error) {
141
+ stackContext = this.stackParser.retrieveContext(error)
142
+ }
143
+
144
+ const payload = Object.assign({
145
+ message: safeError.message,
146
+ stack: safeError.stack,
147
+ name: safeError.name
148
+ }, stackContext === null ? {} : stackContext)
149
+
150
+ if (ServiceManager.get('transport')) {
151
+ ServiceManager.get('transport').send('process:exception', payload)
152
+ }
153
+ }
154
+
155
+ catchAll () {
156
+ if (process.env.exec_mode === 'cluster_mode') {
157
+ return false
158
+ }
159
+
160
+ this._onUncaughtException = this.onUncaughtException.bind(this)
161
+ this._onUnhandledRejection = this.onUnhandledRejection.bind(this)
162
+ process.on('uncaughtException', this._onUncaughtException)
163
+ process.on('unhandledRejection', this._onUnhandledRejection)
164
+ }
165
+
166
+ expressErrorHandler () {
167
+ const self = this
168
+ Configuration.configureModule({
169
+ error: true
170
+ })
171
+ return function errorHandler (err, req, res, next) {
172
+ const safeError = self.getSafeError(err)
173
+ const payload = {
174
+ message: safeError.message,
175
+ stack: safeError.stack,
176
+ name: safeError.name,
177
+ metadata: {
178
+ http: {
179
+ url: req.url,
180
+ params: req.params,
181
+ method: req.method,
182
+ query: req.query,
183
+ body: req.body,
184
+ path: req.path,
185
+ route: req.route && req.route.path ? req.route.path : undefined
186
+ },
187
+ custom: {
188
+ user: typeof req.user === 'object' ? req.user.id : undefined
189
+ }
190
+ }
191
+ }
192
+
193
+ if (ServiceManager.get('transport')) {
194
+ ServiceManager.get('transport').send('process:exception', payload)
195
+ }
196
+ return next(err)
197
+ }
198
+ }
199
+
200
+ koaErrorHandler () {
201
+ const self = this
202
+ Configuration.configureModule({
203
+ error: true
204
+ })
205
+ return async function (ctx, next) {
206
+ try {
207
+ await next()
208
+ } catch (err) {
209
+ const safeError = self.getSafeError(err)
210
+ const payload = {
211
+ message: safeError.message,
212
+ stack: safeError.stack,
213
+ name: safeError.name,
214
+ metadata: {
215
+ http: {
216
+ url: ctx.request.url,
217
+ params: ctx.params,
218
+ method: ctx.request.method,
219
+ query: ctx.request.query,
220
+ body: ctx.request.body,
221
+ path: ctx.request.path,
222
+ route: ctx._matchedRoute
223
+ },
224
+ custom: {
225
+ user: typeof ctx.user === 'object' ? ctx.user.id : undefined
226
+ }
227
+ }
228
+ }
229
+ if (ServiceManager.get('transport')) {
230
+ ServiceManager.get('transport').send('process:exception', payload)
231
+ }
232
+ throw err
233
+ }
234
+ }
235
+ }
236
+ }
237
+
238
+ module.exports = { NotifyFeature }
@@ -0,0 +1,70 @@
1
+ 'use strict'
2
+
3
+ const AddonProfiler = require('../profilers/addonProfiler')
4
+ const InspectorProfiler = require('../profilers/inspectorProfiler')
5
+ const { canUseInspector } = require('../constants')
6
+ const Debug = require('debug')
7
+
8
+ const defaultProfilingConfig = {
9
+ cpuJS: true,
10
+ heapSnapshot: true,
11
+ heapSampling: true,
12
+ implementation: 'both'
13
+ }
14
+
15
+ const disabledProfilingConfig = {
16
+ cpuJS: false,
17
+ heapSnapshot: false,
18
+ heapSampling: false,
19
+ implementation: 'none'
20
+ }
21
+
22
+ class ProfilingFeature {
23
+ constructor () {
24
+ this.profiler = undefined
25
+ this.logger = Debug('axm:features:profiling')
26
+ }
27
+
28
+ init (config) {
29
+ if (config === true) {
30
+ config = defaultProfilingConfig
31
+ } else if (config === false) {
32
+ config = disabledProfilingConfig
33
+ } else if (config === undefined) {
34
+ config = defaultProfilingConfig
35
+ }
36
+
37
+ if (process.env.PM2_PROFILING_FORCE_FALLBACK === 'true') {
38
+ config.implementation = 'addon'
39
+ }
40
+ if (config.implementation === undefined || config.implementation === 'both') {
41
+ config.implementation = canUseInspector() === true ? 'inspector' : 'addon'
42
+ }
43
+
44
+ switch (config.implementation) {
45
+ case 'inspector': {
46
+ this.logger('using inspector implementation')
47
+ this.profiler = new InspectorProfiler()
48
+ break
49
+ }
50
+ case 'addon': {
51
+ this.logger('using addon implementation')
52
+ this.profiler = new AddonProfiler()
53
+ break
54
+ }
55
+ default: {
56
+ return this.logger(`Invalid profiler implementation choosen: ${config.implementation}`)
57
+ }
58
+ }
59
+ this.logger('init')
60
+ this.profiler.init()
61
+ }
62
+
63
+ destroy () {
64
+ this.logger('destroy')
65
+ if (this.profiler === undefined) return
66
+ this.profiler.destroy()
67
+ }
68
+ }
69
+
70
+ module.exports = { ProfilingFeature }
@@ -0,0 +1,174 @@
1
+ 'use strict'
2
+
3
+ const Debug = require('debug')
4
+ const Configuration = require('../configuration')
5
+
6
+ const httpMethodToIgnore = [
7
+ 'options',
8
+ 'head'
9
+ ]
10
+
11
+ const defaultTracingConfig = {
12
+ enabled: true,
13
+ outbound: false,
14
+ samplingRate: 0,
15
+ ignoreIncomingPaths: [],
16
+ ignoreOutgoingUrls: [],
17
+ createSpanWithNet: false
18
+ }
19
+
20
+ const enabledTracingConfig = {
21
+ enabled: true,
22
+ outbound: true,
23
+ samplingRate: 0.5,
24
+ ignoreIncomingPaths: [
25
+ (_url, request) => {
26
+ const method = (request.method || 'GET').toLowerCase()
27
+ return httpMethodToIgnore.indexOf(method) > -1
28
+ },
29
+ /(.*).js/,
30
+ /(.*).css/,
31
+ /(.*).png/,
32
+ /(.*).ico/,
33
+ /(.*).svg/,
34
+ /webpack/
35
+ ],
36
+ ignoreOutgoingUrls: [],
37
+ createSpanWithNet: false
38
+ }
39
+
40
+ class TracingFeature {
41
+ constructor () {
42
+ this.options = null
43
+ this.logger = Debug('axm:tracing')
44
+ this.otel = undefined
45
+ }
46
+
47
+ init (config) {
48
+ this.logger('init tracing', config)
49
+
50
+ if (config.tracing === undefined) {
51
+ config.tracing = defaultTracingConfig
52
+ } else if (config.tracing === true) {
53
+ config.tracing = enabledTracingConfig
54
+ } else if (config.tracing === false) {
55
+ config.tracing = defaultTracingConfig
56
+ }
57
+
58
+ if (config.tracing.enabled === false) {
59
+ this.logger('tracing disabled')
60
+ return
61
+ } else {
62
+ this.logger('tracing enabled')
63
+ }
64
+
65
+ this.options = enabledTracingConfig
66
+
67
+ if (typeof config.apmOptions === 'object' && typeof config.apmOptions.appName === 'string') {
68
+ this.options.serviceName = config.apmOptions.appName
69
+ } else if (typeof process.env.name === 'string') {
70
+ this.options.serviceName = process.env.name
71
+ }
72
+
73
+ if (config.tracing.ignoreOutgoingUrls === undefined) {
74
+ config.tracing.ignoreOutgoingUrls = enabledTracingConfig.ignoreOutgoingUrls
75
+ }
76
+
77
+ if (config.tracing.ignoreIncomingPaths === undefined) {
78
+ config.tracing.ignoreIncomingPaths = enabledTracingConfig.ignoreIncomingPaths
79
+ }
80
+
81
+ let NodeSDK, getNodeAutoInstrumentations, CustomZipkinExporter
82
+ try {
83
+ NodeSDK = require('@opentelemetry/sdk-node').NodeSDK
84
+ getNodeAutoInstrumentations = require('@opentelemetry/auto-instrumentations-node').getNodeAutoInstrumentations
85
+ CustomZipkinExporter = require('../otel/custom-zipkin-exporter/zipkin').CustomZipkinExporter
86
+ } catch(e) {
87
+ console.error('[PM2][ERROR] OpenTelemetry packages not installed. Tracing disabled.')
88
+ console.error('[PM2][ERROR] To enable tracing, run: pm2 install-otel')
89
+ return
90
+ }
91
+
92
+ const traceExporter = new CustomZipkinExporter()
93
+
94
+ const serviceName =
95
+ process.env.OTEL_SERVICE_NAME ||
96
+ this.options.serviceName
97
+
98
+ this.otel = new NodeSDK({
99
+ traceExporter,
100
+ serviceName,
101
+ instrumentations: [
102
+ getNodeAutoInstrumentations({
103
+ '@opentelemetry/instrumentation-dns': {
104
+ enabled: false
105
+ },
106
+ '@opentelemetry/instrumentation-fs': {
107
+ enabled: false
108
+ },
109
+ '@opentelemetry/instrumentation-net': {
110
+ enabled: this.options.createSpanWithNet
111
+ },
112
+ '@opentelemetry/instrumentation-http': {
113
+ ignoreIncomingRequestHook: (request) => {
114
+ if (!this.options.ignoreIncomingPaths) {
115
+ return false
116
+ }
117
+ return this.options.ignoreIncomingPaths.some((matcher) => applyMatcher(matcher, request))
118
+ },
119
+ ignoreOutgoingRequestHook: (request) => {
120
+ if (!this.options.ignoreOutgoingUrls) {
121
+ return false
122
+ }
123
+ return this.options.ignoreOutgoingUrls.some((matcher) => applyMatcher(matcher, request))
124
+ }
125
+ }
126
+ })
127
+ ]
128
+ })
129
+
130
+ this.otel.start()
131
+
132
+ Configuration.configureModule({
133
+ otel_tracing: true
134
+ })
135
+ }
136
+
137
+ getTracer () {
138
+ if (!this.options.serviceName) {
139
+ throw new Error('serviceName is required')
140
+ }
141
+ const { trace } = require('@opentelemetry/api')
142
+ return trace.getTracer(this.options.serviceName)
143
+ }
144
+
145
+ destroy () {
146
+ if (!this.otel) return
147
+ this.logger('stop otel tracer')
148
+ this.otel.shutdown()
149
+
150
+ Configuration.configureModule({
151
+ otel_tracing: false
152
+ })
153
+ }
154
+ }
155
+
156
+ function applyMatcher (matcher, request) {
157
+ if (!matcher) {
158
+ return false
159
+ }
160
+ if (!request.url) {
161
+ return false
162
+ }
163
+
164
+ if (typeof matcher === 'string') {
165
+ return request.url.includes(matcher)
166
+ }
167
+ if (matcher instanceof RegExp) {
168
+ return matcher.test(request.url)
169
+ }
170
+
171
+ return matcher(request.url, request)
172
+ }
173
+
174
+ module.exports = { TracingFeature }
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ const PMX = require('./pmx')
4
+
5
+ const IO_KEY = Symbol.for('@pm2/io')
6
+ const isAlreadyHere = (Object.getOwnPropertySymbols(global).indexOf(IO_KEY) > -1)
7
+
8
+ const io = isAlreadyHere ? global[IO_KEY] : new PMX().init()
9
+ global[IO_KEY] = io
10
+
11
+ module.exports = io
@@ -0,0 +1,136 @@
1
+ 'use strict'
2
+
3
+ const { MetricType } = require('../services/metrics')
4
+ const { ServiceManager } = require('../serviceManager')
5
+ const Debug = require('debug')
6
+ const Histogram = require('../utils/metrics/histogram')
7
+
8
+ const defaultOptions = {
9
+ eventLoopActive: true,
10
+ eventLoopDelay: true
11
+ }
12
+
13
+ class EventLoopHandlesRequestsMetric {
14
+ constructor () {
15
+ this.metricService = undefined
16
+ this.logger = Debug('axm:features:metrics:eventloop')
17
+ this.requestTimer = undefined
18
+ this.handleTimer = undefined
19
+ this.delayTimer = undefined
20
+ this.delayLoopInterval = 1000
21
+ this.runtimeStatsService = undefined
22
+ this.handle = undefined
23
+ }
24
+
25
+ init (config) {
26
+ if (config === false) return
27
+ if (config === undefined) {
28
+ config = defaultOptions
29
+ }
30
+ if (config === true) {
31
+ config = defaultOptions
32
+ }
33
+ this.metricService = ServiceManager.get('metrics')
34
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
35
+
36
+ this.logger('init')
37
+ if (typeof process._getActiveRequests === 'function' && config.eventLoopActive === true) {
38
+ const requestMetric = this.metricService.metric({
39
+ name: 'Active requests',
40
+ id: 'internal/libuv/requests',
41
+ historic: true
42
+ })
43
+ this.requestTimer = setInterval(_ => {
44
+ requestMetric.set(process._getActiveRequests().length)
45
+ }, 1000)
46
+ this.requestTimer.unref()
47
+ }
48
+
49
+ if (typeof process._getActiveHandles === 'function' && config.eventLoopActive === true) {
50
+ const handleMetric = this.metricService.metric({
51
+ name: 'Active handles',
52
+ id: 'internal/libuv/handles',
53
+ historic: true
54
+ })
55
+ this.handleTimer = setInterval(_ => {
56
+ handleMetric.set(process._getActiveHandles().length)
57
+ }, 1000)
58
+ this.handleTimer.unref()
59
+ }
60
+
61
+ if (config.eventLoopDelay === false) return
62
+
63
+ const histogram = new Histogram()
64
+
65
+ const uvLatencyp50 = {
66
+ name: 'Event Loop Latency',
67
+ id: 'internal/libuv/latency/p50',
68
+ type: MetricType.histogram,
69
+ historic: true,
70
+ implementation: histogram,
71
+ handler: function () {
72
+ const percentiles = this.implementation.percentiles([ 0.5 ])
73
+ if (percentiles[0.5] == null) return null
74
+ return percentiles[0.5].toFixed(2)
75
+ },
76
+ unit: 'ms'
77
+ }
78
+ const uvLatencyp95 = {
79
+ name: 'Event Loop Latency p95',
80
+ id: 'internal/libuv/latency/p95',
81
+ type: MetricType.histogram,
82
+ historic: true,
83
+ implementation: histogram,
84
+ handler: function () {
85
+ const percentiles = this.implementation.percentiles([ 0.95 ])
86
+ if (percentiles[0.95] == null) return null
87
+ return percentiles[0.95].toFixed(2)
88
+ },
89
+ unit: 'ms'
90
+ }
91
+
92
+ this.metricService.registerMetric(uvLatencyp50)
93
+ this.metricService.registerMetric(uvLatencyp95)
94
+
95
+ this.runtimeStatsService = ServiceManager.get('runtimeStats')
96
+ if (this.runtimeStatsService === undefined) {
97
+ this.logger('runtimeStats module not found, fallbacking into pure js method')
98
+ let oldTime = process.hrtime()
99
+ this.delayTimer = setInterval(() => {
100
+ const newTime = process.hrtime()
101
+ const delay = (newTime[0] - oldTime[0]) * 1e3 + (newTime[1] - oldTime[1]) / 1e6 - this.delayLoopInterval
102
+ oldTime = newTime
103
+ histogram.update(delay)
104
+ }, this.delayLoopInterval)
105
+
106
+ this.delayTimer.unref()
107
+ } else {
108
+ this.logger('using runtimeStats module as data source for event loop latency')
109
+ this.handle = (stats) => {
110
+ if (typeof stats !== 'object' || !Array.isArray(stats.ticks)) return
111
+ stats.ticks.forEach((tick) => {
112
+ histogram.update(tick)
113
+ })
114
+ }
115
+ this.runtimeStatsService.on('data', this.handle)
116
+ }
117
+ }
118
+
119
+ destroy () {
120
+ if (this.requestTimer !== undefined) {
121
+ clearInterval(this.requestTimer)
122
+ }
123
+ if (this.handleTimer !== undefined) {
124
+ clearInterval(this.handleTimer)
125
+ }
126
+ if (this.delayTimer !== undefined) {
127
+ clearInterval(this.delayTimer)
128
+ }
129
+ if (this.runtimeStatsService !== undefined) {
130
+ this.runtimeStatsService.removeListener('data', this.handle)
131
+ }
132
+ this.logger('destroy')
133
+ }
134
+ }
135
+
136
+ module.exports = EventLoopHandlesRequestsMetric