@datadog/pprof 6.0.0-pre-fdd7f20 → 6.0.0-pre-90fbe2b

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 (146) hide show
  1. package/README.md +2 -6
  2. package/out/src/async-context-frame.d.ts +48 -0
  3. package/out/src/async-context-frame.js +106 -0
  4. package/out/src/async-context-frame.js.map +1 -0
  5. package/out/src/heap-profiler-bindings.d.ts +4 -3
  6. package/out/src/heap-profiler-bindings.js +8 -4
  7. package/out/src/heap-profiler-bindings.js.map +1 -1
  8. package/out/src/heap-profiler.d.ts +48 -21
  9. package/out/src/heap-profiler.js +103 -31
  10. package/out/src/heap-profiler.js.map +1 -1
  11. package/out/src/index.d.ts +10 -0
  12. package/out/src/index.js +14 -1
  13. package/out/src/index.js.map +1 -1
  14. package/out/src/otel-thread-ctx.d.ts +109 -0
  15. package/out/src/otel-thread-ctx.js +166 -0
  16. package/out/src/otel-thread-ctx.js.map +1 -0
  17. package/out/src/profile-serializer.d.ts +26 -3
  18. package/out/src/profile-serializer.js +110 -26
  19. package/out/src/profile-serializer.js.map +1 -1
  20. package/out/src/sourcemapper/sourcemapper.d.ts +38 -7
  21. package/out/src/sourcemapper/sourcemapper.js +265 -75
  22. package/out/src/sourcemapper/sourcemapper.js.map +1 -1
  23. package/out/src/time-profiler.d.ts +21 -3
  24. package/out/src/time-profiler.js +63 -20
  25. package/out/src/time-profiler.js.map +1 -1
  26. package/out/src/v8-types.d.ts +12 -0
  27. package/package.json +44 -23
  28. package/prebuilds/darwin-arm64/dd_pprof.node.abi108.node +0 -0
  29. package/prebuilds/darwin-arm64/dd_pprof.node.abi111.node +0 -0
  30. package/prebuilds/darwin-arm64/dd_pprof.node.abi115.node +0 -0
  31. package/prebuilds/darwin-arm64/dd_pprof.node.abi120.node +0 -0
  32. package/prebuilds/darwin-arm64/dd_pprof.node.abi127.node +0 -0
  33. package/prebuilds/darwin-arm64/dd_pprof.node.abi131.node +0 -0
  34. package/prebuilds/darwin-arm64/dd_pprof.node.abi137.node +0 -0
  35. package/prebuilds/darwin-arm64/dd_pprof.node.abi141.node +0 -0
  36. package/prebuilds/darwin-arm64/dd_pprof.node.abi147.node +0 -0
  37. package/prebuilds/darwin-x64/dd_pprof.node.abi108.node +0 -0
  38. package/prebuilds/darwin-x64/dd_pprof.node.abi111.node +0 -0
  39. package/prebuilds/darwin-x64/dd_pprof.node.abi115.node +0 -0
  40. package/prebuilds/darwin-x64/dd_pprof.node.abi120.node +0 -0
  41. package/prebuilds/darwin-x64/dd_pprof.node.abi127.node +0 -0
  42. package/prebuilds/darwin-x64/dd_pprof.node.abi131.node +0 -0
  43. package/prebuilds/darwin-x64/dd_pprof.node.abi137.node +0 -0
  44. package/prebuilds/darwin-x64/dd_pprof.node.abi141.node +0 -0
  45. package/prebuilds/darwin-x64/dd_pprof.node.abi147.node +0 -0
  46. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi108.node +0 -0
  47. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi111.node +0 -0
  48. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi115.node +0 -0
  49. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi120.node +0 -0
  50. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi127.node +0 -0
  51. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi131.node +0 -0
  52. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi137.node +0 -0
  53. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi141.node +0 -0
  54. package/prebuilds/linux-arm64/dd_pprof.musl.node.abi147.node +0 -0
  55. package/prebuilds/linux-arm64/dd_pprof.node.abi108.node +0 -0
  56. package/prebuilds/linux-arm64/dd_pprof.node.abi111.node +0 -0
  57. package/prebuilds/linux-arm64/dd_pprof.node.abi115.node +0 -0
  58. package/prebuilds/linux-arm64/dd_pprof.node.abi120.node +0 -0
  59. package/prebuilds/linux-arm64/dd_pprof.node.abi127.node +0 -0
  60. package/prebuilds/linux-arm64/dd_pprof.node.abi131.node +0 -0
  61. package/prebuilds/linux-arm64/dd_pprof.node.abi137.node +0 -0
  62. package/prebuilds/linux-arm64/dd_pprof.node.abi141.node +0 -0
  63. package/prebuilds/linux-arm64/dd_pprof.node.abi147.node +0 -0
  64. package/prebuilds/linux-x64/dd_pprof.musl.node.abi108.node +0 -0
  65. package/prebuilds/linux-x64/dd_pprof.musl.node.abi111.node +0 -0
  66. package/prebuilds/linux-x64/dd_pprof.musl.node.abi115.node +0 -0
  67. package/prebuilds/linux-x64/dd_pprof.musl.node.abi120.node +0 -0
  68. package/prebuilds/linux-x64/dd_pprof.musl.node.abi127.node +0 -0
  69. package/prebuilds/linux-x64/dd_pprof.musl.node.abi131.node +0 -0
  70. package/prebuilds/linux-x64/dd_pprof.musl.node.abi137.node +0 -0
  71. package/prebuilds/linux-x64/dd_pprof.musl.node.abi141.node +0 -0
  72. package/prebuilds/linux-x64/dd_pprof.musl.node.abi147.node +0 -0
  73. package/prebuilds/linux-x64/dd_pprof.node.abi108.node +0 -0
  74. package/prebuilds/linux-x64/dd_pprof.node.abi111.node +0 -0
  75. package/prebuilds/linux-x64/dd_pprof.node.abi115.node +0 -0
  76. package/prebuilds/linux-x64/dd_pprof.node.abi120.node +0 -0
  77. package/prebuilds/linux-x64/dd_pprof.node.abi127.node +0 -0
  78. package/prebuilds/linux-x64/dd_pprof.node.abi131.node +0 -0
  79. package/prebuilds/linux-x64/dd_pprof.node.abi137.node +0 -0
  80. package/prebuilds/linux-x64/dd_pprof.node.abi141.node +0 -0
  81. package/prebuilds/linux-x64/dd_pprof.node.abi147.node +0 -0
  82. package/prebuilds/win32-x64/dd_pprof.node.abi108.node +0 -0
  83. package/prebuilds/win32-x64/dd_pprof.node.abi111.node +0 -0
  84. package/prebuilds/win32-x64/dd_pprof.node.abi115.node +0 -0
  85. package/prebuilds/win32-x64/dd_pprof.node.abi120.node +0 -0
  86. package/prebuilds/win32-x64/dd_pprof.node.abi127.node +0 -0
  87. package/prebuilds/win32-x64/dd_pprof.node.abi131.node +0 -0
  88. package/prebuilds/win32-x64/dd_pprof.node.abi137.node +0 -0
  89. package/prebuilds/win32-x64/dd_pprof.node.abi141.node +0 -0
  90. package/prebuilds/win32-x64/dd_pprof.node.abi147.node +0 -0
  91. package/prebuilds/darwin-arm64/node-108.node +0 -0
  92. package/prebuilds/darwin-arm64/node-111.node +0 -0
  93. package/prebuilds/darwin-arm64/node-115.node +0 -0
  94. package/prebuilds/darwin-arm64/node-120.node +0 -0
  95. package/prebuilds/darwin-arm64/node-127.node +0 -0
  96. package/prebuilds/darwin-arm64/node-131.node +0 -0
  97. package/prebuilds/darwin-arm64/node-137.node +0 -0
  98. package/prebuilds/darwin-arm64/node-141.node +0 -0
  99. package/prebuilds/darwin-x64/node-108.node +0 -0
  100. package/prebuilds/darwin-x64/node-111.node +0 -0
  101. package/prebuilds/darwin-x64/node-115.node +0 -0
  102. package/prebuilds/darwin-x64/node-120.node +0 -0
  103. package/prebuilds/darwin-x64/node-127.node +0 -0
  104. package/prebuilds/darwin-x64/node-131.node +0 -0
  105. package/prebuilds/darwin-x64/node-137.node +0 -0
  106. package/prebuilds/darwin-x64/node-141.node +0 -0
  107. package/prebuilds/linuxglibc-arm64/node-108.node +0 -0
  108. package/prebuilds/linuxglibc-arm64/node-111.node +0 -0
  109. package/prebuilds/linuxglibc-arm64/node-115.node +0 -0
  110. package/prebuilds/linuxglibc-arm64/node-120.node +0 -0
  111. package/prebuilds/linuxglibc-arm64/node-127.node +0 -0
  112. package/prebuilds/linuxglibc-arm64/node-131.node +0 -0
  113. package/prebuilds/linuxglibc-arm64/node-137.node +0 -0
  114. package/prebuilds/linuxglibc-arm64/node-141.node +0 -0
  115. package/prebuilds/linuxglibc-x64/node-108.node +0 -0
  116. package/prebuilds/linuxglibc-x64/node-111.node +0 -0
  117. package/prebuilds/linuxglibc-x64/node-115.node +0 -0
  118. package/prebuilds/linuxglibc-x64/node-120.node +0 -0
  119. package/prebuilds/linuxglibc-x64/node-127.node +0 -0
  120. package/prebuilds/linuxglibc-x64/node-131.node +0 -0
  121. package/prebuilds/linuxglibc-x64/node-137.node +0 -0
  122. package/prebuilds/linuxglibc-x64/node-141.node +0 -0
  123. package/prebuilds/linuxmusl-arm64/node-108.node +0 -0
  124. package/prebuilds/linuxmusl-arm64/node-111.node +0 -0
  125. package/prebuilds/linuxmusl-arm64/node-115.node +0 -0
  126. package/prebuilds/linuxmusl-arm64/node-120.node +0 -0
  127. package/prebuilds/linuxmusl-arm64/node-127.node +0 -0
  128. package/prebuilds/linuxmusl-arm64/node-131.node +0 -0
  129. package/prebuilds/linuxmusl-arm64/node-137.node +0 -0
  130. package/prebuilds/linuxmusl-arm64/node-141.node +0 -0
  131. package/prebuilds/linuxmusl-x64/node-108.node +0 -0
  132. package/prebuilds/linuxmusl-x64/node-111.node +0 -0
  133. package/prebuilds/linuxmusl-x64/node-115.node +0 -0
  134. package/prebuilds/linuxmusl-x64/node-120.node +0 -0
  135. package/prebuilds/linuxmusl-x64/node-127.node +0 -0
  136. package/prebuilds/linuxmusl-x64/node-131.node +0 -0
  137. package/prebuilds/linuxmusl-x64/node-137.node +0 -0
  138. package/prebuilds/linuxmusl-x64/node-141.node +0 -0
  139. package/prebuilds/win32-x64/node-108.node +0 -0
  140. package/prebuilds/win32-x64/node-111.node +0 -0
  141. package/prebuilds/win32-x64/node-115.node +0 -0
  142. package/prebuilds/win32-x64/node-120.node +0 -0
  143. package/prebuilds/win32-x64/node-127.node +0 -0
  144. package/prebuilds/win32-x64/node-131.node +0 -0
  145. package/prebuilds/win32-x64/node-137.node +0 -0
  146. package/prebuilds/win32-x64/node-141.node +0 -0
package/out/src/index.js CHANGED
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
33
33
  };
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
- exports.heap = exports.time = exports.getNativeThreadId = exports.setLogger = exports.SourceMapper = exports.encodeSync = exports.encode = void 0;
36
+ exports.otelThreadCtx = exports.heap = exports.time = exports.getNativeThreadId = exports.setLogger = exports.SourceMapper = exports.encodeSync = exports.encode = void 0;
37
37
  /**
38
38
  * Copyright 2019 Google Inc. All Rights Reserved.
39
39
  *
@@ -51,6 +51,7 @@ exports.heap = exports.time = exports.getNativeThreadId = exports.setLogger = ex
51
51
  */
52
52
  const fs_1 = require("fs");
53
53
  const heapProfiler = __importStar(require("./heap-profiler"));
54
+ const otelThreadCtxModule = __importStar(require("./otel-thread-ctx"));
54
55
  const profile_encoder_1 = require("./profile-encoder");
55
56
  const timeProfiler = __importStar(require("./time-profiler"));
56
57
  var profile_encoder_2 = require("./profile-encoder");
@@ -66,6 +67,7 @@ exports.time = {
66
67
  profile: timeProfiler.profile,
67
68
  start: timeProfiler.start,
68
69
  stop: timeProfiler.stop,
70
+ profileV2: timeProfiler.profileV2,
69
71
  getContext: timeProfiler.getContext,
70
72
  setContext: timeProfiler.setContext,
71
73
  runWithContext: timeProfiler.runWithContext,
@@ -79,11 +81,22 @@ exports.heap = {
79
81
  start: heapProfiler.start,
80
82
  stop: heapProfiler.stop,
81
83
  profile: heapProfiler.profile,
84
+ profileV2: heapProfiler.profileV2,
82
85
  convertProfile: heapProfiler.convertProfile,
83
86
  v8Profile: heapProfiler.v8Profile,
84
87
  monitorOutOfMemory: heapProfiler.monitorOutOfMemory,
85
88
  CallbackMode: heapProfiler.CallbackMode,
86
89
  };
90
+ // Writer for the OpenTelemetry Thread Local Context Record (OTEP-4947).
91
+ // Linux + AsyncContextFrame (Node 22 with --experimental-async-context-frame,
92
+ // Node 24+ by default) only; degrades to no-ops on other platforms / Node
93
+ // versions.
94
+ exports.otelThreadCtx = {
95
+ ThreadContext: otelThreadCtxModule.ThreadContext,
96
+ getContext: otelThreadCtxModule.getContext,
97
+ clearContext: otelThreadCtxModule.clearContext,
98
+ getProcessContextAttributes: otelThreadCtxModule.getProcessContextAttributes,
99
+ };
87
100
  // If loaded with --require, start profiling.
88
101
  if (module.parent && module.parent.id === 'internal/preload') {
89
102
  exports.time.start({});
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../ts/src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;;;;GAcG;AACH,2BAAiC;AAEjC,8DAAgD;AAChD,uDAA6C;AAC7C,8DAAgD;AAQhD,qDAAqD;AAA7C,yGAAA,MAAM,OAAA;AAAE,6GAAA,UAAU,OAAA;AAC1B,4DAAyD;AAAjD,4GAAA,YAAY,OAAA;AACpB,mCAAmC;AAA3B,mGAAA,SAAS,OAAA;AACjB,iDAAkD;AAA1C,kHAAA,iBAAiB,OAAA;AAEZ,QAAA,IAAI,GAAG;IAClB,OAAO,EAAE,YAAY,CAAC,OAAO;IAC7B,KAAK,EAAE,YAAY,CAAC,KAAK;IACzB,IAAI,EAAE,YAAY,CAAC,IAAI;IACvB,UAAU,EAAE,YAAY,CAAC,UAAU;IACnC,UAAU,EAAE,YAAY,CAAC,UAAU;IACnC,cAAc,EAAE,YAAY,CAAC,cAAc;IAC3C,SAAS,EAAE,YAAY,CAAC,SAAS;IACjC,gCAAgC,EAC9B,YAAY,CAAC,gCAAgC;IAC/C,QAAQ,EAAE,YAAY,CAAC,QAAQ;IAC/B,UAAU,EAAE,YAAY,CAAC,UAAU;IACnC,SAAS,EAAE,YAAY,CAAC,SAAS;CAClC,CAAC;AAEW,QAAA,IAAI,GAAG;IAClB,KAAK,EAAE,YAAY,CAAC,KAAK;IACzB,IAAI,EAAE,YAAY,CAAC,IAAI;IACvB,OAAO,EAAE,YAAY,CAAC,OAAO;IAC7B,cAAc,EAAE,YAAY,CAAC,cAAc;IAC3C,SAAS,EAAE,YAAY,CAAC,SAAS;IACjC,kBAAkB,EAAE,YAAY,CAAC,kBAAkB;IACnD,YAAY,EAAE,YAAY,CAAC,YAAY;CACxC,CAAC;AAEF,6CAA6C;AAC7C,IAAI,MAAM,CAAC,MAAM,IAAI,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the application
4
+ * hands to its OTEP-4719 process-context publisher.
5
+ */
6
+ export interface ProcessContextAttributes {
7
+ readonly 'threadlocal.schema_version': 'nodejs_v1_dev';
8
+ readonly 'threadlocal.attribute_key_map': readonly string[];
9
+ readonly 'threadlocal.js_object_record_offset': number;
10
+ readonly 'threadlocal.tagged_size': number;
11
+ readonly 'threadlocal.js_map_table_offset': number;
12
+ readonly 'threadlocal.ordered_hash_map_header_size': number;
13
+ }
14
+ /**
15
+ * A thread-context record. Construct with `new ThreadContext(...)`; install
16
+ * via the {@link enter} or {@link run} instance methods. The underlying
17
+ * native record is GC-owned: when no JS or async-context-frame reference
18
+ * survives, it's freed.
19
+ *
20
+ * `appendAttributes` mutates the context's record in place. Because every
21
+ * async-context frame that holds the same `ThreadContext` reference observes
22
+ * the same native record, an append is visible across all those frames even
23
+ * when the reallocate path runs (the record is re-published on the same JS
24
+ * object, which is never replaced).
25
+ */
26
+ export interface ThreadContext {
27
+ appendAttributes(attributes: Array<string | null | undefined> | undefined): void;
28
+ /**
29
+ * Mark this context's underlying record `valid` byte as 0 in place.
30
+ * Every async-context frame that still holds this `ThreadContext`
31
+ * reference (including those that inherited it verbatim from a
32
+ * parent frame) will subsequently present a record with `valid = 0`
33
+ * to a reader, so this one call drops the record out of scope for
34
+ * every such frame at once. Intended for the span-finish path, where
35
+ * clearing only the current frame's context via {@link clearContext}
36
+ * would leave sibling and detached-continuation frames still exposing
37
+ * the finished span's trace / span IDs. Idempotent.
38
+ */
39
+ invalidate(): void;
40
+ /**
41
+ * Overwrite this context's W3C trace-flags byte in place, for the case
42
+ * where the flags are not yet known when the context is built — an SDK
43
+ * whose sampling decision is deferred learns the sampled bit later, and a
44
+ * decision already taken can still be overridden. Like {@link invalidate},
45
+ * the write is seen at once by every async-context frame holding this
46
+ * context, because they all share one record.
47
+ *
48
+ * Must be an integer in 0..255. Bits beyond those W3C currently defines are
49
+ * stored as given rather than masked off, since W3C requires unknown flag
50
+ * bits to be propagated.
51
+ */
52
+ setTraceFlags(traceFlags: number): void;
53
+ isTruncated(): boolean;
54
+ /** Debug-only: returns the on-the-wire record bytes. Not stable. */
55
+ debugBytes(): Uint8Array;
56
+ /**
57
+ * Attach this context to the current async-context frame (and every
58
+ * frame derived from it until the frame ends or {@link clearContext}
59
+ * detaches it). Re-installing the same context reference is cheap (no
60
+ * allocation); per-span caching of the context on the caller side is
61
+ * the intended usage pattern.
62
+ *
63
+ * On non-Linux platforms this is a no-op.
64
+ */
65
+ enter(): void;
66
+ /**
67
+ * Attach this context for the duration of `fn`. Equivalent to
68
+ * `als.run(this, fn)` — after `fn` returns, the previous context is
69
+ * restored. Returns whatever `fn` returns; if `fn` returns a Promise,
70
+ * the same Promise is propagated. On non-Linux platforms simply
71
+ * invokes `fn`.
72
+ */
73
+ run<T>(fn: () => T): T;
74
+ }
75
+ /**
76
+ * Constructor for {@link ThreadContext}. On non-Linux platforms, returns a
77
+ * no-op instance whose methods do nothing — the OTEP-4947 reader
78
+ * contract is ELF-TLSDESC, only meaningful on Linux.
79
+ */
80
+ export interface ThreadContextCtor {
81
+ new (traceId: Uint8Array, spanId: Uint8Array, traceFlags?: number, attributes?: Array<string | null | undefined>): ThreadContext;
82
+ readonly prototype: ThreadContext;
83
+ }
84
+ /** {@inheritDoc ThreadContextCtor} */
85
+ export declare let ThreadContext: ThreadContextCtor;
86
+ /**
87
+ * Returns the {@link ThreadContext} currently attached to the active
88
+ * async-context frame, or `undefined` if none is.
89
+ */
90
+ export declare let getContext: () => ThreadContext | undefined;
91
+ /**
92
+ * Detach any {@link ThreadContext} from the current async-context frame.
93
+ * Idempotent when no context is attached. On non-Linux platforms this is
94
+ * a no-op.
95
+ */
96
+ export declare let clearContext: () => void;
97
+ export declare let _currentRecordBytes: () => Uint8Array | undefined;
98
+ /**
99
+ * Returns the OTEP-4719 process-context attributes the caller should
100
+ * publish so an out-of-process reader can decode the on-the-wire uint8
101
+ * key indexes back to attribute names. The supplied `keys` array is the
102
+ * same string list the caller writes into the positional `attributes`
103
+ * argument of {@link ThreadContext}: index N here is the uint8 key index
104
+ * N in each record.
105
+ *
106
+ * `keys` is validated: must be a string array of length ≤ 256 with no
107
+ * duplicates.
108
+ */
109
+ export declare function getProcessContextAttributes(keys: string[]): ProcessContextAttributes;
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2026 Datadog, Inc
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports._currentRecordBytes = exports.clearContext = exports.getContext = exports.ThreadContext = void 0;
19
+ exports.getProcessContextAttributes = getProcessContextAttributes;
20
+ // Vendored from https://github.com/polarsignals/custom-labels/tree/otel-thread-ctx-wip/js/
21
+ // (originally js/index.js + js/index.d.ts, merged into TypeScript). Kept
22
+ // as a near-verbatim copy: edits should ideally land upstream first and
23
+ // be ported here, so the two stay in sync. We plan to drop this vendored
24
+ // copy once the upstream package is suitable to depend on directly.
25
+ //
26
+ // Known divergence from upstream: AsyncContextFrame availability is
27
+ // feature-detected via ./async-context-frame instead of being inferred from
28
+ // `process.execArgv`, which is wrong in both directions — see that module. Keep
29
+ // the divergence across re-syncs until upstream does the same.
30
+ // Node.js writer for the OpenTelemetry Thread Local Context Record
31
+ // (OTEP-4947), discoverable from an out-of-process reader via the
32
+ // `otel_thread_ctx_nodejs_v1` thread-local symbol exported by
33
+ // `dd_pprof.node`.
34
+ //
35
+ // Linux only; on other platforms the exported functions degrade to no-ops.
36
+ const path_1 = require("path");
37
+ const node_async_hooks_1 = require("node:async_hooks");
38
+ const async_context_frame_1 = require("./async-context-frame");
39
+ const SCHEMA_VERSION = 'nodejs_v1_dev';
40
+ // V8 layout constants the addon captured from the V8 headers Node bundles.
41
+ // On non-Linux these fall back to values matching Node's standard build
42
+ // (no V8 pointer compression, no sandbox); the reader is Linux-only per
43
+ // the OTEP anyway, so the fallbacks just keep processContextAttributes
44
+ // consistent in shape.
45
+ let JS_OBJECT_RECORD_OFFSET = 24;
46
+ let TAGGED_SIZE = 8;
47
+ let JS_MAP_TABLE_OFFSET = 0x18;
48
+ let ORDERED_HASH_MAP_HEADER_SIZE = 0x10;
49
+ // Debug accessor (not part of the stable API; for tests / reader dev).
50
+ let _currentRecordBytes = () => undefined;
51
+ exports._currentRecordBytes = _currentRecordBytes;
52
+ if (process.platform === 'linux') {
53
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
54
+ const findBinding = require('node-gyp-build');
55
+ const addon = findBinding((0, path_1.join)(__dirname, '..', '..'));
56
+ JS_OBJECT_RECORD_OFFSET = addon.otelThreadCtxJsObjectRecordOffset;
57
+ TAGGED_SIZE = addon.otelThreadCtxTaggedSize;
58
+ JS_MAP_TABLE_OFFSET = addon.otelThreadCtxJsMapTableOffset;
59
+ ORDERED_HASH_MAP_HEADER_SIZE = addon.otelThreadCtxOrderedHashMapHeaderSize;
60
+ exports.ThreadContext = addon.threadContext;
61
+ let als;
62
+ function ensureHook() {
63
+ if (als)
64
+ return als;
65
+ if (!(0, async_context_frame_1.isAsyncContextFrameActive)()) {
66
+ throw new Error('otel thread-ctx writer requires async_context_frame support, which is ' +
67
+ `unavailable: ${(0, async_context_frame_1.asyncContextFrameHint)()}.`);
68
+ }
69
+ als = new node_async_hooks_1.AsyncLocalStorage();
70
+ addon.otelThreadCtxStoreAls(als);
71
+ return als;
72
+ }
73
+ exports.getContext = function () {
74
+ return als ? als.getStore() : undefined;
75
+ };
76
+ // Idempotent: clearing when the hook hasn't been installed (no prior
77
+ // enter / run on a ThreadContext) is a no-op.
78
+ exports.clearContext = function () {
79
+ if (!als)
80
+ return;
81
+ als.enterWith(undefined);
82
+ };
83
+ // Install the active-context channel on the ThreadContext prototype so
84
+ // the only way to push a ThreadContext into our AsyncLocalStorage is
85
+ // via the context itself — callers can't poison the ALS with an
86
+ // arbitrary object.
87
+ exports.ThreadContext.prototype.enter = function () {
88
+ ensureHook().enterWith(this);
89
+ };
90
+ exports.ThreadContext.prototype.run = function (fn) {
91
+ return ensureHook().run(this, fn);
92
+ };
93
+ exports._currentRecordBytes = function () {
94
+ if (!als)
95
+ return undefined;
96
+ const context = als.getStore();
97
+ return context ? context.debugBytes() : undefined;
98
+ };
99
+ }
100
+ else {
101
+ // Non-Linux degradation. The writer's reader contract is ELF-TLSDESC,
102
+ // meaningful only on Linux; on other platforms we still want the API
103
+ // to be callable so consumers don't have to gate every call site —
104
+ // construction succeeds but produces an inert context, and the
105
+ // enter/run/clearContext entry points don't wire anything into
106
+ // AsyncLocalStorage.
107
+ class NoopThreadContext {
108
+ appendAttributes() { }
109
+ invalidate() { }
110
+ setTraceFlags() { }
111
+ isTruncated() {
112
+ return false;
113
+ }
114
+ debugBytes() {
115
+ return new Uint8Array(0);
116
+ }
117
+ enter() { }
118
+ run(fn) {
119
+ return fn();
120
+ }
121
+ }
122
+ exports.ThreadContext = NoopThreadContext;
123
+ exports.getContext = function () {
124
+ return undefined;
125
+ };
126
+ exports.clearContext = function () { };
127
+ }
128
+ /**
129
+ * Returns the OTEP-4719 process-context attributes the caller should
130
+ * publish so an out-of-process reader can decode the on-the-wire uint8
131
+ * key indexes back to attribute names. The supplied `keys` array is the
132
+ * same string list the caller writes into the positional `attributes`
133
+ * argument of {@link ThreadContext}: index N here is the uint8 key index
134
+ * N in each record.
135
+ *
136
+ * `keys` is validated: must be a string array of length ≤ 256 with no
137
+ * duplicates.
138
+ */
139
+ function getProcessContextAttributes(keys) {
140
+ if (!Array.isArray(keys)) {
141
+ throw new TypeError('keys must be an array of attribute names');
142
+ }
143
+ if (keys.length > 256) {
144
+ throw new RangeError('keys array exceeds 256 entries');
145
+ }
146
+ const seen = new Set();
147
+ for (let i = 0; i < keys.length; ++i) {
148
+ const name = keys[i];
149
+ if (typeof name !== 'string') {
150
+ throw new TypeError('every key must be a string');
151
+ }
152
+ if (seen.has(name)) {
153
+ throw new Error(`duplicate key name at index ${i}: ${name}`);
154
+ }
155
+ seen.add(name);
156
+ }
157
+ return Object.freeze({
158
+ 'threadlocal.schema_version': SCHEMA_VERSION,
159
+ 'threadlocal.attribute_key_map': Object.freeze(keys.slice()),
160
+ 'threadlocal.js_object_record_offset': JS_OBJECT_RECORD_OFFSET,
161
+ 'threadlocal.tagged_size': TAGGED_SIZE,
162
+ 'threadlocal.js_map_table_offset': JS_MAP_TABLE_OFFSET,
163
+ 'threadlocal.ordered_hash_map_header_size': ORDERED_HASH_MAP_HEADER_SIZE,
164
+ });
165
+ }
166
+ //# sourceMappingURL=otel-thread-ctx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"otel-thread-ctx.js","sourceRoot":"","sources":["../../ts/src/otel-thread-ctx.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAuQH,kEA4BC;AAjSD,2FAA2F;AAC3F,yEAAyE;AACzE,wEAAwE;AACxE,yEAAyE;AACzE,oEAAoE;AACpE,EAAE;AACF,oEAAoE;AACpE,4EAA4E;AAC5E,gFAAgF;AAChF,+DAA+D;AAE/D,mEAAmE;AACnE,kEAAkE;AAClE,8DAA8D;AAC9D,mBAAmB;AACnB,EAAE;AACF,2EAA2E;AAE3E,+BAA0B;AAC1B,uDAAmD;AAEnD,+DAG+B;AA8G/B,MAAM,cAAc,GAAG,eAAe,CAAC;AAEvC,2EAA2E;AAC3E,wEAAwE;AACxE,wEAAwE;AACxE,uEAAuE;AACvE,uBAAuB;AACvB,IAAI,uBAAuB,GAAG,EAAE,CAAC;AACjC,IAAI,WAAW,GAAG,CAAC,CAAC;AACpB,IAAI,mBAAmB,GAAG,IAAI,CAAC;AAC/B,IAAI,4B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AAC,EAAE,CAAC;QACzB,MAAM,IAAI,SAAS,CAAC,0CAA0C,CAAC,CAAC;IAClE,CAAC;IACD,IAAI,IAAI,CAAC,MAAM,GAAG,GAAG,EAAE,CAAC;QACtB,MAAM,IAAI,UAAU,CAAC,gCAAgC,CAAC,CAAC;IACzD,CAAC;IACD,MAAM,IAAI,GAAG,IAAI,GAAG,EAAU,CAAC;IAC/B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,CAAC,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;QACrB,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,MAAM,IAAI,SAAS,CAAC,4BAA4B,CAAC,CAAC;QACpD,CAAC;QACD,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;YACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,KAAK,IAAI,EAAE,CAAC,CAAC;QAC/D,CAAC;QACD,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACjB,CAAC;IACD,OAAO,MAAM,CAAC,MAAM,CAAC;QACnB,4BAA4B,EAAE,cAAc;QAC5C,+BAA+B,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;QAC5D,qCAAqC,EAAE,uBAAuB;QAC9D,yBAAyB,EAAE,WAAW;QACtC,iCAAiC,EAAE,mBAAmB;QACtD,0CAA0C,EAAE,4BAA4B;KACzE,CAA6B,CAAC;AACjC,CAAC"}
@@ -15,9 +15,32 @@
15
15
  */
16
16
  import { Profile } from 'pprof-format';
17
17
  import { SourceMapper } from './sourcemapper/sourcemapper';
18
- import { AllocationProfileNode, GenerateAllocationLabelsFunction, GenerateTimeLabelsFunction, TimeProfile } from './v8-types';
18
+ import { AllocationProfileNode, AllocationProfileNodeWithStats, GenerateAllocationLabelsFunction, GenerateTimeLabelsFunction, TimeProfile, TimeProfileNode } from './v8-types';
19
19
  export declare const NON_JS_THREADS_FUNCTION_NAME = "Non JS threads activity";
20
20
  export declare const GARBAGE_COLLECTION_FUNCTION_NAME = "Garbage Collection";
21
+ /**
22
+ * Controls how a frame's column number is represented in the emitted profile.
23
+ * - `'drop'`: omit the column; only the line number is emitted. This is the
24
+ * default and matches the historical behavior and standard pprof line
25
+ * semantics, so existing consumers see unchanged line numbers.
26
+ * - `'pack'`: pack the column into the high 32 bits of the line field
27
+ * (`column << 32 | line`) — the encoding the Datadog deobfuscation backend
28
+ * decodes (the same one the Chrome profile intake uses). Required to
29
+ * deobfuscate single-line (bundled/minified) frames, where the column is the
30
+ * only discriminator between functions. Only frames whose source map was
31
+ * declared but missing locally are packed.
32
+ * - `'emit'`: like `'pack'`, but records the column in the dedicated pprof
33
+ * `Line.column` field instead of packing it into the line field, so the line
34
+ * field stays standards-compliant and the two concerns are cleanly separated.
35
+ * Scoped to the same frames as `'pack'` — only those whose source map was
36
+ * declared but missing locally, i.e. the frames bound for server-side
37
+ * unminification. The Datadog backend performs the `column << 32 | line`
38
+ * packing itself (for Node.js profiles) when it consumes the column field.
39
+ * Requires pprof-format >= 2.3.0, the version that added `Line.column`.
40
+ */
41
+ export type ColumnNumbers = 'drop' | 'pack' | 'emit';
42
+ export declare const DEFAULT_COLUMN_NUMBERS: ColumnNumbers;
43
+ export declare function computeTotalHitCount(root: TimeProfileNode): number;
21
44
  /**
22
45
  * Converts v8 time profile into into a profile proto.
23
46
  * (https://github.com/google/pprof/blob/master/proto/profile.proto)
@@ -25,7 +48,7 @@ export declare const GARBAGE_COLLECTION_FUNCTION_NAME = "Garbage Collection";
25
48
  * @param prof - profile to be converted.
26
49
  * @param intervalMicros - average time (microseconds) between samples.
27
50
  */
28
- export declare function serializeTimeProfile(prof: TimeProfile, intervalMicros: number, sourceMapper?: SourceMapper, recomputeSamplingInterval?: boolean, generateLabels?: GenerateTimeLabelsFunction, lowCardinalityLabels?: string[]): Profile;
51
+ export declare function serializeTimeProfile(prof: TimeProfile, intervalMicros: number, sourceMapper?: SourceMapper, recomputeSamplingInterval?: boolean, generateLabels?: GenerateTimeLabelsFunction, lowCardinalityLabels?: string[], columnNumbers?: ColumnNumbers): Profile;
29
52
  /**
30
53
  * Converts v8 heap profile into into a profile proto.
31
54
  * (https://github.com/google/pprof/blob/master/proto/profile.proto)
@@ -36,4 +59,4 @@ export declare function serializeTimeProfile(prof: TimeProfile, intervalMicros:
36
59
  * nanoseconds.
37
60
  * @param intervalBytes - bytes allocated between samples.
38
61
  */
39
- export declare function serializeHeapProfile(prof: AllocationProfileNode, startTimeNanos: number, intervalBytes: number, ignoreSamplesPath?: string, sourceMapper?: SourceMapper, generateLabels?: GenerateAllocationLabelsFunction): Profile;
62
+ export declare function serializeHeapProfile(prof: AllocationProfileNode | AllocationProfileNodeWithStats, startTimeNanos: number, intervalBytes: number, ignoreSamplesPath?: string, sourceMapper?: SourceMapper, generateLabels?: GenerateAllocationLabelsFunction, allocations?: boolean, columnNumbers?: ColumnNumbers): Profile;
@@ -15,7 +15,8 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.GARBAGE_COLLECTION_FUNCTION_NAME = exports.NON_JS_THREADS_FUNCTION_NAME = void 0;
18
+ exports.DEFAULT_COLUMN_NUMBERS = exports.GARBAGE_COLLECTION_FUNCTION_NAME = exports.NON_JS_THREADS_FUNCTION_NAME = void 0;
19
+ exports.computeTotalHitCount = computeTotalHitCount;
19
20
  exports.serializeTimeProfile = serializeTimeProfile;
20
21
  exports.serializeHeapProfile = serializeHeapProfile;
21
22
  const pprof_format_1 = require("pprof-format");
@@ -26,6 +27,34 @@ function isGeneratedLocation(location) {
26
27
  location.line !== undefined &&
27
28
  location.line > 0);
28
29
  }
30
+ exports.DEFAULT_COLUMN_NUMBERS = 'drop';
31
+ /**
32
+ * Packs a frame's line and column into the single value carried by the pprof
33
+ * Line.line field: the column in the high 32 bits, the line in the low 32 bits
34
+ * (matching the backend's JavaScriptProfileUnminifier.decodeLineAndColumn).
35
+ *
36
+ * BigInt is required: JavaScript's bitwise operators are 32-bit, so `column <<
37
+ * 32` on a plain number is a no-op that returns `column` unchanged. Computing
38
+ * the pack arithmetically as `column * 2**32 + line` instead loses integer
39
+ * precision once the column exceeds 2**21 (the product passes 2**53, Number's
40
+ * safe-integer limit) — and minified single-line bundles routinely have columns
41
+ * that large.
42
+ */
43
+ function packLineAndColumn(line, column) {
44
+ if (!line) {
45
+ return 0;
46
+ }
47
+ if (!column) {
48
+ return line;
49
+ }
50
+ return (BigInt(column) << 32n) | BigInt(line);
51
+ }
52
+ function requireAllocationStat(value, field) {
53
+ if (value === undefined) {
54
+ throw new Error(`Allocation profile is missing ${field}.`);
55
+ }
56
+ return value;
57
+ }
29
58
  /**
30
59
  * Takes v8 profile and populates sample, location, and function fields of
31
60
  * profile.proto.
@@ -38,12 +67,15 @@ function isGeneratedLocation(location) {
38
67
  * appends these to end of an array of samples.
39
68
  * @param stringTable - string table for the existing profile.
40
69
  */
41
- function serialize(profile, root, appendToSamples, stringTable, ignoreSamplesPath, sourceMapper) {
70
+ function serialize(profile, root, appendToSamples, stringTable, ignoreSamplesPath, sourceMapper, columnNumbers = exports.DEFAULT_COLUMN_NUMBERS) {
42
71
  const samples = [];
43
72
  const locations = [];
44
73
  const functions = [];
45
74
  const functionIdMap = new Map();
46
75
  const locationIdMap = new Map();
76
+ const packColumns = columnNumbers === 'pack';
77
+ const emitColumns = columnNumbers === 'emit';
78
+ let hasMissingMapFiles = false;
47
79
  const entries = root.children.map((n) => ({
48
80
  node: n,
49
81
  stack: [],
@@ -70,6 +102,9 @@ function serialize(profile, root, appendToSamples, stringTable, ignoreSamplesPat
70
102
  profile.location = locations;
71
103
  profile.function = functions;
72
104
  profile.stringTable = stringTable;
105
+ if (hasMissingMapFiles) {
106
+ profile.comment = [stringTable.dedup('dd:has-missing-map-files')];
107
+ }
73
108
  function getLocation(node, scriptName, sourceMapper) {
74
109
  let profLoc = {
75
110
  file: scriptName || '',
@@ -80,6 +115,9 @@ function serialize(profile, root, appendToSamples, stringTable, ignoreSamplesPat
80
115
  if (profLoc.line) {
81
116
  if (sourceMapper && isGeneratedLocation(profLoc)) {
82
117
  profLoc = sourceMapper.mappingInfo(profLoc);
118
+ if (profLoc.missingMapFile) {
119
+ hasMissingMapFiles = true;
120
+ }
83
121
  }
84
122
  }
85
123
  const keyStr = `${node.scriptId}:${profLoc.line}:${profLoc.column}:${profLoc.name}`;
@@ -96,9 +134,22 @@ function serialize(profile, root, appendToSamples, stringTable, ignoreSamplesPat
96
134
  return location;
97
135
  }
98
136
  function getLine(loc, scriptId) {
137
+ // Both 'pack' and 'emit' carry the column only for frames whose source map
138
+ // was declared but missing locally — i.e. exactly the frames bound for
139
+ // server-side unminification (the same condition that sets
140
+ // dd:has-missing-map-files). Locally-resolved frames keep a plain line and
141
+ // no column, so column data never reaches profiles that skip server-side
142
+ // unminification.
143
+ const carryColumn = loc.missingMapFile === true;
99
144
  return new pprof_format_1.Line({
100
145
  functionId: getFunction(loc, scriptId).id,
101
- line: loc.line,
146
+ // 'pack' encodes the column into the high 32 bits of the line field.
147
+ line: packColumns && carryColumn
148
+ ? packLineAndColumn(loc.line, loc.column)
149
+ : loc.line,
150
+ // 'emit' records the column in the dedicated pprof Line.column field
151
+ // instead; the backend does the line/column packing itself.
152
+ column: emitColumns && carryColumn ? loc.column : undefined,
102
153
  });
103
154
  }
104
155
  function getFunction(loc, scriptId) {
@@ -167,23 +218,27 @@ function createCpuValueType(table) {
167
218
  unit: table.dedup('nanoseconds'),
168
219
  });
169
220
  }
170
- /**
171
- * @return value type for object counts (type:objects, units:count), and
172
- * adds strings used in this value type to the table.
173
- */
174
- function createObjectCountValueType(table) {
221
+ function createInUseObjectCountValueType(table) {
175
222
  return new pprof_format_1.ValueType({
176
- type: table.dedup('objects'),
223
+ type: table.dedup('inuse_objects'),
177
224
  unit: table.dedup('count'),
178
225
  });
179
226
  }
180
- /**
181
- * @return value type for memory allocations (type:space, units:bytes), and
182
- * adds strings used in this value type to the table.
183
- */
184
- function createAllocationValueType(table) {
227
+ function createAllocatedObjectCountValueType(table) {
228
+ return new pprof_format_1.ValueType({
229
+ type: table.dedup('alloc_objects'),
230
+ unit: table.dedup('count'),
231
+ });
232
+ }
233
+ function createInUseSpaceValueType(table) {
185
234
  return new pprof_format_1.ValueType({
186
- type: table.dedup('space'),
235
+ type: table.dedup('inuse_space'),
236
+ unit: table.dedup('bytes'),
237
+ });
238
+ }
239
+ function createAllocatedSpaceValueType(table) {
240
+ return new pprof_format_1.ValueType({
241
+ type: table.dedup('alloc_space'),
187
242
  unit: table.dedup('bytes'),
188
243
  });
189
244
  }
@@ -269,7 +324,7 @@ function updateTimeProfile(prof) {
269
324
  * @param prof - profile to be converted.
270
325
  * @param intervalMicros - average time (microseconds) between samples.
271
326
  */
272
- function serializeTimeProfile(prof, intervalMicros, sourceMapper, recomputeSamplingInterval = false, generateLabels, lowCardinalityLabels = []) {
327
+ function serializeTimeProfile(prof, intervalMicros, sourceMapper, recomputeSamplingInterval = false, generateLabels, lowCardinalityLabels = [], columnNumbers = exports.DEFAULT_COLUMN_NUMBERS) {
273
328
  // If requested, recompute sampling interval from profile duration and total number of hits,
274
329
  // since profile duration should be #hits x interval.
275
330
  // Recomputing an average interval is more accurate, since in practice intervals between
@@ -277,7 +332,7 @@ function serializeTimeProfile(prof, intervalMicros, sourceMapper, recomputeSampl
277
332
  // For very short durations, computation becomes meaningless (eg. if there is only one hit),
278
333
  // therefore keep intervalMicros as a lower bound and 2 * intervalMicros as upper bound.
279
334
  if (recomputeSamplingInterval) {
280
- const totalHitCount = computeTotalHitCount(prof.topDownRoot);
335
+ const totalHitCount = prof.totalHitCount ?? computeTotalHitCount(prof.topDownRoot);
281
336
  if (totalHitCount > 0) {
282
337
  intervalMicros = Math.min(Math.max(Math.floor((prof.endTime - prof.startTime) / totalHitCount), intervalMicros), 2 * intervalMicros);
283
338
  }
@@ -314,7 +369,7 @@ function serializeTimeProfile(prof, intervalMicros, sourceMapper, recomputeSampl
314
369
  for (const context of entry.node.contexts || []) {
315
370
  const labels = generateLabels
316
371
  ? generateLabels({ node: entry.node, context })
317
- : context.context ?? {};
372
+ : (context.context ?? {});
318
373
  const labelsArr = buildLabels(labels, stringTable);
319
374
  if (labelsArr.length > 0) {
320
375
  // Only assign wall time if there are hits, some special nodes such as `(Non-JS threads)`
@@ -367,7 +422,7 @@ function serializeTimeProfile(prof, intervalMicros, sourceMapper, recomputeSampl
367
422
  period: intervalNanos,
368
423
  };
369
424
  const updatedProf = updateTimeProfile(prof);
370
- serialize(profile, updatedProf.topDownRoot, appendTimeEntryToSamples, stringTable, undefined, sourceMapper);
425
+ serialize(profile, updatedProf.topDownRoot, appendTimeEntryToSamples, stringTable, undefined, sourceMapper, columnNumbers);
371
426
  return new pprof_format_1.Profile(profile);
372
427
  }
373
428
  function buildLabels(labelSet, stringTable) {
@@ -401,16 +456,36 @@ function buildLabels(labelSet, stringTable) {
401
456
  * nanoseconds.
402
457
  * @param intervalBytes - bytes allocated between samples.
403
458
  */
404
- function serializeHeapProfile(prof, startTimeNanos, intervalBytes, ignoreSamplesPath, sourceMapper, generateLabels) {
459
+ function serializeHeapProfile(prof, startTimeNanos, intervalBytes, ignoreSamplesPath, sourceMapper, generateLabels, allocations = false, columnNumbers = exports.DEFAULT_COLUMN_NUMBERS) {
405
460
  const appendHeapEntryToSamples = (entry, samples) => {
406
461
  if (entry.node.allocations.length > 0) {
407
462
  const labels = generateLabels
408
463
  ? buildLabels(generateLabels({ node: entry.node }), stringTable)
409
464
  : [];
410
465
  for (const alloc of entry.node.allocations) {
466
+ // Live heap only
467
+ if (!allocations) {
468
+ const heapAllocation = alloc;
469
+ const sample = new pprof_format_1.Sample({
470
+ locationId: entry.stack,
471
+ value: [
472
+ heapAllocation.count,
473
+ heapAllocation.sizeBytes * heapAllocation.count,
474
+ ],
475
+ label: labels,
476
+ // TODO: add tag for allocation size
477
+ });
478
+ samples.push(sample);
479
+ continue;
480
+ }
481
+ const stats = alloc;
482
+ const inuseObjects = requireAllocationStat(stats.inuseObjects, 'inuseObjects');
483
+ const inuseSpaceBytes = requireAllocationStat(stats.inuseSpaceBytes, 'inuseSpaceBytes');
484
+ const allocObjects = requireAllocationStat(stats.allocObjects, 'allocObjects');
485
+ const allocSpaceBytes = requireAllocationStat(stats.allocSpaceBytes, 'allocSpaceBytes');
411
486
  const sample = new pprof_format_1.Sample({
412
487
  locationId: entry.stack,
413
- value: [alloc.count, alloc.sizeBytes * alloc.count],
488
+ value: [inuseObjects, allocObjects, inuseSpaceBytes, allocSpaceBytes],
414
489
  label: labels,
415
490
  // TODO: add tag for allocation size
416
491
  });
@@ -419,15 +494,24 @@ function serializeHeapProfile(prof, startTimeNanos, intervalBytes, ignoreSamples
419
494
  }
420
495
  };
421
496
  const stringTable = new pprof_format_1.StringTable();
422
- const sampleValueType = createObjectCountValueType(stringTable);
423
- const allocationValueType = createAllocationValueType(stringTable);
497
+ const sampleTypes = allocations
498
+ ? [
499
+ createInUseObjectCountValueType(stringTable),
500
+ createAllocatedObjectCountValueType(stringTable),
501
+ createInUseSpaceValueType(stringTable),
502
+ createAllocatedSpaceValueType(stringTable),
503
+ ]
504
+ : [
505
+ createInUseObjectCountValueType(stringTable),
506
+ createInUseSpaceValueType(stringTable),
507
+ ];
424
508
  const profile = {
425
- sampleType: [sampleValueType, allocationValueType],
509
+ sampleType: sampleTypes,
426
510
  timeNanos: startTimeNanos,
427
- periodType: allocationValueType,
511
+ periodType: sampleTypes[sampleTypes.length - 1],
428
512
  period: intervalBytes,
429
513
  };
430
- serialize(profile, prof, appendHeapEntryToSamples, stringTable, ignoreSamplesPath, sourceMapper);
514
+ serialize(profile, prof, appendHeapEntryToSamples, stringTable, ignoreSamplesPath, sourceMapper, columnNumbers);
431
515
  return new pprof_format_1.Profile(profile);
432
516
  }
433
517
  //# sourceMappingURL=profile-serializer.js.map