@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,175 @@
1
+ 'use strict'
2
+
3
+ const shimmer = require('../utils/shimmer')
4
+ const Debug = require('debug')
5
+ const Configuration = require('../configuration')
6
+ const { ServiceManager } = require('../serviceManager')
7
+ const Meter = require('../utils/metrics/meter')
8
+ const Histogram = require('../utils/metrics/histogram')
9
+
10
+ const {
11
+ MetricType
12
+ } = require('../services/metrics')
13
+
14
+ class HttpMetrics {
15
+ constructor () {
16
+ this.defaultConf = {
17
+ http: true
18
+ }
19
+ this.metrics = new Map()
20
+ this.logger = Debug('axm:features:metrics:http')
21
+ this.metricService = undefined
22
+ this.modules = {}
23
+ }
24
+
25
+ init (config) {
26
+ if (config === false) return
27
+ if (config === undefined) {
28
+ config = this.defaultConf
29
+ }
30
+ if (typeof config !== 'object') {
31
+ config = this.defaultConf
32
+ }
33
+ this.logger('init')
34
+ Configuration.configureModule({
35
+ latency: true
36
+ })
37
+ this.metricService = ServiceManager.get('metrics')
38
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
39
+
40
+ this.logger('hooking to require')
41
+ this._hookRequire()
42
+ }
43
+
44
+ _registerHttpMetric () {
45
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
46
+ const histogram = new Histogram()
47
+ const p50 = {
48
+ name: 'HTTP Mean Latency',
49
+ id: 'internal/http/builtin/latency/p50',
50
+ type: MetricType.histogram,
51
+ historic: true,
52
+ implementation: histogram,
53
+ unit: 'ms',
54
+ handler: () => {
55
+ const percentiles = histogram.percentiles([ 0.5 ])
56
+ return percentiles[0.5]
57
+ }
58
+ }
59
+ const p95 = {
60
+ name: 'HTTP P95 Latency',
61
+ id: 'internal/http/builtin/latency/p95',
62
+ type: MetricType.histogram,
63
+ historic: true,
64
+ implementation: histogram,
65
+ handler: () => {
66
+ const percentiles = histogram.percentiles([ 0.95 ])
67
+ return percentiles[0.95]
68
+ },
69
+ unit: 'ms'
70
+ }
71
+ const meter = {
72
+ name: 'HTTP',
73
+ historic: true,
74
+ id: 'internal/http/builtin/reqs',
75
+ unit: 'req/min'
76
+ }
77
+ this.metricService.registerMetric(p50)
78
+ this.metricService.registerMetric(p95)
79
+ this.metrics.set('http.latency', histogram)
80
+ this.metrics.set('http.meter', this.metricService.meter(meter))
81
+ }
82
+
83
+ _registerHttpsMetric () {
84
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
85
+ const histogram = new Histogram()
86
+ const p50 = {
87
+ name: 'HTTPS Mean Latency',
88
+ id: 'internal/https/builtin/latency/p50',
89
+ type: MetricType.histogram,
90
+ historic: true,
91
+ implementation: histogram,
92
+ unit: 'ms',
93
+ handler: () => {
94
+ const percentiles = histogram.percentiles([ 0.5 ])
95
+ return percentiles[0.5]
96
+ }
97
+ }
98
+ const p95 = {
99
+ name: 'HTTPS P95 Latency',
100
+ id: 'internal/https/builtin/latency/p95',
101
+ type: MetricType.histogram,
102
+ historic: true,
103
+ implementation: histogram,
104
+ handler: () => {
105
+ const percentiles = histogram.percentiles([ 0.95 ])
106
+ return percentiles[0.95]
107
+ },
108
+ unit: 'ms'
109
+ }
110
+ const meter = {
111
+ name: 'HTTPS',
112
+ historic: true,
113
+ id: 'internal/https/builtin/reqs',
114
+ unit: 'req/min'
115
+ }
116
+ this.metricService.registerMetric(p50)
117
+ this.metricService.registerMetric(p95)
118
+ this.metrics.set('https.latency', histogram)
119
+ this.metrics.set('https.meter', this.metricService.meter(meter))
120
+ }
121
+
122
+ destroy () {
123
+ if (this.modules.http !== undefined) {
124
+ this.logger('unwraping http module')
125
+ shimmer.unwrap(this.modules.http, 'emit')
126
+ this.modules.http = undefined
127
+ }
128
+ if (this.modules.https !== undefined) {
129
+ this.logger('unwraping https module')
130
+ shimmer.unwrap(this.modules.https, 'emit')
131
+ this.modules.https = undefined
132
+ }
133
+ this.logger('destroy')
134
+ }
135
+
136
+ _hookHttp (nodule, name) {
137
+ if (nodule.Server === undefined || nodule.Server.prototype === undefined) return
138
+ if (this.modules[name] !== undefined) return this.logger(`Module ${name} already hooked`)
139
+ this.logger(`Hooking to ${name} module`)
140
+ this.modules[name] = nodule.Server.prototype
141
+ if (name === 'http') {
142
+ this._registerHttpMetric()
143
+ } else if (name === 'https') {
144
+ this._registerHttpsMetric()
145
+ }
146
+ const self = this
147
+ shimmer.wrap(nodule.Server.prototype, 'emit', (original) => {
148
+ return function (event, req, res) {
149
+ if (event !== 'request') return original.apply(this, arguments)
150
+
151
+ const meter = self.metrics.get(`${name}.meter`)
152
+ if (meter !== undefined) {
153
+ meter.mark()
154
+ }
155
+ const latency = self.metrics.get(`${name}.latency`)
156
+ if (latency === undefined) return original.apply(this, arguments)
157
+ if (res === undefined || res === null) return original.apply(this, arguments)
158
+ const startTime = Date.now()
159
+ res.once('finish', _ => {
160
+ latency.update(Date.now() - startTime)
161
+ })
162
+ return original.apply(this, arguments)
163
+ }
164
+ })
165
+ }
166
+
167
+ _hookRequire () {
168
+ const http = require('http')
169
+ const https = require('https')
170
+ this._hookHttp(http, 'http')
171
+ this._hookHttp(https, 'https')
172
+ }
173
+ }
174
+
175
+ module.exports = HttpMetrics
@@ -0,0 +1,132 @@
1
+ 'use strict'
2
+
3
+ const net = require('net')
4
+ const { MetricType } = require('../services/metrics')
5
+ const Debug = require('debug')
6
+ const Meter = require('../utils/metrics/meter')
7
+ const shimmer = require('../utils/shimmer')
8
+ const { ServiceManager } = require('../serviceManager')
9
+
10
+ const defaultConfig = {
11
+ upload: false,
12
+ download: false
13
+ }
14
+
15
+ const allEnabled = {
16
+ upload: true,
17
+ download: true
18
+ }
19
+
20
+ class NetworkMetric {
21
+ constructor () {
22
+ this.metricService = undefined
23
+ this.timer = undefined
24
+ this.logger = Debug('axm:features:metrics:network')
25
+ this.socketProto = undefined
26
+ }
27
+
28
+ init (config) {
29
+ if (config === false) return
30
+ if (config === true) {
31
+ config = allEnabled
32
+ }
33
+ if (config === undefined) {
34
+ config = defaultConfig
35
+ }
36
+
37
+ this.metricService = ServiceManager.get('metrics')
38
+ if (this.metricService === undefined) {
39
+ return this.logger('Failed to load metric service')
40
+ }
41
+
42
+ if (config.download === true) {
43
+ this._catchDownload()
44
+ }
45
+ if (config.upload === true) {
46
+ this._catchUpload()
47
+ }
48
+ this.logger('init')
49
+ }
50
+
51
+ destroy () {
52
+ if (this.timer !== undefined) {
53
+ clearTimeout(this.timer)
54
+ }
55
+
56
+ if (this.socketProto !== undefined && this.socketProto !== null) {
57
+ shimmer.unwrap(this.socketProto, 'read')
58
+ shimmer.unwrap(this.socketProto, 'write')
59
+ }
60
+
61
+ this.logger('destroy')
62
+ }
63
+
64
+ _catchDownload () {
65
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
66
+ const downloadMeter = new Meter({})
67
+
68
+ this.metricService.registerMetric({
69
+ name: 'Network In',
70
+ id: 'internal/network/in',
71
+ historic: true,
72
+ type: MetricType.meter,
73
+ implementation: downloadMeter,
74
+ unit: 'kb/s',
75
+ handler: function () {
76
+ return Math.floor(this.implementation.val() / 1024 * 1000) / 1000
77
+ }
78
+ })
79
+
80
+ setTimeout(() => {
81
+ const property = net.Socket.prototype.read
82
+ const isWrapped = property && property.__wrapped === true
83
+ if (isWrapped) {
84
+ return this.logger('Already patched socket read, canceling')
85
+ }
86
+ shimmer.wrap(net.Socket.prototype, 'read', function (original) {
87
+ return function () {
88
+ this.on('data', (data) => {
89
+ if (typeof data.length === 'number') {
90
+ downloadMeter.mark(data.length)
91
+ }
92
+ })
93
+ return original.apply(this, arguments)
94
+ }
95
+ })
96
+ }, 500)
97
+ }
98
+
99
+ _catchUpload () {
100
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
101
+ const uploadMeter = new Meter()
102
+ this.metricService.registerMetric({
103
+ name: 'Network Out',
104
+ id: 'internal/network/out',
105
+ type: MetricType.meter,
106
+ historic: true,
107
+ implementation: uploadMeter,
108
+ unit: 'kb/s',
109
+ handler: function () {
110
+ return Math.floor(this.implementation.val() / 1024 * 1000) / 1000
111
+ }
112
+ })
113
+
114
+ setTimeout(() => {
115
+ const property = net.Socket.prototype.write
116
+ const isWrapped = property && property.__wrapped === true
117
+ if (isWrapped) {
118
+ return this.logger('Already patched socket write, canceling')
119
+ }
120
+ shimmer.wrap(net.Socket.prototype, 'write', function (original) {
121
+ return function (data) {
122
+ if (typeof data.length === 'number') {
123
+ uploadMeter.mark(data.length)
124
+ }
125
+ return original.apply(this, arguments)
126
+ }
127
+ })
128
+ }, 500)
129
+ }
130
+ }
131
+
132
+ module.exports = NetworkMetric
@@ -0,0 +1,161 @@
1
+ 'use strict'
2
+
3
+ const { MetricType, MetricMeasurements } = 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
+ gcNewPause: true,
10
+ gcOldPause: true,
11
+ pageFaults: true,
12
+ contextSwitchs: true
13
+ }
14
+
15
+ class RuntimeMetrics {
16
+ constructor () {
17
+ this.metricService = undefined
18
+ this.logger = Debug('axm:features:metrics:runtime')
19
+ this.runtimeStatsService = undefined
20
+ this.handle = undefined
21
+ this.metrics = new Map()
22
+ }
23
+
24
+ init (config) {
25
+ if (config === false) return
26
+ if (config === undefined) {
27
+ config = defaultOptions
28
+ }
29
+ if (config === true) {
30
+ config = defaultOptions
31
+ }
32
+
33
+ this.metricService = ServiceManager.get('metrics')
34
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
35
+
36
+ this.runtimeStatsService = ServiceManager.get('runtimeStats')
37
+ if (this.runtimeStatsService === undefined) return this.logger('Failed to load runtime stats service')
38
+
39
+ this.logger('init')
40
+
41
+ const newHistogram = new Histogram()
42
+ if (config.gcNewPause === true) {
43
+ this.metricService.registerMetric({
44
+ name: 'GC New Space Pause',
45
+ id: 'internal/v8/gc/new/pause/p50',
46
+ type: MetricType.histogram,
47
+ historic: true,
48
+ implementation: newHistogram,
49
+ unit: 'ms',
50
+ handler: function () {
51
+ const percentiles = this.implementation.percentiles([ 0.5 ])
52
+ return percentiles[0.5]
53
+ }
54
+ })
55
+ this.metricService.registerMetric({
56
+ name: 'GC New Space Pause p95',
57
+ id: 'internal/v8/gc/new/pause/p95',
58
+ type: MetricType.histogram,
59
+ historic: true,
60
+ implementation: newHistogram,
61
+ unit: 'ms',
62
+ handler: function () {
63
+ const percentiles = this.implementation.percentiles([ 0.95 ])
64
+ return percentiles[0.95]
65
+ }
66
+ })
67
+ }
68
+
69
+ const oldHistogram = new Histogram()
70
+ if (config.gcOldPause === true) {
71
+ this.metricService.registerMetric({
72
+ name: 'GC Old Space Pause',
73
+ id: 'internal/v8/gc/old/pause/p50',
74
+ type: MetricType.histogram,
75
+ historic: true,
76
+ implementation: oldHistogram,
77
+ unit: 'ms',
78
+ handler: function () {
79
+ const percentiles = this.implementation.percentiles([ 0.5 ])
80
+ return percentiles[0.5]
81
+ }
82
+ })
83
+ this.metricService.registerMetric({
84
+ name: 'GC Old Space Pause p95',
85
+ id: 'internal/v8/gc/old/pause/p95',
86
+ type: MetricType.histogram,
87
+ historic: true,
88
+ implementation: oldHistogram,
89
+ unit: 'ms',
90
+ handler: function () {
91
+ const percentiles = this.implementation.percentiles([ 0.95 ])
92
+ return percentiles[0.95]
93
+ }
94
+ })
95
+ }
96
+
97
+ if (config.contextSwitchs === true) {
98
+ const volontarySwitchs = this.metricService.histogram({
99
+ name: 'Volontary CPU Context Switch',
100
+ id: 'internal/uv/cpu/contextswitch/volontary',
101
+ measurement: MetricMeasurements.mean
102
+ })
103
+ const inVolontarySwitchs = this.metricService.histogram({
104
+ name: 'Involuntary CPU Context Switch',
105
+ id: 'internal/uv/cpu/contextswitch/involontary',
106
+ measurement: MetricMeasurements.mean
107
+ })
108
+ this.metrics.set('inVolontarySwitchs', inVolontarySwitchs)
109
+ this.metrics.set('volontarySwitchs', volontarySwitchs)
110
+ }
111
+
112
+ if (config.pageFaults === true) {
113
+ const softPageFault = this.metricService.histogram({
114
+ name: 'Minor Page Fault',
115
+ id: 'internal/uv/memory/pagefault/minor',
116
+ measurement: MetricMeasurements.mean
117
+ })
118
+ const hardPageFault = this.metricService.histogram({
119
+ name: 'Major Page Fault',
120
+ id: 'internal/uv/memory/pagefault/major',
121
+ measurement: MetricMeasurements.mean
122
+ })
123
+ this.metrics.set('softPageFault', softPageFault)
124
+ this.metrics.set('hardPageFault', hardPageFault)
125
+ }
126
+
127
+ this.handle = (stats) => {
128
+ if (typeof stats !== 'object' || typeof stats.gc !== 'object') return
129
+ newHistogram.update(stats.gc.newPause)
130
+ oldHistogram.update(stats.gc.oldPause)
131
+ if (typeof stats.usage !== 'object') return
132
+ const volontarySwitchs = this.metrics.get('volontarySwitchs')
133
+ if (volontarySwitchs !== undefined) {
134
+ volontarySwitchs.update(stats.usage.ru_nvcsw)
135
+ }
136
+ const inVolontarySwitchs = this.metrics.get('inVolontarySwitchs')
137
+ if (inVolontarySwitchs !== undefined) {
138
+ inVolontarySwitchs.update(stats.usage.ru_nivcsw)
139
+ }
140
+ const softPageFault = this.metrics.get('softPageFault')
141
+ if (softPageFault !== undefined) {
142
+ softPageFault.update(stats.usage.ru_minflt)
143
+ }
144
+ const hardPageFault = this.metrics.get('hardPageFault')
145
+ if (hardPageFault !== undefined) {
146
+ hardPageFault.update(stats.usage.ru_majflt)
147
+ }
148
+ }
149
+
150
+ this.runtimeStatsService.on('data', this.handle)
151
+ }
152
+
153
+ destroy () {
154
+ if (this.runtimeStatsService !== undefined) {
155
+ this.runtimeStatsService.removeListener('data', this.handle)
156
+ }
157
+ this.logger('destroy')
158
+ }
159
+ }
160
+
161
+ module.exports = RuntimeMetrics
@@ -0,0 +1,104 @@
1
+ 'use strict'
2
+
3
+ const v8 = require('v8')
4
+ const { ServiceManager } = require('../serviceManager')
5
+ const Debug = require('debug')
6
+ const Gauge = require('../utils/metrics/gauge')
7
+
8
+ const defaultOptions = {
9
+ new_space: false,
10
+ old_space: false,
11
+ map_space: false,
12
+ code_space: false,
13
+ large_object_space: false,
14
+ heap_total_size: true,
15
+ heap_used_size: true,
16
+ heap_used_percent: true
17
+ }
18
+
19
+ class V8Metric {
20
+ constructor () {
21
+ this.timer = undefined
22
+ this.TIME_INTERVAL = 800
23
+ this.metricService = undefined
24
+ this.logger = Debug('axm:features:metrics:v8')
25
+ this.metricStore = new Map()
26
+ this.unitKB = 'MiB'
27
+ this.metricsDefinitions = {
28
+ total_heap_size: {
29
+ name: 'Heap Size',
30
+ id: 'internal/v8/heap/total',
31
+ unit: this.unitKB,
32
+ historic: true
33
+ },
34
+ heap_used_percent: {
35
+ name: 'Heap Usage',
36
+ id: 'internal/v8/heap/usage',
37
+ unit: '%',
38
+ historic: true
39
+ },
40
+ used_heap_size: {
41
+ name: 'Used Heap Size',
42
+ id: 'internal/v8/heap/used',
43
+ unit: this.unitKB,
44
+ historic: true
45
+ }
46
+ }
47
+ }
48
+
49
+ init (config) {
50
+ if (config === false) return
51
+ if (config === undefined) {
52
+ config = defaultOptions
53
+ }
54
+ if (config === true) {
55
+ config = defaultOptions
56
+ }
57
+
58
+ this.metricService = ServiceManager.get('metrics')
59
+ if (this.metricService === undefined) return this.logger('Failed to load metric service')
60
+ this.logger('init')
61
+
62
+ if (!v8.hasOwnProperty('getHeapStatistics')) {
63
+ return this.logger('V8.getHeapStatistics is not available, aborting')
64
+ }
65
+
66
+ for (let metricName in this.metricsDefinitions) {
67
+ if (config[metricName] === false) continue
68
+ const isEnabled = config[metricName]
69
+ if (isEnabled === false) continue
70
+ let metric = this.metricsDefinitions[metricName]
71
+ this.metricStore.set(metricName, this.metricService.metric(metric))
72
+ }
73
+
74
+ this.timer = setInterval(() => {
75
+ const stats = v8.getHeapStatistics()
76
+ for (let metricName in this.metricsDefinitions) {
77
+ if (typeof stats[metricName] !== 'number') continue
78
+ const gauge = this.metricStore.get(metricName)
79
+ if (gauge === undefined) continue
80
+ gauge.set(this._formatMiBytes(stats[metricName]))
81
+ }
82
+ const usage = (stats.used_heap_size / stats.total_heap_size * 100).toFixed(2)
83
+ const usageMetric = this.metricStore.get('heap_used_percent')
84
+ if (usageMetric !== undefined) {
85
+ usageMetric.set(parseFloat(usage))
86
+ }
87
+ }, this.TIME_INTERVAL)
88
+
89
+ this.timer.unref()
90
+ }
91
+
92
+ destroy () {
93
+ if (this.timer !== undefined) {
94
+ clearInterval(this.timer)
95
+ }
96
+ this.logger('destroy')
97
+ }
98
+
99
+ _formatMiBytes (val) {
100
+ return (val / 1024 / 1024).toFixed(2)
101
+ }
102
+ }
103
+
104
+ module.exports = V8Metric
@@ -0,0 +1,7 @@
1
+ 'use strict'
2
+
3
+ const Constants = {
4
+ MINIMUM_TRACE_DURATION: process.env.NODE_ENV === 'test' ? 0 : 1000,
5
+ }
6
+
7
+ module.exports = { Constants }
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ module.exports = require('./node')
@@ -0,0 +1,17 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ module.exports = require('./util')
@@ -0,0 +1,51 @@
1
+ /*
2
+ * Copyright The OpenTelemetry Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * https://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ const { diag } = require('@opentelemetry/api')
18
+ const { ExportResultCode } = require('@opentelemetry/core')
19
+ const { Constants } = require('../../../constants')
20
+
21
+ /**
22
+ * Prepares send function that will send spans to the remote Zipkin service.
23
+ * @param transport - transport instance
24
+ * @param headers - headers
25
+ * send
26
+ */
27
+ function prepareSend (transport, headers) {
28
+ /**
29
+ * Send spans to the remote Zipkin service.
30
+ */
31
+ return function send (zipkinSpans, done) {
32
+ if (zipkinSpans.length === 0) {
33
+ diag.debug('Zipkin send with empty spans')
34
+ return done({ code: ExportResultCode.SUCCESS })
35
+ }
36
+
37
+ zipkinSpans.forEach(span => {
38
+ const isRootClient = span.kind === 'CLIENT' && !span.parentId
39
+ if (isRootClient) return
40
+
41
+ /* CUSTOM - DROP USELESS TRACE */
42
+ if ((span.duration > Constants.MINIMUM_TRACE_DURATION)) {
43
+ transport.send('trace-span', span)
44
+ }
45
+ })
46
+
47
+ return done({ code: ExportResultCode.SUCCESS })
48
+ }
49
+ }
50
+
51
+ module.exports = { prepareSend }