traceperf 0.2.1 → 1.3.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 (268) hide show
  1. package/README.md +608 -253
  2. package/dist/__tests__/helpers/console.d.ts +9 -0
  3. package/dist/__tests__/helpers/console.js +25 -0
  4. package/dist/__tests__/helpers/console.js.map +1 -0
  5. package/dist/auto.d.ts +30 -0
  6. package/dist/auto.js +91 -0
  7. package/dist/auto.js.map +1 -0
  8. package/dist/browser.d.ts +52 -144
  9. package/dist/browser.js +112 -439
  10. package/dist/browser.js.map +1 -1
  11. package/dist/cli.d.ts +68 -0
  12. package/dist/cli.js +528 -0
  13. package/dist/cli.js.map +1 -0
  14. package/dist/core/allocations.d.ts +38 -0
  15. package/dist/core/allocations.js +85 -0
  16. package/dist/core/allocations.js.map +1 -0
  17. package/dist/core/cpu-profile.d.ts +84 -0
  18. package/dist/core/cpu-profile.js +181 -0
  19. package/dist/core/cpu-profile.js.map +1 -0
  20. package/dist/core/env.d.ts +10 -0
  21. package/dist/core/env.js +40 -0
  22. package/dist/core/env.js.map +1 -0
  23. package/dist/core/event-loop.d.ts +30 -0
  24. package/dist/core/event-loop.js +43 -0
  25. package/dist/core/event-loop.js.map +1 -0
  26. package/dist/core/explain.d.ts +48 -0
  27. package/dist/core/explain.js +236 -0
  28. package/dist/core/explain.js.map +1 -0
  29. package/dist/core/exporters.d.ts +111 -0
  30. package/dist/core/exporters.js +236 -0
  31. package/dist/core/exporters.js.map +1 -0
  32. package/dist/core/index.d.ts +9 -3
  33. package/dist/core/index.js +9 -3
  34. package/dist/core/index.js.map +1 -1
  35. package/dist/core/optimized-performance-monitor.d.ts +180 -26
  36. package/dist/core/optimized-performance-monitor.js +687 -175
  37. package/dist/core/optimized-performance-monitor.js.map +1 -1
  38. package/dist/core/prometheus.d.ts +36 -0
  39. package/dist/core/prometheus.js +93 -0
  40. package/dist/core/prometheus.js.map +1 -0
  41. package/dist/core/records.d.ts +97 -0
  42. package/dist/core/records.js +87 -0
  43. package/dist/core/records.js.map +1 -0
  44. package/dist/core/stats.d.ts +45 -0
  45. package/dist/core/stats.js +136 -0
  46. package/dist/core/stats.js.map +1 -0
  47. package/dist/core/timeline.d.ts +28 -0
  48. package/dist/core/timeline.js +65 -0
  49. package/dist/core/timeline.js.map +1 -0
  50. package/dist/drivers.d.ts +51 -0
  51. package/dist/drivers.js +205 -0
  52. package/dist/drivers.js.map +1 -0
  53. package/dist/esm/auto.d.ts +30 -0
  54. package/dist/esm/auto.js +87 -0
  55. package/dist/esm/auto.js.map +1 -0
  56. package/dist/esm/browser.d.ts +52 -144
  57. package/dist/esm/browser.js +101 -445
  58. package/dist/esm/browser.js.map +1 -1
  59. package/dist/esm/cli.d.ts +68 -0
  60. package/dist/esm/cli.js +520 -0
  61. package/dist/esm/cli.js.map +1 -0
  62. package/dist/esm/core/allocations.d.ts +38 -0
  63. package/dist/esm/core/allocations.js +82 -0
  64. package/dist/esm/core/allocations.js.map +1 -0
  65. package/dist/esm/core/cpu-profile.d.ts +84 -0
  66. package/dist/esm/core/cpu-profile.js +176 -0
  67. package/dist/esm/core/cpu-profile.js.map +1 -0
  68. package/dist/esm/core/env.d.ts +10 -0
  69. package/dist/esm/core/env.js +35 -0
  70. package/dist/esm/core/env.js.map +1 -0
  71. package/dist/esm/core/event-loop.d.ts +30 -0
  72. package/dist/esm/core/event-loop.js +40 -0
  73. package/dist/esm/core/event-loop.js.map +1 -0
  74. package/dist/esm/core/explain.d.ts +48 -0
  75. package/dist/esm/core/explain.js +232 -0
  76. package/dist/esm/core/explain.js.map +1 -0
  77. package/dist/esm/core/exporters.d.ts +111 -0
  78. package/dist/esm/core/exporters.js +229 -0
  79. package/dist/esm/core/exporters.js.map +1 -0
  80. package/dist/esm/core/index.d.ts +10 -4
  81. package/dist/esm/core/index.js +10 -4
  82. package/dist/esm/core/index.js.map +1 -1
  83. package/dist/esm/core/optimized-performance-monitor.d.ts +180 -26
  84. package/dist/esm/core/optimized-performance-monitor.js +685 -173
  85. package/dist/esm/core/optimized-performance-monitor.js.map +1 -1
  86. package/dist/esm/core/prometheus.d.ts +36 -0
  87. package/dist/esm/core/prometheus.js +89 -0
  88. package/dist/esm/core/prometheus.js.map +1 -0
  89. package/dist/esm/core/records.d.ts +97 -0
  90. package/dist/esm/core/records.js +82 -0
  91. package/dist/esm/core/records.js.map +1 -0
  92. package/dist/esm/core/stats.d.ts +45 -0
  93. package/dist/esm/core/stats.js +131 -0
  94. package/dist/esm/core/stats.js.map +1 -0
  95. package/dist/esm/core/timeline.d.ts +28 -0
  96. package/dist/esm/core/timeline.js +62 -0
  97. package/dist/esm/core/timeline.js.map +1 -0
  98. package/dist/esm/drivers.d.ts +51 -0
  99. package/dist/esm/drivers.js +197 -0
  100. package/dist/esm/drivers.js.map +1 -0
  101. package/dist/esm/index.d.ts +13 -5
  102. package/dist/esm/index.js +21 -17
  103. package/dist/esm/index.js.map +1 -1
  104. package/dist/esm/instrument.d.ts +88 -0
  105. package/dist/esm/instrument.js +274 -0
  106. package/dist/esm/instrument.js.map +1 -0
  107. package/dist/esm/integrations.d.ts +70 -0
  108. package/dist/esm/integrations.js +197 -0
  109. package/dist/esm/integrations.js.map +1 -0
  110. package/dist/esm/node-builtins.d.ts +1 -0
  111. package/dist/esm/node-builtins.js +32 -0
  112. package/dist/esm/node-builtins.js.map +1 -0
  113. package/dist/esm/package.json +3 -0
  114. package/dist/esm/register.d.ts +20 -0
  115. package/dist/esm/register.js +98 -0
  116. package/dist/esm/register.js.map +1 -0
  117. package/dist/esm/report-html.d.ts +11 -0
  118. package/dist/esm/report-html.js +106 -0
  119. package/dist/esm/report-html.js.map +1 -0
  120. package/dist/esm/types/index.d.ts +4 -80
  121. package/dist/esm/types/index.js +1 -2
  122. package/dist/esm/types/index.js.map +1 -1
  123. package/dist/esm/types/options.d.ts +72 -0
  124. package/dist/esm/types/options.js +2 -0
  125. package/dist/esm/types/options.js.map +1 -0
  126. package/dist/esm/watch.d.ts +17 -0
  127. package/dist/esm/watch.js +87 -0
  128. package/dist/esm/watch.js.map +1 -0
  129. package/dist/index.d.ts +10 -2
  130. package/dist/index.js +26 -29
  131. package/dist/index.js.map +1 -1
  132. package/dist/instrument.d.ts +88 -0
  133. package/dist/instrument.js +282 -0
  134. package/dist/instrument.js.map +1 -0
  135. package/dist/integrations.d.ts +70 -0
  136. package/dist/integrations.js +205 -0
  137. package/dist/integrations.js.map +1 -0
  138. package/dist/node-builtins.d.ts +1 -0
  139. package/dist/node-builtins.js +67 -0
  140. package/dist/node-builtins.js.map +1 -0
  141. package/dist/register.d.ts +20 -0
  142. package/dist/register.js +103 -0
  143. package/dist/register.js.map +1 -0
  144. package/dist/report-html.d.ts +11 -0
  145. package/dist/report-html.js +109 -0
  146. package/dist/report-html.js.map +1 -0
  147. package/dist/traceperf.browser.js +1910 -0
  148. package/dist/traceperf.browser.min.js +6 -0
  149. package/dist/types/index.d.ts +4 -80
  150. package/dist/types/index.js +1 -2
  151. package/dist/types/index.js.map +1 -1
  152. package/dist/types/options.d.ts +72 -0
  153. package/dist/types/{config.js → options.js} +1 -1
  154. package/dist/types/options.js.map +1 -0
  155. package/dist/watch.d.ts +17 -0
  156. package/dist/watch.js +90 -0
  157. package/dist/watch.js.map +1 -0
  158. package/package.json +99 -19
  159. package/dist/core/config.d.ts +0 -77
  160. package/dist/core/config.js +0 -116
  161. package/dist/core/config.js.map +0 -1
  162. package/dist/core/constants.d.ts +0 -78
  163. package/dist/core/constants.js +0 -109
  164. package/dist/core/constants.js.map +0 -1
  165. package/dist/core/logger.d.ts +0 -97
  166. package/dist/core/logger.js +0 -186
  167. package/dist/core/logger.js.map +0 -1
  168. package/dist/esm/core/config.d.ts +0 -77
  169. package/dist/esm/core/config.js +0 -112
  170. package/dist/esm/core/config.js.map +0 -1
  171. package/dist/esm/core/constants.d.ts +0 -78
  172. package/dist/esm/core/constants.js +0 -106
  173. package/dist/esm/core/constants.js.map +0 -1
  174. package/dist/esm/core/logger.d.ts +0 -97
  175. package/dist/esm/core/logger.js +0 -182
  176. package/dist/esm/core/logger.js.map +0 -1
  177. package/dist/esm/formatters/ascii.d.ts +0 -52
  178. package/dist/esm/formatters/ascii.js +0 -100
  179. package/dist/esm/formatters/ascii.js.map +0 -1
  180. package/dist/esm/formatters/cli.d.ts +0 -50
  181. package/dist/esm/formatters/cli.js +0 -96
  182. package/dist/esm/formatters/cli.js.map +0 -1
  183. package/dist/esm/formatters/index.d.ts +0 -3
  184. package/dist/esm/formatters/index.js +0 -4
  185. package/dist/esm/formatters/index.js.map +0 -1
  186. package/dist/esm/formatters/json.d.ts +0 -45
  187. package/dist/esm/formatters/json.js +0 -85
  188. package/dist/esm/formatters/json.js.map +0 -1
  189. package/dist/esm/trackers/execution.d.ts +0 -101
  190. package/dist/esm/trackers/execution.js +0 -226
  191. package/dist/esm/trackers/execution.js.map +0 -1
  192. package/dist/esm/trackers/index.d.ts +0 -2
  193. package/dist/esm/trackers/index.js +0 -3
  194. package/dist/esm/trackers/index.js.map +0 -1
  195. package/dist/esm/trackers/performance.d.ts +0 -68
  196. package/dist/esm/trackers/performance.js +0 -112
  197. package/dist/esm/trackers/performance.js.map +0 -1
  198. package/dist/esm/types/config.d.ts +0 -142
  199. package/dist/esm/types/config.js +0 -2
  200. package/dist/esm/types/config.js.map +0 -1
  201. package/dist/esm/types/logger.d.ts +0 -147
  202. package/dist/esm/types/logger.js +0 -2
  203. package/dist/esm/types/logger.js.map +0 -1
  204. package/dist/esm/utils/colors.d.ts +0 -87
  205. package/dist/esm/utils/colors.js +0 -146
  206. package/dist/esm/utils/colors.js.map +0 -1
  207. package/dist/esm/utils/index.d.ts +0 -3
  208. package/dist/esm/utils/index.js +0 -4
  209. package/dist/esm/utils/index.js.map +0 -1
  210. package/dist/esm/utils/proxy-tracker.d.ts +0 -68
  211. package/dist/esm/utils/proxy-tracker.js +0 -127
  212. package/dist/esm/utils/proxy-tracker.js.map +0 -1
  213. package/dist/esm/utils/stack.d.ts +0 -63
  214. package/dist/esm/utils/stack.js +0 -110
  215. package/dist/esm/utils/stack.js.map +0 -1
  216. package/dist/esm/utils/string.d.ts +0 -29
  217. package/dist/esm/utils/string.js +0 -45
  218. package/dist/esm/utils/string.js.map +0 -1
  219. package/dist/esm/utils/timing.d.ts +0 -65
  220. package/dist/esm/utils/timing.js +0 -99
  221. package/dist/esm/utils/timing.js.map +0 -1
  222. package/dist/formatters/ascii.d.ts +0 -52
  223. package/dist/formatters/ascii.js +0 -104
  224. package/dist/formatters/ascii.js.map +0 -1
  225. package/dist/formatters/cli.d.ts +0 -50
  226. package/dist/formatters/cli.js +0 -100
  227. package/dist/formatters/cli.js.map +0 -1
  228. package/dist/formatters/index.d.ts +0 -3
  229. package/dist/formatters/index.js +0 -20
  230. package/dist/formatters/index.js.map +0 -1
  231. package/dist/formatters/json.d.ts +0 -45
  232. package/dist/formatters/json.js +0 -89
  233. package/dist/formatters/json.js.map +0 -1
  234. package/dist/trackers/execution.d.ts +0 -101
  235. package/dist/trackers/execution.js +0 -230
  236. package/dist/trackers/execution.js.map +0 -1
  237. package/dist/trackers/index.d.ts +0 -2
  238. package/dist/trackers/index.js +0 -19
  239. package/dist/trackers/index.js.map +0 -1
  240. package/dist/trackers/performance.d.ts +0 -68
  241. package/dist/trackers/performance.js +0 -116
  242. package/dist/trackers/performance.js.map +0 -1
  243. package/dist/types/config.d.ts +0 -142
  244. package/dist/types/config.js.map +0 -1
  245. package/dist/types/logger.d.ts +0 -147
  246. package/dist/types/logger.js +0 -3
  247. package/dist/types/logger.js.map +0 -1
  248. package/dist/types.d.ts +0 -65
  249. package/dist/types.js +0 -6
  250. package/dist/types.js.map +0 -1
  251. package/dist/utils/colors.d.ts +0 -87
  252. package/dist/utils/colors.js +0 -160
  253. package/dist/utils/colors.js.map +0 -1
  254. package/dist/utils/index.d.ts +0 -3
  255. package/dist/utils/index.js +0 -20
  256. package/dist/utils/index.js.map +0 -1
  257. package/dist/utils/proxy-tracker.d.ts +0 -68
  258. package/dist/utils/proxy-tracker.js +0 -131
  259. package/dist/utils/proxy-tracker.js.map +0 -1
  260. package/dist/utils/stack.d.ts +0 -63
  261. package/dist/utils/stack.js +0 -116
  262. package/dist/utils/stack.js.map +0 -1
  263. package/dist/utils/string.d.ts +0 -29
  264. package/dist/utils/string.js +0 -50
  265. package/dist/utils/string.js.map +0 -1
  266. package/dist/utils/timing.d.ts +0 -65
  267. package/dist/utils/timing.js +0 -106
  268. package/dist/utils/timing.js.map +0 -1
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Show tracked calls in the browser DevTools Performance panel (and in any
3
+ * User Timing consumer) via performance.measure().
4
+ */
5
+ import { TracePerfPlugin } from './records.js';
6
+ export interface PerformanceTimelineOptions {
7
+ /** DevTools track name (default "traceperf") */
8
+ track?: string;
9
+ /** Optional DevTools track group */
10
+ trackGroup?: string;
11
+ /**
12
+ * Keep the entries in the performance timeline (default false). DevTools
13
+ * recordings and PerformanceObservers see every entry either way; keeping
14
+ * them makes performance.getEntriesByType('measure') return them but grows
15
+ * memory in long-running pages.
16
+ */
17
+ keep?: boolean;
18
+ }
19
+ /**
20
+ * Emit a performance.measure() for every finished tracked call.
21
+ *
22
+ * tp.use(performanceTimeline());
23
+ *
24
+ * In Chrome 129+ the entries appear on a custom "traceperf" track in the
25
+ * Performance panel (errors in red, details in the tooltip); elsewhere they
26
+ * appear in the Timings track / User Timing API.
27
+ */
28
+ export declare function performanceTimeline(options?: PerformanceTimelineOptions): TracePerfPlugin;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Show tracked calls in the browser DevTools Performance panel (and in any
3
+ * User Timing consumer) via performance.measure().
4
+ */
5
+ import { walkRecords } from './records.js';
6
+ /**
7
+ * Emit a performance.measure() for every finished tracked call.
8
+ *
9
+ * tp.use(performanceTimeline());
10
+ *
11
+ * In Chrome 129+ the entries appear on a custom "traceperf" track in the
12
+ * Performance panel (errors in red, details in the tooltip); elsewhere they
13
+ * appear in the Timings track / User Timing API.
14
+ */
15
+ export function performanceTimeline(options = {}) {
16
+ var _a;
17
+ const perf = typeof performance !== 'undefined' ? performance : undefined;
18
+ const supported = !!perf && typeof perf.measure === 'function';
19
+ const track = (_a = options.track) !== null && _a !== void 0 ? _a : 'traceperf';
20
+ return {
21
+ name: 'performance-timeline',
22
+ onComplete(root) {
23
+ if (!supported)
24
+ return;
25
+ walkRecords(root, record => {
26
+ const failed = record.error !== undefined && record.error !== null;
27
+ const properties = [
28
+ ['duration', `${record.duration.toFixed(3)} ms`],
29
+ ['self', `${record.selfDuration.toFixed(3)} ms`],
30
+ ];
31
+ if (record.async)
32
+ properties.push(['sync', `${record.syncDuration.toFixed(3)} ms`]);
33
+ if (record.memoryDelta !== undefined)
34
+ properties.push(['heap Δ', `${(record.memoryDelta / 1024).toFixed(2)} KB`]);
35
+ if (failed)
36
+ properties.push(['error', record.error instanceof Error ? record.error.message : String(record.error)]);
37
+ try {
38
+ perf.measure(record.label, {
39
+ start: record.startTime,
40
+ duration: record.duration,
41
+ detail: {
42
+ devtools: {
43
+ dataType: 'track-entry',
44
+ track,
45
+ ...(options.trackGroup ? { trackGroup: options.trackGroup } : {}),
46
+ color: failed ? 'error' : 'primary',
47
+ properties,
48
+ tooltipText: `${record.label}: ${record.duration.toFixed(2)} ms`,
49
+ },
50
+ },
51
+ });
52
+ if (!options.keep && typeof perf.clearMeasures === 'function')
53
+ perf.clearMeasures(record.label);
54
+ }
55
+ catch (_a) {
56
+ // Older engines without measure(name, options): skip silently
57
+ }
58
+ });
59
+ },
60
+ };
61
+ }
62
+ //# sourceMappingURL=timeline.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timeline.js","sourceRoot":"","sources":["../../../src/core/timeline.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAoC,WAAW,EAAE,MAAM,WAAW,CAAC;AAgB1E;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,UAAsC,EAAE;;IAC1E,MAAM,IAAI,GAAQ,OAAO,WAAW,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AAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBACpH,IAAI,CAAC;oBACH,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE;wBACzB,KAAK,EAAE,MAAM,CAAC,SAAS;wBACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ;wBACzB,MAAM,EAAE;4BACN,QAAQ,EAAE;gCACR,QAAQ,EAAE,aAAa;gCACvB,KAAK;gCACL,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;gCACjE,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS;gCACnC,UAAU;gCACV,WAAW,EAAE,GAAG,MAAM,CAAC,KAAK,KAAK,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK;6BACjE;yBACF;qBACF,CAAC,CAAC;oBACH,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,OAAO,IAAI,CAAC,aAAa,KAAK,UAAU;wBAAE,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;gBAClG,CAAC;gBAAC,WAAM,CAAC;oBACP,8DAA8D;gBAChE,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Ready-made instrumentation for popular database and cloud clients.
3
+ *
4
+ * Each helper takes the library module (or client) you already import, so
5
+ * traceperf has no dependency on any of them, patches it in place, is
6
+ * idempotent, and returns an uninstall function.
7
+ */
8
+ import type { OptimizedPerformanceMonitor } from './core/optimized-performance-monitor.js';
9
+ import type { TrackOptions } from './types/options.js';
10
+ import type { Uninstall } from './instrument.js';
11
+ export type DriverOptions = Omit<TrackOptions, 'label'>;
12
+ /**
13
+ * "SELECT users", "INSERT orders", "UPDATE accounts", "BEGIN": the SQL verb
14
+ * and the first table it touches, without values (labels stay low-cardinality).
15
+ */
16
+ export declare function sqlLabel(sql: unknown): string;
17
+ /**
18
+ * node-postgres: tracks every query ("pg SELECT users"), including those
19
+ * made through Pool.query (it delegates to Client.query).
20
+ *
21
+ * instrumentPg(tp, require('pg'));
22
+ */
23
+ export declare function instrumentPg(tp: OptimizedPerformanceMonitor, pg: any, options?: DriverOptions): Uninstall;
24
+ /**
25
+ * mysql2 (callback and promise APIs, connections and pools):
26
+ *
27
+ * instrumentMysql2(tp, require('mysql2'));
28
+ */
29
+ export declare function instrumentMysql2(tp: OptimizedPerformanceMonitor, mysql2: any, options?: DriverOptions): Uninstall;
30
+ /**
31
+ * MongoDB Node driver, and therefore Mongoose: "mongo insertOne users",
32
+ * "mongo find users" (cursor reads are tracked when the results are fetched).
33
+ *
34
+ * instrumentMongo(tp, require('mongodb'));
35
+ * instrumentMongo(tp, require('mongoose').mongo); // Mongoose's bundled driver
36
+ */
37
+ export declare function instrumentMongo(tp: OptimizedPerformanceMonitor, mongodb: any, options?: DriverOptions): Uninstall;
38
+ /**
39
+ * ioredis: every command, "redis GET", "redis HSET" (pipelines and
40
+ * transactions show their individual commands).
41
+ *
42
+ * instrumentRedis(tp, require('ioredis'));
43
+ */
44
+ export declare function instrumentRedis(tp: OptimizedPerformanceMonitor, Redis: any, options?: DriverOptions): Uninstall;
45
+ /**
46
+ * AWS SDK v3 clients: "aws S3.GetObject", "aws DynamoDB.Query".
47
+ * Adds a middleware to the given client instance.
48
+ *
49
+ * instrumentAwsClient(tp, new S3Client({}));
50
+ */
51
+ export declare function instrumentAwsClient(tp: OptimizedPerformanceMonitor, client: any, options?: DriverOptions): Uninstall;
@@ -0,0 +1,197 @@
1
+ const PATCHED = Symbol.for('traceperf.instrumented');
2
+ const isPromiseLike = (value) => value !== null && (typeof value === 'object' || typeof value === 'function') && typeof value.then === 'function';
3
+ const isCommandObject = (value) => value !== null && typeof value === 'object' && !(value instanceof Promise) &&
4
+ typeof value.once === 'function' && typeof value.on === 'function' &&
5
+ typeof value.emit === 'function';
6
+ /**
7
+ * Run one driver call as a tracked span, whether it reports completion via
8
+ * a callback (last argument), a returned promise, or synchronously.
9
+ */
10
+ function trackCall(tp, label, options, thisArg, original, args, callbackIndex = args.length - 1) {
11
+ const span = tp.start(label, options);
12
+ let ended = false;
13
+ const end = (error) => {
14
+ if (ended)
15
+ return;
16
+ ended = true;
17
+ span.end(error);
18
+ };
19
+ const cb = callbackIndex >= 0 ? args[callbackIndex] : undefined;
20
+ if (typeof cb === 'function') {
21
+ const wrappedArgs = args.slice();
22
+ wrappedArgs[callbackIndex] = function (error, ...rest) {
23
+ end(error || undefined);
24
+ return cb.call(this, error, ...rest);
25
+ };
26
+ try {
27
+ return span.run(() => original.apply(thisArg, wrappedArgs));
28
+ }
29
+ catch (error) {
30
+ end(error);
31
+ throw error;
32
+ }
33
+ }
34
+ let result;
35
+ try {
36
+ result = span.run(() => original.apply(thisArg, args));
37
+ }
38
+ catch (error) {
39
+ end(error);
40
+ throw error;
41
+ }
42
+ // Command objects (mysql2 Query, pg Query) are event emitters; some, like
43
+ // mysql2's, have a .then() that throws. Finish on their completion
44
+ // callback or 'end' event instead of treating them as promises.
45
+ if (isCommandObject(result)) {
46
+ const command = result;
47
+ if (typeof command.onResult === 'function') {
48
+ const onResult = command.onResult;
49
+ command.onResult = function (error, ...rest) {
50
+ end(error || undefined);
51
+ return onResult.call(this, error, ...rest);
52
+ };
53
+ }
54
+ command.once('end', () => end());
55
+ return result;
56
+ }
57
+ if (isPromiseLike(result)) {
58
+ result.then(() => end(), error => end(error));
59
+ return result;
60
+ }
61
+ end();
62
+ return result;
63
+ }
64
+ function patchMethod(target, name, makePatched, restores) {
65
+ if (!target)
66
+ return;
67
+ const original = target[name];
68
+ if (typeof original !== 'function' || original[PATCHED])
69
+ return;
70
+ const patched = makePatched(original);
71
+ Object.defineProperty(patched, 'name', { value: original.name, configurable: true });
72
+ Object.defineProperty(patched, 'length', { value: original.length, configurable: true });
73
+ patched[PATCHED] = original;
74
+ const hadOwn = Object.prototype.hasOwnProperty.call(target, name);
75
+ target[name] = patched;
76
+ restores.push(() => {
77
+ if (target[name] !== patched)
78
+ return;
79
+ if (hadOwn)
80
+ target[name] = original;
81
+ else
82
+ delete target[name];
83
+ });
84
+ }
85
+ /**
86
+ * "SELECT users", "INSERT orders", "UPDATE accounts", "BEGIN": the SQL verb
87
+ * and the first table it touches, without values (labels stay low-cardinality).
88
+ */
89
+ export function sqlLabel(sql) {
90
+ const text = typeof sql === 'string' ? sql : sql && typeof sql.text === 'string' ? sql.text
91
+ : sql && typeof sql.sql === 'string' ? sql.sql : '';
92
+ const cleaned = text.replace(/--[^\n]*|\/\*[\s\S]*?\*\//g, ' ').trim();
93
+ const verb = (cleaned.split(/\s+/)[0] || 'query').toUpperCase();
94
+ const table = /\b(?:from|into|update|join|table)\s+("?[\w.]+"?)/i.exec(cleaned);
95
+ return table ? `${verb} ${table[1].replace(/"/g, '')}` : verb;
96
+ }
97
+ /**
98
+ * node-postgres: tracks every query ("pg SELECT users"), including those
99
+ * made through Pool.query (it delegates to Client.query).
100
+ *
101
+ * instrumentPg(tp, require('pg'));
102
+ */
103
+ export function instrumentPg(tp, pg, options = {}) {
104
+ const restores = [];
105
+ patchMethod(pg && pg.Client && pg.Client.prototype, 'query', original => function (...args) {
106
+ return trackCall(tp, `pg ${sqlLabel(args[0])}`, options, this, original, args);
107
+ }, restores);
108
+ return () => restores.forEach(r => r());
109
+ }
110
+ /**
111
+ * mysql2 (callback and promise APIs, connections and pools):
112
+ *
113
+ * instrumentMysql2(tp, require('mysql2'));
114
+ */
115
+ export function instrumentMysql2(tp, mysql2, options = {}) {
116
+ const restores = [];
117
+ const proto = mysql2 && mysql2.Connection && mysql2.Connection.prototype;
118
+ for (const method of ['query', 'execute']) {
119
+ patchMethod(proto, method, original => function (...args) {
120
+ return trackCall(tp, `mysql ${sqlLabel(args[0])}`, options, this, original, args);
121
+ }, restores);
122
+ }
123
+ return () => restores.forEach(r => r());
124
+ }
125
+ const MONGO_COLLECTION_METHODS = [
126
+ 'insertOne', 'insertMany', 'findOne', 'updateOne', 'updateMany', 'replaceOne', 'deleteOne', 'deleteMany',
127
+ 'countDocuments', 'estimatedDocumentCount', 'distinct', 'findOneAndUpdate', 'findOneAndDelete',
128
+ 'findOneAndReplace', 'bulkWrite', 'createIndex',
129
+ ];
130
+ /**
131
+ * MongoDB Node driver, and therefore Mongoose: "mongo insertOne users",
132
+ * "mongo find users" (cursor reads are tracked when the results are fetched).
133
+ *
134
+ * instrumentMongo(tp, require('mongodb'));
135
+ * instrumentMongo(tp, require('mongoose').mongo); // Mongoose's bundled driver
136
+ */
137
+ export function instrumentMongo(tp, mongodb, options = {}) {
138
+ const restores = [];
139
+ const collection = mongodb && mongodb.Collection && mongodb.Collection.prototype;
140
+ for (const method of MONGO_COLLECTION_METHODS) {
141
+ patchMethod(collection, method, original => function (...args) {
142
+ // Modern driver methods are promise-only; never treat a filter as a callback
143
+ return trackCall(tp, `mongo ${method} ${this && this.collectionName}`, options, this, original, args, -1);
144
+ }, restores);
145
+ }
146
+ const cursorName = (cursor) => {
147
+ const ns = cursor && cursor.namespace;
148
+ return (ns && (ns.collection || ns.coll)) || '';
149
+ };
150
+ for (const [className, op] of [['FindCursor', 'find'], ['AggregationCursor', 'aggregate']]) {
151
+ const proto = mongodb && mongodb[className] && mongodb[className].prototype;
152
+ patchMethod(proto, 'toArray', original => function (...args) {
153
+ return trackCall(tp, `mongo ${op} ${cursorName(this)}`, options, this, original, args, -1);
154
+ }, restores);
155
+ }
156
+ return () => restores.forEach(r => r());
157
+ }
158
+ /**
159
+ * ioredis: every command, "redis GET", "redis HSET" (pipelines and
160
+ * transactions show their individual commands).
161
+ *
162
+ * instrumentRedis(tp, require('ioredis'));
163
+ */
164
+ export function instrumentRedis(tp, Redis, options = {}) {
165
+ const restores = [];
166
+ const RedisClass = Redis && (Redis.default || Redis.Redis || Redis);
167
+ patchMethod(RedisClass && RedisClass.prototype, 'sendCommand', original => function (command, ...rest) {
168
+ const name = command && command.name ? String(command.name).toUpperCase() : 'COMMAND';
169
+ return trackCall(tp, `redis ${name}`, options, this, original, [command, ...rest], -1);
170
+ }, restores);
171
+ return () => restores.forEach(r => r());
172
+ }
173
+ /**
174
+ * AWS SDK v3 clients: "aws S3.GetObject", "aws DynamoDB.Query".
175
+ * Adds a middleware to the given client instance.
176
+ *
177
+ * instrumentAwsClient(tp, new S3Client({}));
178
+ */
179
+ export function instrumentAwsClient(tp, client, options = {}) {
180
+ const stack = client && client.middlewareStack;
181
+ if (!stack || typeof stack.add !== 'function')
182
+ return () => undefined;
183
+ const name = 'traceperfMiddleware';
184
+ if (client[PATCHED])
185
+ return () => undefined;
186
+ const service = (context) => { var _a; return String((context && context.clientName) || ((_a = client.constructor) === null || _a === void 0 ? void 0 : _a.name) || 'AWS').replace(/Client$/, ''); };
187
+ stack.add((next, context) => (args) => tp.track(() => next(args), {
188
+ ...options,
189
+ label: `aws ${service(context)}.${String((context && context.commandName) || 'Command').replace(/Command$/, '')}`,
190
+ }), { step: 'initialize', name, priority: 'high' });
191
+ client[PATCHED] = true;
192
+ return () => {
193
+ stack.remove(name);
194
+ delete client[PATCHED];
195
+ };
196
+ }
197
+ //# sourceMappingURL=drivers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drivers.js","sourceRoot":"","sources":["../../src/drivers.ts"],"names":[],"mappings":"AAWA,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAC;AAIrD,MAAM,aAAa,GAAG,CAAC,KAAc,EAAiC,EAAE,CACtE,KAAK,KAAK,IAAI,IAAI,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC,IAAI,OAAQ,KAAa,CAAC,IAAI,KAAK,UAAU,CAAC;AAE5H,MAAM,eAAe,GAAG,CAAC,KAAc,EAAW,EAAE,CAClD,KAAK,KAAK,IAAI,IAAI,OAAO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AU,CAAC,KAAI,MAAA,MAAM,CAAC,WAAW,0CAAE,IAAI,CAAA,IAAI,KAAK,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAA,EAAA,CAAC;IACtI,KAAK,CAAC,GAAG,CAAC,CAAC,IAAyC,EAAE,OAAY,EAAE,EAAE,CAAC,CAAC,IAAa,EAAE,EAAE,CACvF,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE;QACzB,GAAG,OAAO;QACV,KAAK,EAAE,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,MAAM,CAAC,CAAC,OAAO,IAAI,OAAO,CAAC,WAAW,CAAC,IAAI,SAAS,CAAC,CAAC,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,EAAE;KAClH,CAAC,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;IACtD,MAAM,CAAC,OAAO,CAAC,GAAG,IAAI,CAAC;IACvB,OAAO,GAAG,EAAE;QACV,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QACnB,OAAO,MAAM,CAAC,OAAO,CAAC,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC"}
@@ -1,9 +1,17 @@
1
- import { OptimizedPerformanceMonitor, TrackingModes } from './core/optimized-performance-monitor';
2
- import { ITracePerfConfig } from './types';
1
+ import { OptimizedPerformanceMonitor, TrackingModes } from './core/optimized-performance-monitor.js';
2
+ import { ITracePerfConfig, TrackOptions } from './types/index.js';
3
3
  declare const tracePerf: OptimizedPerformanceMonitor;
4
4
  export default tracePerf;
5
- export declare function createTracePerf(config?: Partial<ITracePerfConfig>): OptimizedPerformanceMonitor;
6
- export * from './types';
7
- export * from './core';
5
+ export declare function createTracePerf(config?: ITracePerfConfig): OptimizedPerformanceMonitor;
6
+ /**
7
+ * Method decorator bound to the default instance, see `OptimizedPerformanceMonitor.traced`.
8
+ */
9
+ export declare function traced(options?: TrackOptions): import("./core/index.js").TracedDecorator;
10
+ export * from './types/index.js';
11
+ export * from './core/index.js';
12
+ export * from './integrations.js';
13
+ export * from './instrument.js';
14
+ export * from './drivers.js';
15
+ export * from './auto.js';
8
16
  export { OptimizedPerformanceMonitor };
9
17
  export { TrackingModes as TrackingMode };
package/dist/esm/index.js CHANGED
@@ -1,28 +1,32 @@
1
1
  // Import optimized performance monitor from core
2
- import { OptimizedPerformanceMonitor, TrackingModes } from './core/optimized-performance-monitor';
3
- import * as typesExports from './types';
4
- // Create singleton instance
5
- const tracePerf = new OptimizedPerformanceMonitor();
2
+ import { OptimizedPerformanceMonitor, TrackingModes } from './core/optimized-performance-monitor.js';
3
+ // Create singleton instance, shared by the CommonJS and ESM builds of this
4
+ // major version (so `--require traceperf/auto` and `import tp from
5
+ // 'traceperf'` see the same instance and the same call trees)
6
+ const DEFAULT_KEY = Symbol.for('traceperf.default.v1');
7
+ const shared = globalThis;
8
+ const tracePerf = shared[DEFAULT_KEY] || (shared[DEFAULT_KEY] = new OptimizedPerformanceMonitor());
6
9
  // Export the singleton instance as the default export
7
10
  export default tracePerf;
8
11
  // Export a function to create a new tracePerf instance
9
12
  export function createTracePerf(config = {}) {
10
13
  return new OptimizedPerformanceMonitor(config);
11
14
  }
15
+ /**
16
+ * Method decorator bound to the default instance, see `OptimizedPerformanceMonitor.traced`.
17
+ */
18
+ export function traced(options = {}) {
19
+ // Call the prototype method: in CommonJS this function is also installed
20
+ // as tracePerf.traced, so tracePerf.traced(...) would recurse.
21
+ return OptimizedPerformanceMonitor.prototype.traced.call(tracePerf, options);
22
+ }
12
23
  // Export types and classes for advanced usage
13
- export * from './types';
14
- export * from './core';
24
+ export * from './types/index.js';
25
+ export * from './core/index.js';
26
+ export * from './integrations.js';
27
+ export * from './instrument.js';
28
+ export * from './drivers.js';
29
+ export * from './auto.js';
15
30
  export { OptimizedPerformanceMonitor };
16
31
  export { TrackingModes as TrackingMode };
17
- // Add CommonJS compatibility
18
- if (typeof module !== 'undefined' && module.exports) {
19
- module.exports = Object.assign(tracePerf, {
20
- default: tracePerf,
21
- createTracePerf,
22
- OptimizedPerformanceMonitor,
23
- TrackingMode: TrackingModes,
24
- // Re-export types
25
- ...typesExports
26
- });
27
- }
28
32
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,EAAE,2BAA2B,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAElG,OAAO,KAAK,YAAY,MAAM,SAAS,CAAC;AAExC,4BAA4B;AAC5B,MAAM,SAAS,GAAG,IAAI,2BAA2B,EAAE,CAAC;AAEpD,sDAAsD;AACtD,eAAe,SAAS,CAAC;AAEzB,uDAAuD;AACvD,MAAM,UAAU,eAAe,CAAC,SAAoC,EAAE;IACpE,OAAO,IAAI,2BAA2B,CAAC,MAAM,CAAC,CAAC;AACjD,CAAC;AAED,8CAA8C;AAC9C,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,OAAO,EAAE,2BAA2B,EAAE,CAAC;AACvC,OAAO,EAAE,aAAa,IAAI,YAAY,EAAE,CAAC;AAEzC,6BAA6B;AAC7B,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;IACpD,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE;QACxC,OAAO,EAAE,SAAS;QAClB,eAAe;QACf,2BAA2B;QAC3B,YAAY,EAAE,aAAa;QAC3B,kBAAkB;QAClB,GAAG,YAAY;KAChB,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,EAAE,2BAA2B,EAAE,aAAa,EAAE,MAAM,sCAAsC,CAAC;AAGlG,2EAA2E;AAC3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@@
1
+ /**
2
+ * Auto-instrumentation: track common I/O without wrapping each call by hand.
3
+ *
4
+ * Every instrument* function patches in place, is idempotent, and returns a
5
+ * function that removes the patch. Calls made inside a tracked call nest
6
+ * under it like any other tracked call.
7
+ */
8
+ import type { OptimizedPerformanceMonitor } from './core/optimized-performance-monitor.js';
9
+ import type { TrackOptions } from './types/options.js';
10
+ export interface InstrumentOptions extends Omit<TrackOptions, 'label'> {
11
+ /** Treat HTTP responses with status >= this as failed (default 500) */
12
+ errorStatus?: number;
13
+ }
14
+ /** Removes an instrumentation patch */
15
+ export type Uninstall = () => void;
16
+ /**
17
+ * Track every `fetch()` call: label "fetch GET api.example.com/users".
18
+ * Timing covers the request until the response headers arrive (reading the
19
+ * body is not included). Responses with status >= 500 are marked failed.
20
+ */
21
+ export declare function instrumentFetch(tp: OptimizedPerformanceMonitor, options?: InstrumentOptions): Uninstall;
22
+ /**
23
+ * Track outgoing Node.js http/https requests (`request` and `get`), which
24
+ * covers clients built on them such as axios and got. Label:
25
+ * "http GET api.example.com/users". Timing covers the full response
26
+ * (until the body has been read). Node.js only.
27
+ */
28
+ export declare function instrumentHttp(tp: OptimizedPerformanceMonitor, options?: InstrumentOptions): Uninstall;
29
+ export interface InstrumentMethodsOptions extends TrackOptions {
30
+ /** Label prefix, e.g. "pg." (default: the constructor name of `target` plus ".") */
31
+ prefix?: string;
32
+ /** Compute the label per call, e.g. from the SQL text */
33
+ labelFor?: (method: string, args: unknown[]) => string;
34
+ }
35
+ /**
36
+ * Patch methods of an object or prototype in place, e.g. a database client:
37
+ *
38
+ * instrumentMethods(tp, pg.Client.prototype, ['query'], {
39
+ * labelFor: (_m, [q]) => `pg ${String(q?.text ?? q).trim().split(/\s+/)[0]}`,
40
+ * });
41
+ *
42
+ * `methods` defaults to every own function property except the constructor.
43
+ */
44
+ export declare function instrumentMethods(tp: OptimizedPerformanceMonitor, target: object, methods?: string[], options?: InstrumentMethodsOptions): Uninstall;
45
+ /**
46
+ * Prisma client extension: every model operation becomes a tracked call
47
+ * labelled "prisma.User.findMany".
48
+ *
49
+ * const prisma = new PrismaClient().$extends(prismaExtension(tp));
50
+ */
51
+ export declare function prismaExtension(tp: OptimizedPerformanceMonitor, options?: TrackOptions): {
52
+ name: string;
53
+ query: {
54
+ $allModels: {
55
+ $allOperations(params: {
56
+ model?: string;
57
+ operation: string;
58
+ args: unknown;
59
+ query: (args: unknown) => Promise<unknown>;
60
+ }): Promise<unknown>;
61
+ };
62
+ };
63
+ };
64
+ /**
65
+ * Collapse ids in a URL path so labels stay few: /users/42/orders/9f1c... ->
66
+ * /users/:id/orders/:id. Numbers, UUIDs, long hex and long mixed tokens count as ids.
67
+ */
68
+ export declare function normalizePath(url: string): string;
69
+ export interface HttpServerInstrumentOptions extends InstrumentOptions {
70
+ /** Label for a request (default "GET /users/:id", ids collapsed by normalizePath) */
71
+ labelFor?: (req: {
72
+ method?: string;
73
+ url?: string;
74
+ headers?: Record<string, unknown>;
75
+ }) => string;
76
+ /** Skip requests for which this returns true, e.g. health checks */
77
+ ignore?: (req: {
78
+ method?: string;
79
+ url?: string;
80
+ }) => boolean;
81
+ }
82
+ /**
83
+ * Track every incoming request of every Node http/https server as a
84
+ * top-level call, so work done while handling it (queries, fetches, tracked
85
+ * functions) nests under it, with no framework middleware. Responses with
86
+ * status >= 500 and connections closed before a response count as failed.
87
+ */
88
+ export declare function instrumentHttpServer(tp: OptimizedPerformanceMonitor, options?: HttpServerInstrumentOptions): Uninstall;