@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,670 @@
1
+ 'use strict'
2
+
3
+ const cst = require('../../constants.js')
4
+ const log = require('debug')('interactor:aggregator')
5
+ const Utility = require('../Utility.js')
6
+ const fclone = require('../../../fclone')
7
+ const Histogram = require('../utils/probes/Histogram')
8
+
9
+ /**
10
+ *
11
+ * # Data structure sent to interactor
12
+ *
13
+ * {
14
+ * 'process_name': {
15
+ * process : {}, // PM2 process meta data
16
+ * data : {
17
+ * routes : [ // array of all routes ordered by count
18
+ * {
19
+ * path: '/', // path of the route
20
+ * meta: {
21
+ * count: 50, // count of this route
22
+ * max: 300, // max latency of this route
23
+ * min: 50, // min latency of this route
24
+ * mean: 120 // mean latency of this route
25
+ * }
26
+ * variances: [{ // array of variance order by count
27
+ * spans : [
28
+ * ... // transactions
29
+ * ],
30
+ * count: 50, // count of this variance
31
+ * max: 300, // max latency of this variance
32
+ * min: 50, // min latency of this variance
33
+ * mean: 120 // mean latency of this variance
34
+ * }]
35
+ * }
36
+ * ],
37
+ * meta : {
38
+ * trace_count : 50, // trace number
39
+ * mean_latency : 40, // global app latency in ms
40
+ * http_meter : 30, // global app req per minutes
41
+ * db_meter : 20, // number of database transaction per min
42
+ * }
43
+ * }
44
+ * }
45
+ * }
46
+ */
47
+
48
+ module.exports = class TransactionAggregator {
49
+ constructor (pushInteractor) {
50
+ this.processes = {}
51
+ this.stackParser = pushInteractor._stackParser
52
+ this.pushInteractor = pushInteractor
53
+
54
+ this.LABELS = {
55
+ 'HTTP_RESPONSE_CODE_LABEL_KEY': 'http/status_code',
56
+ 'HTTP_URL_LABEL_KEY': 'http/url',
57
+ 'HTTP_METHOD_LABEL_KEY': 'http/method',
58
+ 'HTTP_RESPONSE_SIZE_LABEL_KEY': 'http/response/size',
59
+ 'STACK_TRACE_DETAILS_KEY': 'stacktrace',
60
+ 'ERROR_DETAILS_NAME': 'error/name',
61
+ 'ERROR_DETAILS_MESSAGE': 'error/message',
62
+ 'HTTP_SOURCE_IP': 'http/source/ip',
63
+ 'HTTP_PATH_LABEL_KEY': 'http/path'
64
+ }
65
+ this.SPANS_DB = ['redis', 'mysql', 'pg', 'mongo', 'outbound_http']
66
+ this.REGEX_JSON_CLEANUP = /":(?!\[|{)\\"[^"]*\\"|":(["'])(?:(?=(\\?))\2.)*?\1|":(?!\[|{)[^,}\]]*|":\[[^{]*]/g
67
+
68
+ this.init()
69
+ }
70
+
71
+ /**
72
+ * First method to call in real environment
73
+ * - Listen to restart event for initialization period
74
+ * - Clear aggregation on process stop
75
+ * - Launch worker to attach data to be pushed to KM
76
+ */
77
+ init () {
78
+ // New Process Online, reset & wait a bit before processing
79
+ this.pushInteractor._ipm2.bus.on('process:event', (data) => {
80
+ if (data.event !== 'online' || !this.processes[data.process.name]) return false
81
+
82
+ let rev = (data.process.versioning && data.process.versioning.revision)
83
+ ? data.process.versioning.revision : null
84
+
85
+ this.resetAggregation(data.process.name, {
86
+ rev: rev,
87
+ server: this.pushInteractor.opts.MACHINE_NAME
88
+ })
89
+ })
90
+
91
+ // Process getting offline, delete aggregation
92
+ this.pushInteractor._ipm2.bus.on('process:event', (data) => {
93
+ if (data.event !== 'stop' || !this.processes[data.process.name]) return false
94
+ log('Deleting aggregation for %s', data.process.name)
95
+ delete this.processes[data.process.name]
96
+ })
97
+
98
+ this.launchWorker()
99
+ }
100
+
101
+ /**
102
+ * Reset aggregation for target app_name
103
+ */
104
+ resetAggregation (appName, meta) {
105
+ log('Reseting agg for app:%s meta:%j', appName, meta)
106
+
107
+ if (this.processes[appName].initialization_timeout) {
108
+ log('Reseting initialization timeout app:%s', appName)
109
+ clearTimeout(this.processes[appName].initialization_timeout)
110
+ clearInterval(this.processes[appName].notifier)
111
+ this.processes[appName].notifier = null
112
+ }
113
+
114
+ this.processes[appName] = this.initializeRouteMeta({
115
+ name: appName,
116
+ rev: meta.rev,
117
+ server: meta.server
118
+ })
119
+
120
+ let start = Date.now()
121
+ this.processes[appName].notifier = setInterval(_ => {
122
+ let elapsed = Date.now() - start
123
+ // failsafe
124
+ if (elapsed / 1000 > cst.AGGREGATION_DURATION) {
125
+ clearInterval(this.processes[appName].notifier)
126
+ this.processes[appName].notifier = null
127
+ }
128
+
129
+ let msg = {
130
+ data: {
131
+ learning_duration: cst.AGGREGATION_DURATION,
132
+ elapsed: elapsed
133
+ },
134
+ process: this.processes[appName].process
135
+ }
136
+ this.pushInteractor && this.pushInteractor.transport.send('axm:transaction:learning', msg)
137
+ }, 5000)
138
+
139
+ this.processes[appName].initialization_timeout = setTimeout(_ => {
140
+ log('Initialization timeout finished for app:%s', appName)
141
+ clearInterval(this.processes[appName].notifier)
142
+ this.processes[appName].notifier = null
143
+ this.processes[appName].initialization_timeout = null
144
+ }, cst.AGGREGATION_DURATION)
145
+ }
146
+
147
+ /**
148
+ * Clear aggregated data for all process
149
+ */
150
+ clearData () {
151
+ Object.keys(this.processes).forEach((process) => {
152
+ this.resetAggregation(process, this.processes[process].process)
153
+ })
154
+ }
155
+
156
+ /**
157
+ * Generate new entry for application
158
+ *
159
+ * @param {Object} process process meta
160
+ */
161
+ initializeRouteMeta (process) {
162
+ if (process.pm_id) delete process.pm_id
163
+
164
+ return {
165
+ routes: {},
166
+ meta: {
167
+ trace_count: 0,
168
+ http_meter: new Utility.EWMA(),
169
+ db_meter: new Utility.EWMA(),
170
+ histogram: new Histogram({ measurement: 'median' }),
171
+ db_histograms: {}
172
+ },
173
+ process: process
174
+ }
175
+ }
176
+
177
+ getAggregation () {
178
+ return this.processes
179
+ }
180
+
181
+ validateData (packet) {
182
+ if (!packet || !packet.data) {
183
+ log('Packet malformated', packet)
184
+ return false
185
+ }
186
+
187
+ if (!packet.process) {
188
+ log('Got packet without process: %j', packet)
189
+ return false
190
+ }
191
+
192
+ if (!packet.data.spans || !packet.data.spans[0]) {
193
+ log('Trace without spans: %s', Object.keys(packet.data))
194
+ return false
195
+ }
196
+
197
+ if (!packet.data.spans[0].labels) {
198
+ log('Trace spans without labels: %s', Object.keys(packet.data.spans))
199
+ return false
200
+ }
201
+
202
+ return true
203
+ }
204
+
205
+ /**
206
+ * Main method to aggregate and compute stats for traces
207
+ *
208
+ * @param {Object} packet
209
+ * @param {Object} packet.process process metadata
210
+ * @param {Object} packet.data trace
211
+ */
212
+ aggregate (packet) {
213
+ if (this.validateData(packet) === false) return false
214
+
215
+ const newTrace = packet.data
216
+ const appName = packet.process.name
217
+
218
+ if (!this.processes[appName]) {
219
+ this.processes[appName] = this.initializeRouteMeta(packet.process)
220
+ }
221
+
222
+ let process = this.processes[appName]
223
+
224
+ // Get http path of current span
225
+ let path = newTrace.spans[0].labels[this.LABELS.HTTP_PATH_LABEL_KEY]
226
+ if (!path) return false
227
+
228
+ // Cleanup spans
229
+ this.censorSpans(newTrace.spans)
230
+
231
+ // remove spans with startTime == endTime
232
+ newTrace.spans = newTrace.spans.filter((span) => {
233
+ return span.endTime !== span.startTime
234
+ })
235
+
236
+ // compute duration of child spans
237
+ newTrace.spans.forEach((span) => {
238
+ span.mean = Math.round(new Date(span.endTime) - new Date(span.startTime))
239
+ delete span.endTime
240
+ })
241
+
242
+ // Update app meta (mean_latency, http_meter, db_meter, trace_count)
243
+ newTrace.spans.forEach((span) => {
244
+ if (!span.name || !span.kind) return false
245
+
246
+ if (span.kind === 'RPC_SERVER') {
247
+ process.meta.histogram.update(span.mean)
248
+ return process.meta.http_meter.update()
249
+ }
250
+
251
+ // Override outbount http queries for processing
252
+ if (span.labels && span.labels['http/method'] && span.labels['http/status_code']) {
253
+ span.labels['service'] = span.name
254
+ span.name = 'outbound_http'
255
+ }
256
+
257
+ for (let i = 0, len = this.SPANS_DB.length; i < len; i++) {
258
+ if (span.name.indexOf(this.SPANS_DB[i]) > -1) {
259
+ process.meta.db_meter.update()
260
+ if (!process.meta.db_histograms[this.SPANS_DB[i]]) {
261
+ process.meta.db_histograms[this.SPANS_DB[i]] = new Histogram({ measurement: 'mean' })
262
+ }
263
+ process.meta.db_histograms[this.SPANS_DB[i]].update(span.mean)
264
+ break
265
+ }
266
+ }
267
+ })
268
+
269
+ process.meta.trace_count++
270
+
271
+ /**
272
+ * Handle traces aggregation
273
+ */
274
+ if (path[0] === '/' && path !== '/') {
275
+ path = path.substr(1, path.length - 1)
276
+ }
277
+
278
+ let matched = this.matchPath(path, process.routes)
279
+
280
+ if (!matched) {
281
+ process.routes[path] = []
282
+ this.mergeTrace(process.routes[path], newTrace, process)
283
+ } else {
284
+ this.mergeTrace(process.routes[matched], newTrace, process)
285
+ }
286
+
287
+ return this.processes
288
+ }
289
+
290
+ /**
291
+ * Merge new trace and compute mean, min, max, count
292
+ *
293
+ * @param {Object} aggregated previous aggregated route
294
+ * @param {Object} trace
295
+ */
296
+ mergeTrace (aggregated, trace, process) {
297
+ if (!aggregated || !trace) return
298
+
299
+ // if the trace doesn't any spans stop aggregation here
300
+ if (trace.spans.length === 0) return
301
+
302
+ // create data structure if needed
303
+ if (!aggregated.variances) aggregated.variances = []
304
+ if (!aggregated.meta) {
305
+ aggregated.meta = {
306
+ histogram: new Histogram({ measurement: 'median' }),
307
+ meter: new Utility.EWMA()
308
+ }
309
+ }
310
+
311
+ aggregated.meta.histogram.update(trace.spans[0].mean)
312
+ aggregated.meta.meter.update()
313
+
314
+ const merge = (variance) => {
315
+ // no variance found so its a new one
316
+ if (variance == null) {
317
+ delete trace.projectId
318
+ delete trace.traceId
319
+ trace.histogram = new Histogram({ measurement: 'median' })
320
+ trace.histogram.update(trace.spans[0].mean)
321
+
322
+ trace.spans.forEach((span) => {
323
+ span.histogram = new Histogram({ measurement: 'median' })
324
+ span.histogram.update(span.mean)
325
+ delete span.mean
326
+ })
327
+
328
+ // parse strackrace
329
+ this.parseStacktrace(trace.spans)
330
+ aggregated.variances.push(trace)
331
+ } else {
332
+ // check to see if request is anormally slow, if yes send it as inquisitor
333
+ if (trace.spans[0].mean > variance.histogram.percentiles([0.95])[0.95] &&
334
+ typeof pushInteractor !== 'undefined' && !process.initialization_timeout) {
335
+ // serialize and add metadata
336
+ this.parseStacktrace(trace.spans)
337
+ let data = {
338
+ trace: fclone(trace.spans),
339
+ variance: fclone(variance.spans.map((span) => {
340
+ return {
341
+ labels: span.labels,
342
+ kind: span.kind,
343
+ name: span.name,
344
+ startTime: span.startTime,
345
+ percentiles: {
346
+ p5: variance.histogram.percentiles([0.5])[0.5],
347
+ p95: variance.histogram.percentiles([0.95])[0.95]
348
+ }
349
+ }
350
+ })),
351
+ meta: {
352
+ value: trace.spans[0].mean,
353
+ percentiles: {
354
+ p5: variance.histogram.percentiles([0.5])[0.5],
355
+ p75: variance.histogram.percentiles([0.75])[0.75],
356
+ p95: variance.histogram.percentiles([0.95])[0.95],
357
+ p99: variance.histogram.percentiles([0.99])[0.99]
358
+ },
359
+ min: variance.histogram.getMin(),
360
+ max: variance.histogram.getMax(),
361
+ count: variance.histogram.getCount()
362
+ },
363
+ process: process.process
364
+ }
365
+ this.pushInteractor.transport.send('axm:transaction:outlier', data)
366
+ }
367
+
368
+ // variance found, merge spans
369
+ variance.histogram.update(trace.spans[0].mean)
370
+
371
+ // update duration of spans to be mean
372
+ this.updateSpanDuration(variance.spans, trace.spans, variance.count)
373
+
374
+ // delete stacktrace before merging
375
+ trace.spans.forEach((span) => {
376
+ delete span.labels.stacktrace
377
+ })
378
+ }
379
+ }
380
+
381
+ // for every variance, check spans same variance
382
+ for (let i = 0; i < aggregated.variances.length; i++) {
383
+ if (this.compareList(aggregated.variances[i].spans, trace.spans)) {
384
+ return merge(aggregated.variances[i])
385
+ }
386
+ }
387
+ // else its a new variance
388
+ return merge(null)
389
+ }
390
+
391
+ /**
392
+ * Parkour simultaneously both spans list to update value of the first one using value of the second one
393
+ * The first should be variance already aggregated for which we want to merge the second one
394
+ * The second one is a new trace, so we need to re-compute mean/min/max time for each spans
395
+ */
396
+ updateSpanDuration (spans, newSpans) {
397
+ for (let i = 0, len = spans.length; i < len; i++) {
398
+ if (!newSpans[i]) continue
399
+ spans[i].histogram.update(newSpans[i].mean)
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Compare two spans list by going down on each span and comparing child and attribute
405
+ */
406
+ compareList (one, two) {
407
+ if (one.length !== two.length) return false
408
+
409
+ for (let i = 0, len = one; i < len; i++) {
410
+ if (one[i].name !== two[i].name) return false
411
+ if (one[i].kind !== two[i].kind) return false
412
+ if (!one[i].labels && two[i].labels) return false
413
+ if (one[i].labels && !two[i].labels) return false
414
+ if (one[i].labels.length !== two[i].labels.length) return false
415
+ }
416
+ return true
417
+ }
418
+
419
+ /**
420
+ * Will return the route if we found an already matched route
421
+ */
422
+ matchPath (path, routes) {
423
+ // empty route is / without the fist slash
424
+ if (!path || !routes) return false
425
+ if (path === '/') return routes[path] ? path : null
426
+
427
+ // remove the last slash if exist
428
+ if (path[path.length - 1] === '/') {
429
+ path = path.substr(0, path.length - 1)
430
+ }
431
+
432
+ // split to get array of segment
433
+ path = path.split('/')
434
+
435
+ // if the path has only one segment, we just need to compare the key
436
+ if (path.length === 1) return routes[path[0]] ? routes[path[0]] : null
437
+
438
+ // check in routes already stored for match
439
+ let keys = Object.keys(routes)
440
+ for (let i = 0, len = keys.length; i < len; i++) {
441
+ let route = keys[i]
442
+ let segments = route.split('/')
443
+
444
+ if (segments.length !== path.length) continue
445
+
446
+ for (let j = path.length - 1; j >= 0; j--) {
447
+ // different segment, try to find if new route or not
448
+ if (path[j] !== segments[j]) {
449
+ // if the aggregator already have matched that segment with a wildcard and the next segment is the same
450
+ if (this.isIdentifier(path[j]) && segments[j] === '*' && path[j - 1] === segments[j - 1]) {
451
+ return segments.join('/')
452
+ // case a let in url match, so we continue because they must be other let in url
453
+ } else if (path[j - 1] !== undefined && path[j - 1] === segments[j - 1] && this.isIdentifier(path[j]) && this.isIdentifier(segments[j])) {
454
+ segments[j] = '*'
455
+ // update routes in cache
456
+ routes[segments.join('/')] = routes[route]
457
+ delete routes[keys[i]]
458
+ return segments.join('/')
459
+ } else {
460
+ break
461
+ }
462
+ }
463
+
464
+ // if finish to iterate over segment of path, we must be on the same route
465
+ if (j === 0) return segments.join('/')
466
+ }
467
+ }
468
+ }
469
+
470
+ launchWorker () {
471
+ this._worker = setInterval(_ => {
472
+ let normalized = this.prepareAggregationforShipping()
473
+ Object.keys(normalized).forEach((key) => {
474
+ this.pushInteractor.transport.send('axm:transaction', normalized[key])
475
+ })
476
+ }, cst.TRACE_FLUSH_INTERVAL)
477
+ }
478
+
479
+ /**
480
+ * Normalize aggregation
481
+ */
482
+ prepareAggregationforShipping () {
483
+ let normalized = {}
484
+
485
+ // Iterate each applications
486
+ Object.keys(this.processes).forEach((appName) => {
487
+ let process = this.processes[appName]
488
+ let routes = process.routes
489
+
490
+ if (process.initialization_timeout) {
491
+ log('Waiting for app %s to be initialized', appName)
492
+ return null
493
+ }
494
+
495
+ normalized[appName] = {
496
+ data: {
497
+ routes: [],
498
+ meta: fclone({
499
+ trace_count: process.meta.trace_count,
500
+ http_meter: Math.round(process.meta.http_meter.rate(1000) * 100) / 100,
501
+ db_meter: Math.round(process.meta.db_meter.rate(1000) * 100) / 100,
502
+ http_percentiles: {
503
+ median: process.meta.histogram.percentiles([0.5])[0.5],
504
+ p95: process.meta.histogram.percentiles([0.95])[0.95],
505
+ p99: process.meta.histogram.percentiles([0.99])[0.99]
506
+ },
507
+ db_percentiles: {}
508
+ })
509
+ },
510
+ process: process.process
511
+ }
512
+
513
+ // compute percentiles for each db spans if they exist
514
+ this.SPANS_DB.forEach((name) => {
515
+ let histogram = process.meta.db_histograms[name]
516
+ if (!histogram) return
517
+ normalized[appName].data.meta.db_percentiles[name] = fclone(histogram.percentiles([0.5])[0.5])
518
+ })
519
+
520
+ Object.keys(routes).forEach((path) => {
521
+ let data = routes[path]
522
+
523
+ // hard check for invalid data
524
+ if (!data.variances || data.variances.length === 0) return
525
+
526
+ // get top 5 variances of the same route
527
+ const variances = data.variances.sort((a, b) => {
528
+ return b.count - a.count
529
+ }).slice(0, 5)
530
+
531
+ // create a copy without reference to stored one
532
+ let routeCopy = {
533
+ path: path === '/' ? '/' : '/' + path,
534
+ meta: fclone({
535
+ min: data.meta.histogram.getMin(),
536
+ max: data.meta.histogram.getMax(),
537
+ count: data.meta.histogram.getCount(),
538
+ meter: Math.round(data.meta.meter.rate(1000) * 100) / 100,
539
+ median: data.meta.histogram.percentiles([0.5])[0.5],
540
+ p95: data.meta.histogram.percentiles([0.95])[0.95]
541
+ }),
542
+ variances: []
543
+ }
544
+
545
+ variances.forEach((variance) => {
546
+ // hard check for invalid data
547
+ if (!variance.spans || variance.spans.length === 0) return
548
+
549
+ // deep copy of variances data
550
+ let tmp = fclone({
551
+ spans: [],
552
+ count: variance.histogram.getCount(),
553
+ min: variance.histogram.getMin(),
554
+ max: variance.histogram.getMax(),
555
+ median: variance.histogram.percentiles([0.5])[0.5],
556
+ p95: variance.histogram.percentiles([0.95])[0.95]
557
+ })
558
+
559
+ // get data for each span
560
+ variance.spans.forEach((span) => {
561
+ tmp.spans.push(fclone({
562
+ name: span.name,
563
+ labels: span.labels,
564
+ kind: span.kind,
565
+ startTime: span.startTime,
566
+ min: span.histogram.getMin(),
567
+ max: span.histogram.getMax(),
568
+ median: span.histogram.percentiles([0.5])[0.5]
569
+ }))
570
+ })
571
+ // push serialized into normalized data
572
+ routeCopy.variances.push(tmp)
573
+ })
574
+ // push the route into normalized data
575
+ normalized[appName].data.routes.push(routeCopy)
576
+ })
577
+ })
578
+
579
+ return normalized
580
+ }
581
+
582
+ /**
583
+ * Check if the string can be a id of some sort
584
+ *
585
+ * @param {String} id
586
+ */
587
+ isIdentifier (id) {
588
+ id = typeof (id) !== 'string' ? id + '' : id
589
+
590
+ // uuid v1/v4 with/without dash
591
+ if (id.match(/[0-9a-f]{8}-[0-9a-f]{4}-[14][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}|[0-9a-f]{12}[14][0-9a-f]{19}/i)) {
592
+ return true
593
+ // if number
594
+ } else if (id.match(/\d+/)) {
595
+ return true
596
+ // if suit of nbr/letters
597
+ } else if (id.match(/[0-9]+[a-z]+|[a-z]+[0-9]+/)) {
598
+ return true
599
+ // if match pattern with multiple char spaced by . - _ @
600
+ } else if (id.match(/((?:[0-9a-zA-Z]+[@\-_.][0-9a-zA-Z]+|[0-9a-zA-Z]+[@\-_.]|[@\-_.][0-9a-zA-Z]+)+)/)) {
601
+ return true
602
+ }
603
+ return false
604
+ }
605
+
606
+ /**
607
+ * Cleanup trace data
608
+ * - delete result(s)
609
+ * - replace labels value with a question mark
610
+ *
611
+ * @param {Object} spans list of span for a trace
612
+ */
613
+ censorSpans (spans) {
614
+ if (!spans) return log('spans is null')
615
+ if (cst.DEBUG) return
616
+
617
+ spans.forEach((span) => {
618
+ if (!span.labels) return
619
+
620
+ delete span.labels.results
621
+ delete span.labels.result
622
+ delete span.spanId
623
+ delete span.parentSpanId
624
+ delete span.labels.values
625
+
626
+ Object.keys(span.labels).forEach((key) => {
627
+ if (typeof (span.labels[key]) === 'string' && key !== 'stacktrace') {
628
+ span.labels[key] = span.labels[key].replace(this.REGEX_JSON_CLEANUP, '\": \"?\"') // eslint-disable-line
629
+ }
630
+ })
631
+ })
632
+ }
633
+
634
+ /**
635
+ * Parse stackrace of spans to extract and normalize data
636
+ *
637
+ * @param {Object} spans list of span for a trace
638
+ */
639
+ parseStacktrace (spans) {
640
+ if (!spans) return log('spans is null')
641
+
642
+ spans.forEach((span) => {
643
+ // if empty make sure that it doesnt exist
644
+ if (!span ||
645
+ !span.labels ||
646
+ !span.labels.stacktrace ||
647
+ typeof (span.labels.stacktrace) !== 'string') return
648
+
649
+ // you never know what come through that door
650
+ try {
651
+ span.labels.stacktrace = JSON.parse(span.labels.stacktrace)
652
+ } catch (e) {
653
+ return
654
+ }
655
+
656
+ if (!span.labels.stacktrace || !(span.labels.stacktrace.stack_frame instanceof Array)) return
657
+ // parse the stacktrace
658
+ let result = this.stackParser.parse(span.labels.stacktrace.stack_frame)
659
+ if (result) {
660
+ span.labels['source/file'] = result.callsite || undefined
661
+ span.labels['source/context'] = result.context || undefined
662
+ }
663
+ })
664
+
665
+ spans.forEach((span) => {
666
+ if (!span || !span.labels) return
667
+ delete span.labels.stacktrace
668
+ })
669
+ }
670
+ }