@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,204 @@
1
+ 'use strict'
2
+
3
+ // Hacked from https://github.com/felixge/node-measured
4
+
5
+ var EDS = require('../EDS.js')
6
+
7
+ function Histogram (opts) {
8
+ opts = opts || {}
9
+
10
+ this._measurement = opts.measurement
11
+ this._call_fn = null
12
+
13
+ var methods = {
14
+ min: this.getMin,
15
+ max: this.getMax,
16
+ sum: this.getSum,
17
+ count: this.getCount,
18
+ variance: this._calculateVariance,
19
+ mean: this._calculateMean,
20
+ stddev: this._calculateStddev,
21
+ ema: this.getEma()
22
+ }
23
+
24
+ if (methods[this._measurement]) {
25
+ this._call_fn = methods[this._measurement]
26
+ } else {
27
+ this._call_fn = function () {
28
+ var percentiles = this.percentiles([0.5, 0.75, 0.95, 0.99, 0.999])
29
+
30
+ var medians = {
31
+ median: percentiles[0.5],
32
+ p75: percentiles[0.75],
33
+ p95: percentiles[0.95],
34
+ p99: percentiles[0.99],
35
+ p999: percentiles[0.999]
36
+ }
37
+
38
+ return medians[this._measurement]
39
+ }
40
+ }
41
+ this._sample = new EDS()
42
+ this._min = null
43
+ this._max = null
44
+ this._count = 0
45
+ this._sum = 0
46
+
47
+ // These are for the Welford algorithm for calculating running variance
48
+ // without floating-point doom.
49
+ this._varianceM = 0
50
+ this._varianceS = 0
51
+ this._ema = 0
52
+ }
53
+
54
+ Histogram.prototype.update = function (value) {
55
+ this._count++
56
+ this._sum += value
57
+
58
+ this._sample.update(value)
59
+ this._updateMin(value)
60
+ this._updateMax(value)
61
+ this._updateVariance(value)
62
+ this._updateEma(value)
63
+ }
64
+
65
+ Histogram.prototype.percentiles = function (percentiles) {
66
+ var values = this._sample
67
+ .toArray()
68
+ .sort(function (a, b) {
69
+ return (a === b)
70
+ ? 0
71
+ : a - b
72
+ })
73
+
74
+ var results = {}
75
+
76
+ for (var i = 0; i < percentiles.length; i++) {
77
+ var percentile = percentiles[i]
78
+ if (!values.length) {
79
+ results[percentile] = null
80
+ continue
81
+ }
82
+
83
+ var pos = percentile * (values.length + 1)
84
+
85
+ if (pos < 1) {
86
+ results[percentile] = values[0]
87
+ } else if (pos >= values.length) {
88
+ results[percentile] = values[values.length - 1]
89
+ } else {
90
+ var lower = values[Math.floor(pos) - 1]
91
+ var upper = values[Math.ceil(pos) - 1]
92
+
93
+ results[percentile] = lower + (pos - Math.floor(pos)) * (upper - lower)
94
+ }
95
+ }
96
+
97
+ return results
98
+ }
99
+
100
+ Histogram.prototype.reset = function () {
101
+ this.constructor.call(this) // eslint-disable-line
102
+ }
103
+
104
+ Histogram.prototype.val = function () {
105
+ if (typeof (this._call_fn) === 'function') {
106
+ return this._call_fn()
107
+ } else {
108
+ return this._call_fn
109
+ }
110
+ }
111
+
112
+ Histogram.prototype.getMin = function () {
113
+ return this._min
114
+ }
115
+
116
+ Histogram.prototype.getMax = function () {
117
+ return this._max
118
+ }
119
+
120
+ Histogram.prototype.getSum = function () {
121
+ return this._sum
122
+ }
123
+
124
+ Histogram.prototype.getCount = function () {
125
+ return this._count
126
+ }
127
+
128
+ Histogram.prototype.getEma = function () {
129
+ return this._ema
130
+ }
131
+
132
+ Histogram.prototype.fullResults = function () {
133
+ var percentiles = this.percentiles([0.5, 0.75, 0.95, 0.99, 0.999])
134
+
135
+ return {
136
+ min: this._min,
137
+ max: this._max,
138
+ sum: this._sum,
139
+ variance: this._calculateVariance(),
140
+ mean: this._calculateMean(),
141
+ stddev: this._calculateStddev(),
142
+ count: this._count,
143
+ median: percentiles[0.5],
144
+ p75: percentiles[0.75],
145
+ p95: percentiles[0.95],
146
+ p99: percentiles[0.99],
147
+ p999: percentiles[0.999],
148
+ ema: this._ema
149
+ }
150
+ }
151
+
152
+ Histogram.prototype._updateMin = function (value) {
153
+ if (this._min === null || value < this._min) {
154
+ this._min = value
155
+ // console.log(value);
156
+ }
157
+ }
158
+
159
+ Histogram.prototype._updateMax = function (value) {
160
+ if (this._max === null || value > this._max) {
161
+ this._max = value
162
+ }
163
+ }
164
+
165
+ Histogram.prototype._updateVariance = function (value) {
166
+ if (this._count === 1) {
167
+ this._varianceM = value
168
+ return this._varianceM
169
+ }
170
+
171
+ var oldM = this._varianceM
172
+
173
+ this._varianceM += ((value - oldM) / this._count)
174
+ this._varianceS += ((value - oldM) * (value - this._varianceM))
175
+ }
176
+
177
+ Histogram.prototype._updateEma = function (value) {
178
+ if (this._count <= 1) {
179
+ this._ema = this._calculateMean()
180
+ return this._ema
181
+ }
182
+ var alpha = 2 / (1 + this._count)
183
+ this._ema = value * alpha + this._ema * (1 - alpha)
184
+ }
185
+
186
+ Histogram.prototype._calculateMean = function () {
187
+ return (this._count === 0)
188
+ ? 0
189
+ : this._sum / this._count
190
+ }
191
+
192
+ Histogram.prototype._calculateVariance = function () {
193
+ return (this._count <= 1)
194
+ ? null
195
+ : this._varianceS / (this._count - 1)
196
+ }
197
+
198
+ Histogram.prototype._calculateStddev = function () {
199
+ return (this._count < 1)
200
+ ? null
201
+ : Math.sqrt(this._calculateVariance())
202
+ }
203
+
204
+ module.exports = Histogram
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+ // Time units, as found in Java:
3
+ // see: http://download.oracle.com/javase/6/docs/api/java/util/concurrent/TimeUnit.html
4
+ exports.NANOSECONDS = 1 / (1000 * 1000)
5
+ exports.MICROSECONDS = 1 / 1000
6
+ exports.MILLISECONDS = 1
7
+ exports.SECONDS = 1000 * exports.MILLISECONDS
8
+ exports.MINUTES = 60 * exports.SECONDS
9
+ exports.HOURS = 60 * exports.MINUTES
10
+ exports.DAYS = 24 * exports.HOURS
@@ -0,0 +1,141 @@
1
+ 'use strict'
2
+
3
+ const Debug = require('debug')
4
+ const debug = Debug('axm:configuration')
5
+
6
+ const { ServiceManager } = require('./serviceManager')
7
+ const Autocast = require('./utils/autocast')
8
+ const path = require('path')
9
+ const fs = require('fs')
10
+
11
+ const VERSION = '8.0.0'
12
+
13
+ class Configuration {
14
+
15
+ static configureModule (opts) {
16
+ if (ServiceManager.get('transport')) ServiceManager.get('transport').setOptions(opts)
17
+ }
18
+
19
+ static findPackageJson () {
20
+ try {
21
+ require.main = Configuration.getMain()
22
+ } catch (_e) {
23
+ // Ignore error when getter is set on require.main, but no setter
24
+ }
25
+
26
+ if (!require.main) {
27
+ return
28
+ }
29
+
30
+ if (!require.main.paths) {
31
+ return
32
+ }
33
+
34
+ let pkgPath = path.resolve(path.dirname(require.main.filename), 'package.json')
35
+ try {
36
+ fs.statSync(pkgPath)
37
+ } catch (e) {
38
+ try {
39
+ pkgPath = path.resolve(path.dirname(require.main.filename), '..', 'package.json')
40
+ fs.statSync(pkgPath)
41
+ } catch (e) {
42
+ debug('Cannot find package.json')
43
+ try {
44
+ pkgPath = path.resolve(path.dirname(require.main.filename), '..', '..', 'package.json')
45
+ fs.statSync(pkgPath)
46
+ } catch (e) {
47
+ debug('Cannot find package.json')
48
+ return null
49
+ }
50
+ }
51
+ return pkgPath
52
+ }
53
+
54
+ return pkgPath
55
+ }
56
+
57
+ static init (conf, doNotTellPm2) {
58
+ const packageFilepath = Configuration.findPackageJson()
59
+ let packageJson
60
+
61
+ if (!conf.module_conf) {
62
+ conf.module_conf = {}
63
+ }
64
+ conf.apm = {
65
+ type: 'node',
66
+ version: VERSION
67
+ }
68
+
69
+ if (conf.isModule === true) {
70
+ /**
71
+ * Merge package.json metadata
72
+ */
73
+ try {
74
+ if (!packageFilepath) throw new Error('Cannot find package.json')
75
+ packageJson = require(packageFilepath)
76
+
77
+ conf.module_version = packageJson.version
78
+ conf.module_name = packageJson.name
79
+ conf.description = packageJson.description
80
+
81
+ if (packageJson.config) {
82
+ conf = Object.assign(conf, packageJson.config)
83
+ conf.module_conf = packageJson.config
84
+ }
85
+ } catch (e) {
86
+ throw new Error(e)
87
+ }
88
+ } else {
89
+ conf.module_name = process.env.name || 'outside-pm2'
90
+ try {
91
+ if (!packageFilepath) throw new Error('Cannot find package.json')
92
+ packageJson = require(packageFilepath)
93
+
94
+ conf.module_version = packageJson.version
95
+
96
+ if (packageJson.config) {
97
+ conf = Object.assign(conf, packageJson.config)
98
+ conf.module_conf = packageJson.config
99
+ }
100
+ } catch (e) {
101
+ debug(e.message)
102
+ }
103
+ }
104
+
105
+ /**
106
+ * If custom variables has been set, merge with returned configuration
107
+ */
108
+ try {
109
+ if (process.env[conf.module_name]) {
110
+ const castedConf = new Autocast().autocast(JSON.parse(process.env[conf.module_name] || ''))
111
+ conf = Object.assign(conf, castedConf)
112
+ // Do not display probe configuration in Keymetrics
113
+ delete castedConf.probes
114
+ // This is the configuration variable modifiable from keymetrics
115
+ conf.module_conf = JSON.parse(JSON.stringify(Object.assign(conf.module_conf, castedConf)))
116
+
117
+ // Obfuscate passwords
118
+ Object.keys(conf.module_conf).forEach(function (key) {
119
+ if ((key === 'password' || key === 'passwd') &&
120
+ conf.module_conf[key].length >= 1) {
121
+ conf.module_conf[key] = 'Password hidden'
122
+ }
123
+
124
+ })
125
+ }
126
+ } catch (e) {
127
+ debug(e)
128
+ }
129
+
130
+ if (doNotTellPm2 === true) return conf
131
+
132
+ Configuration.configureModule(conf)
133
+ return conf
134
+ }
135
+
136
+ static getMain () {
137
+ return require.main || { filename: './somefile.js' }
138
+ }
139
+ }
140
+
141
+ module.exports = Configuration
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+
3
+ module.exports = {
4
+ METRIC_INTERVAL: 990
5
+ }
6
+
7
+ module.exports.canUseInspector = function canUseInspector () {
8
+ const isBun = typeof Bun !== 'undefined'
9
+ return !isBun
10
+ }
@@ -0,0 +1,110 @@
1
+ 'use strict'
2
+
3
+ const { NotifyFeature } = require('./features/notify')
4
+ const { ProfilingFeature } = require('./features/profiling')
5
+ const { EventsFeature } = require('./features/events')
6
+ const { MetricsFeature } = require('./features/metrics')
7
+ const { TracingFeature } = require('./features/tracing')
8
+ const { DependenciesFeature } = require('./features/dependencies')
9
+ const Debug = require('debug')
10
+
11
+ function getObjectAtPath (context, path) {
12
+ if (path.indexOf('.') === -1 && path.indexOf('[') === -1) {
13
+ return context[path]
14
+ }
15
+
16
+ let crumbs = path.split(/\.|\[|\]/g)
17
+ let i = -1
18
+ let len = crumbs.length
19
+ let result
20
+
21
+ while (++i < len) {
22
+ if (i === 0) result = context
23
+ if (!crumbs[i]) continue
24
+ if (result === undefined) break
25
+ result = result[crumbs[i]]
26
+ }
27
+
28
+ return result
29
+ }
30
+
31
+ const availablesFeatures = [
32
+ {
33
+ name: 'notify',
34
+ optionsPath: '.',
35
+ module: NotifyFeature
36
+ },
37
+ {
38
+ name: 'profiler',
39
+ optionsPath: 'profiling',
40
+ module: ProfilingFeature
41
+ },
42
+ {
43
+ name: 'events',
44
+ module: EventsFeature
45
+ },
46
+ {
47
+ name: 'metrics',
48
+ optionsPath: 'metrics',
49
+ module: MetricsFeature
50
+ },
51
+ {
52
+ name: 'tracing',
53
+ optionsPath: '.',
54
+ module: TracingFeature
55
+ },
56
+ {
57
+ name: 'dependencies',
58
+ module: DependenciesFeature
59
+ }
60
+ ]
61
+
62
+ class FeatureManager {
63
+
64
+ constructor () {
65
+ this.logger = Debug('axm:features')
66
+ }
67
+
68
+ /**
69
+ * Construct all the features and init them with their respective configuration
70
+ */
71
+ init (options) {
72
+ for (let availableFeature of availablesFeatures) {
73
+ this.logger(`Creating feature ${availableFeature.name}`)
74
+ const feature = new availableFeature.module()
75
+ let config = undefined
76
+ if (typeof availableFeature.optionsPath !== 'string') {
77
+ config = {}
78
+ } else if (availableFeature.optionsPath === '.') {
79
+ config = options
80
+ } else {
81
+ config = getObjectAtPath(options, availableFeature.optionsPath)
82
+ }
83
+ this.logger(`Init feature ${availableFeature.name}`)
84
+ feature.init(config)
85
+ availableFeature.instance = feature
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Get a internal implementation of a feature method
91
+ * WARNING: should only be used by user facing API
92
+ */
93
+ get (name) {
94
+ const feature = availablesFeatures.find(feature => feature.name === name)
95
+ if (feature === undefined || feature.instance === undefined) {
96
+ throw new Error(`Tried to call feature ${name} which doesn't exist or wasn't initiated`)
97
+ }
98
+ return feature.instance
99
+ }
100
+
101
+ destroy () {
102
+ for (let availableFeature of availablesFeatures) {
103
+ if (availableFeature.instance === undefined) continue
104
+ this.logger(`Destroy feature ${availableFeature.name}`)
105
+ availableFeature.instance.destroy()
106
+ }
107
+ }
108
+ }
109
+
110
+ module.exports = { getObjectAtPath, FeatureManager }
@@ -0,0 +1,48 @@
1
+ 'use strict'
2
+
3
+ const { ServiceManager } = require('../serviceManager')
4
+ const Debug = require('debug')
5
+ const Configuration = require('../configuration')
6
+ const { readFile } = require('fs')
7
+
8
+ class DependenciesFeature {
9
+ constructor () {
10
+ this.transport = undefined
11
+ this.logger = Debug('axm:features:dependencies')
12
+ }
13
+
14
+ init () {
15
+ this.transport = ServiceManager.get('transport')
16
+ this.logger('init')
17
+
18
+ const pkgPath = Configuration.findPackageJson()
19
+ if (typeof pkgPath !== 'string') return this.logger('failed to found pkg.json path')
20
+
21
+ this.logger(`found pkg.json in ${pkgPath}`)
22
+ readFile(pkgPath, (err, data) => {
23
+ if (err) return this.logger('failed to read pkg.json', err)
24
+ try {
25
+ const pkg = JSON.parse(data.toString())
26
+ if (typeof pkg.dependencies !== 'object') {
27
+ return this.logger('failed to find deps in pkg.json')
28
+ }
29
+ const dependencies = Object.keys(pkg.dependencies)
30
+ .reduce((list, name) => {
31
+ list[name] = { version: pkg.dependencies[name] }
32
+ return list
33
+ }, {})
34
+ this.logger(`collected ${Object.keys(dependencies).length} dependencies`)
35
+ this.transport.send('application:dependencies', dependencies)
36
+ this.logger('sent dependencies list')
37
+ } catch (err) {
38
+ return this.logger('failed to parse pkg.json', err)
39
+ }
40
+ })
41
+ }
42
+
43
+ destroy () {
44
+ this.logger('destroy')
45
+ }
46
+ }
47
+
48
+ module.exports = { DependenciesFeature }
@@ -0,0 +1,50 @@
1
+ 'use strict'
2
+
3
+ const { ServiceManager } = require('../serviceManager')
4
+ const Debug = require('debug')
5
+
6
+ class EventsFeature {
7
+ constructor () {
8
+ this.transport = undefined
9
+ this.logger = Debug('axm:features:events')
10
+ }
11
+
12
+ init () {
13
+ this.transport = ServiceManager.get('transport')
14
+ this.logger('init')
15
+ }
16
+
17
+ emit (name, data) {
18
+ if (typeof name !== 'string') {
19
+ console.error('event name must be a string')
20
+ return console.trace()
21
+ }
22
+ if (typeof data !== 'object') {
23
+ console.error('event data must be an object')
24
+ return console.trace()
25
+ }
26
+ if (data instanceof Array) {
27
+ console.error('event data cannot be an array')
28
+ return console.trace()
29
+ }
30
+
31
+ let inflightObj = {}
32
+ try {
33
+ inflightObj = JSON.parse(JSON.stringify(data))
34
+ } catch (err) {
35
+ return console.log('Failed to serialize the event data', err.message)
36
+ }
37
+
38
+ inflightObj.__name = name
39
+ if (this.transport === undefined) {
40
+ return this.logger('Failed to send event as transporter isnt available')
41
+ }
42
+ this.transport.send('human:event', inflightObj)
43
+ }
44
+
45
+ destroy () {
46
+ this.logger('destroy')
47
+ }
48
+ }
49
+
50
+ module.exports = { EventsFeature }
@@ -0,0 +1,102 @@
1
+ 'use strict'
2
+
3
+ const Debug = require('debug')
4
+ const EventLoopHandlesRequestsMetric = require('../metrics/eventLoopMetrics')
5
+
6
+ function getObjectAtPath (context, path) {
7
+ if (path.indexOf('.') === -1 && path.indexOf('[') === -1) {
8
+ return context[path]
9
+ }
10
+ let crumbs = path.split(/\.|\[|\]/g)
11
+ let i = -1
12
+ let len = crumbs.length
13
+ let result
14
+ while (++i < len) {
15
+ if (i === 0) result = context
16
+ if (!crumbs[i]) continue
17
+ if (result === undefined) break
18
+ result = result[crumbs[i]]
19
+ }
20
+ return result
21
+ }
22
+ const NetworkMetric = require('../metrics/network')
23
+ const HttpMetrics = require('../metrics/httpMetrics')
24
+ const V8Metric = require('../metrics/v8')
25
+ const RuntimeMetrics = require('../metrics/runtime')
26
+
27
+ const defaultMetricConf = {
28
+ eventLoop: true,
29
+ network: false,
30
+ http: true,
31
+ runtime: true,
32
+ v8: true
33
+ }
34
+
35
+ const availableMetrics = [
36
+ {
37
+ name: 'eventloop',
38
+ module: EventLoopHandlesRequestsMetric,
39
+ optionsPath: 'eventLoop'
40
+ },
41
+ {
42
+ name: 'http',
43
+ module: HttpMetrics,
44
+ optionsPath: 'http'
45
+ },
46
+ {
47
+ name: 'network',
48
+ module: NetworkMetric,
49
+ optionsPath: 'network'
50
+ },
51
+ {
52
+ name: 'v8',
53
+ module: V8Metric,
54
+ optionsPath: 'v8'
55
+ },
56
+ {
57
+ name: 'runtime',
58
+ module: RuntimeMetrics,
59
+ optionsPath: 'runtime'
60
+ }
61
+ ]
62
+
63
+ class MetricsFeature {
64
+ constructor () {
65
+ this.logger = Debug('axm:features:metrics')
66
+ }
67
+
68
+ init (options) {
69
+ if (typeof options !== 'object') options = {}
70
+ this.logger('init')
71
+
72
+ for (let availableMetric of availableMetrics) {
73
+ const metric = new availableMetric.module()
74
+ let config = undefined
75
+ if (typeof availableMetric.optionsPath !== 'string') {
76
+ config = {}
77
+ } else if (availableMetric.optionsPath === '.') {
78
+ config = options
79
+ } else {
80
+ config = getObjectAtPath(options, availableMetric.optionsPath)
81
+ }
82
+ metric.init(config)
83
+ availableMetric.instance = metric
84
+ }
85
+ }
86
+
87
+ get (name) {
88
+ const metric = availableMetrics.find(metric => metric.name === name)
89
+ if (metric === undefined) return undefined
90
+ return metric.instance
91
+ }
92
+
93
+ destroy () {
94
+ this.logger('destroy')
95
+ for (let availableMetric of availableMetrics) {
96
+ if (availableMetric.instance === undefined) continue
97
+ availableMetric.instance.destroy()
98
+ }
99
+ }
100
+ }
101
+
102
+ module.exports = { MetricsFeature, defaultMetricConf }