@datadog/pprof 5.20.0 → 5.21.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.
- package/out/src/heap-profiler-bindings.d.ts +1 -0
- package/out/src/heap-profiler-bindings.js +4 -0
- package/out/src/heap-profiler-bindings.js.map +1 -1
- package/out/src/heap-profiler.d.ts +13 -3
- package/out/src/heap-profiler.js +81 -50
- package/out/src/heap-profiler.js.map +1 -1
- package/out/src/index.d.ts +7 -1
- package/out/src/index.js +7 -1
- package/out/src/index.js.map +1 -1
- package/out/src/otel-serializer.d.ts +164 -0
- package/out/src/otel-serializer.js +431 -0
- package/out/src/otel-serializer.js.map +1 -0
- package/out/src/profile-normalizer.d.ts +182 -0
- package/out/src/profile-normalizer.js +425 -0
- package/out/src/profile-normalizer.js.map +1 -0
- package/out/src/profile-serializer.d.ts +37 -7
- package/out/src/profile-serializer.js +158 -399
- package/out/src/profile-serializer.js.map +1 -1
- package/out/src/time-profiler.d.ts +13 -2
- package/out/src/time-profiler.js +67 -18
- package/out/src/time-profiler.js.map +1 -1
- package/out/src/v8-types.d.ts +8 -2
- package/package.json +7 -7
- package/prebuilds/darwin-arm64/dd_pprof.node.abi108.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi111.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi115.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi120.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi127.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi131.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi137.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi141.node +0 -0
- package/prebuilds/darwin-arm64/dd_pprof.node.abi147.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi108.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi111.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi115.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi120.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi127.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi131.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi137.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi141.node +0 -0
- package/prebuilds/darwin-x64/dd_pprof.node.abi147.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi108.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi111.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi115.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi120.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi127.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi131.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi137.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi141.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.musl.node.abi147.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi108.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi111.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi115.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi120.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi127.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi131.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi137.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi141.node +0 -0
- package/prebuilds/linux-arm64/dd_pprof.node.abi147.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi108.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi111.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi115.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi120.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi127.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi131.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi137.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi141.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.musl.node.abi147.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi108.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi111.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi115.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi120.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi127.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi131.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi137.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi141.node +0 -0
- package/prebuilds/linux-x64/dd_pprof.node.abi147.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi108.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi111.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi115.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi120.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi127.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi131.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi137.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi141.node +0 -0
- package/prebuilds/win32-x64/dd_pprof.node.abi147.node +0 -0
|
@@ -0,0 +1,431 @@
|
|
|
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.OtelProfilesBuilder = void 0;
|
|
19
|
+
/**
|
|
20
|
+
* Serializes normalized profiles as OpenTelemetry profiles, in an
|
|
21
|
+
* ExportProfilesServiceRequest of pprof-format/otel.
|
|
22
|
+
*
|
|
23
|
+
* The request has one profile per sample type, since an OTel profile has a
|
|
24
|
+
* single sample type, and all of them share one dictionary. A sample count
|
|
25
|
+
* is the exception when every sampling event is a single timestamped
|
|
26
|
+
* observation (time profiles normalized with samplePerContext): it is then
|
|
27
|
+
* folded into the profile of the period type, whose observations include
|
|
28
|
+
* every sampling event, even zero-valued ones such as idle samples' wall
|
|
29
|
+
* time. Otherwise the count keeps a profile of its own, so no count is lost.
|
|
30
|
+
*
|
|
31
|
+
* Samples with the same stack, attributes and link are merged into one, as
|
|
32
|
+
* the OTel format requires: timestamped samples accumulate a value and a
|
|
33
|
+
* timestamp per observation, and samples without a timestamp are summed into
|
|
34
|
+
* one value. Other zero values are left out of the profile of their sample
|
|
35
|
+
* type: e.g. zero CPU time, or the wall time of the synthetic non-JS threads
|
|
36
|
+
* sample.
|
|
37
|
+
*
|
|
38
|
+
* OtelProfilesBuilder collects several profiles, e.g. time and heap, into one
|
|
39
|
+
* request.
|
|
40
|
+
*
|
|
41
|
+
* Frames carry their columns. With the `missing-source-maps` column policy,
|
|
42
|
+
* only frames whose script declares a source map that can't be found locally
|
|
43
|
+
* do: the candidates for server-side unminification, which can then be told
|
|
44
|
+
* apart without attributes of their own, as with pprof's `emit` columns.
|
|
45
|
+
*/
|
|
46
|
+
const otel_1 = require("pprof-format/otel");
|
|
47
|
+
const profile_normalizer_1 = require("./profile-normalizer");
|
|
48
|
+
const DEFAULT_SCOPE = { name: '@datadog/pprof' };
|
|
49
|
+
const SAMPLE_COUNT = { type: 'sample', unit: 'count' };
|
|
50
|
+
/**
|
|
51
|
+
* Builds one ExportProfilesServiceRequest from any number of normalized
|
|
52
|
+
* profiles, which share the request's dictionary: e.g. the time and heap
|
|
53
|
+
* profiles of one collection cycle. Each profile is added through a visitor
|
|
54
|
+
* from visitor(), so that whatever drives the visit (the normalizer, or a
|
|
55
|
+
* profiler emitting samples itself) needs no knowledge of the OTel format.
|
|
56
|
+
*
|
|
57
|
+
* Functions, locations, stacks, attributes and links are deduplicated across
|
|
58
|
+
* all profiles, and each dictionary table starts with its zero value. A
|
|
59
|
+
* sample whose stack has no frames refers to the zero-value stack.
|
|
60
|
+
*
|
|
61
|
+
* All profiles of the request get the same time range, the union of their
|
|
62
|
+
* own: one collection cycle's profiles then cover the same period, even a heap
|
|
63
|
+
* profile, which is a snapshot at a single instant.
|
|
64
|
+
*
|
|
65
|
+
* Visitors run synchronously, and build() can be called after any number of
|
|
66
|
+
* completed visits. A visit's profiles are added to the request when it ends,
|
|
67
|
+
* so a visit that throws, or isn't ended, adds none: the builder can go on
|
|
68
|
+
* with other visits, and its request has their profiles only. (A failed
|
|
69
|
+
* visit can leave dictionary entries that no profile refers to.)
|
|
70
|
+
*/
|
|
71
|
+
class OtelProfilesBuilder {
|
|
72
|
+
constructor(options = {}) {
|
|
73
|
+
this.options = options;
|
|
74
|
+
this.strings = new otel_1.StringTable();
|
|
75
|
+
this.functionTable = [{}];
|
|
76
|
+
this.locationTable = [{}];
|
|
77
|
+
this.stackTable = [{}];
|
|
78
|
+
this.attributeTable = [{}];
|
|
79
|
+
// An all-zero link, with IDs of the lengths codecs expect.
|
|
80
|
+
this.linkTable = [
|
|
81
|
+
{ traceId: new Uint8Array(16), spanId: new Uint8Array(8) },
|
|
82
|
+
];
|
|
83
|
+
this.functionIndices = new Map();
|
|
84
|
+
this.locationIndices = new Map();
|
|
85
|
+
this.stackIndices = new Map();
|
|
86
|
+
// Label attributes' indices, by key and then by value. Map keys compare as
|
|
87
|
+
// SameValueZero, which tells 1, 1n and '1' apart, without building a string
|
|
88
|
+
// key for every label of every sample.
|
|
89
|
+
this.labelAttributeIndices = new Map();
|
|
90
|
+
this.profileAttributeIndices = new Map();
|
|
91
|
+
this.linkIndices = new Map();
|
|
92
|
+
this.profiles = [];
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Returns a visitor that adds one normalized profile to the request, as one
|
|
96
|
+
* OTel profile per sample type, when the visit ends: its end() must be
|
|
97
|
+
* called. Use a new visitor for each profile.
|
|
98
|
+
*/
|
|
99
|
+
visitor(options = {}) {
|
|
100
|
+
let layout = {
|
|
101
|
+
profiles: [],
|
|
102
|
+
samplesByIdentity: [],
|
|
103
|
+
countIndex: -1,
|
|
104
|
+
periodIndex: -1,
|
|
105
|
+
foldable: false,
|
|
106
|
+
};
|
|
107
|
+
return {
|
|
108
|
+
metadata: metadata => {
|
|
109
|
+
layout = this.layout(metadata);
|
|
110
|
+
const attributeIndices = (options.attributes ?? []).map(attribute => this.profileAttributeIndex(attribute));
|
|
111
|
+
for (const profile of layout.profiles) {
|
|
112
|
+
profile.attributeIndices = [...attributeIndices];
|
|
113
|
+
if (options.originalPayload) {
|
|
114
|
+
profile.originalPayloadFormat = options.originalPayload.format;
|
|
115
|
+
profile.originalPayload = options.originalPayload.payload;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
frame: frame => this.locationIndex(frame),
|
|
120
|
+
stack: frameIds => this.stackIndex(frameIds),
|
|
121
|
+
sample: sample => this.addSample(sample, layout),
|
|
122
|
+
end: () => {
|
|
123
|
+
const folded = layout.countIndex >= 0 && layout.foldable
|
|
124
|
+
? layout.profiles[layout.countIndex]
|
|
125
|
+
: undefined;
|
|
126
|
+
this.profiles.push(...layout.profiles.filter(p => p !== folded));
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Creates the OTel profiles for a normalized profile's sample types.
|
|
132
|
+
*
|
|
133
|
+
* A sample count type can be folded into the profile of the period type
|
|
134
|
+
* (wall time, for time profiles) instead of keeping a profile of its own:
|
|
135
|
+
* that profile gets an observation for every sampling event, even one whose
|
|
136
|
+
* value is zero, so the count is its number of observations. That keeps
|
|
137
|
+
* idle samples, which have zero wall time, as timestamped evidence that
|
|
138
|
+
* sampling happened, without repeating every sample in a count profile.
|
|
139
|
+
* The fold is only lossless if every sampling event is a single timestamped
|
|
140
|
+
* observation, so the count profile is built anyway, and dropped at the end
|
|
141
|
+
* of the visit only if that turned out to be the case.
|
|
142
|
+
*/
|
|
143
|
+
layout(metadata) {
|
|
144
|
+
const { sampleTypes, periodType } = metadata;
|
|
145
|
+
const sameType = (a, b) => a.type === b.type && a.unit === b.unit;
|
|
146
|
+
let countIndex = sampleTypes.findIndex(t => sameType(t, SAMPLE_COUNT));
|
|
147
|
+
const periodIndex = sampleTypes.findIndex(t => sameType(t, periodType));
|
|
148
|
+
if (periodIndex < 0 || periodIndex === countIndex)
|
|
149
|
+
countIndex = -1;
|
|
150
|
+
const rename = this.options.renameSampleType ?? (t => t);
|
|
151
|
+
const otelPeriodType = this.valueType(rename(periodType));
|
|
152
|
+
const profiles = sampleTypes.map(sampleType => ({
|
|
153
|
+
sampleType: this.valueType(rename(sampleType)),
|
|
154
|
+
periodType: otelPeriodType,
|
|
155
|
+
period: metadata.period,
|
|
156
|
+
// Sample timestamps should lie within the profile's time range, so it
|
|
157
|
+
// starts at the start of collection when that is known.
|
|
158
|
+
timeUnixNano: metadata.startTimeNanos ?? metadata.timeNanos,
|
|
159
|
+
durationNano: metadata.durationNanos ?? 0,
|
|
160
|
+
samples: [],
|
|
161
|
+
}));
|
|
162
|
+
return {
|
|
163
|
+
profiles,
|
|
164
|
+
samplesByIdentity: profiles.map(() => new Map()),
|
|
165
|
+
countIndex,
|
|
166
|
+
periodIndex,
|
|
167
|
+
foldable: countIndex >= 0,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/** Builds the request from the profiles added so far. */
|
|
171
|
+
build() {
|
|
172
|
+
return new otel_1.ExportProfilesServiceRequest({
|
|
173
|
+
resourceProfiles: [
|
|
174
|
+
{
|
|
175
|
+
resource: { attributes: this.options.resourceAttributes ?? [] },
|
|
176
|
+
scopeProfiles: [
|
|
177
|
+
{
|
|
178
|
+
scope: this.options.scope ?? DEFAULT_SCOPE,
|
|
179
|
+
profiles: withCommonTimeRange(this.profiles),
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
dictionary: {
|
|
185
|
+
functionTable: this.functionTable,
|
|
186
|
+
locationTable: this.locationTable,
|
|
187
|
+
linkTable: this.linkTable,
|
|
188
|
+
stringTable: this.strings,
|
|
189
|
+
attributeTable: this.attributeTable,
|
|
190
|
+
stackTable: this.stackTable,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
addSample(sample, layout) {
|
|
195
|
+
const { profiles, samplesByIdentity, countIndex, periodIndex } = layout;
|
|
196
|
+
// With a foldable count, the period type's profile observes every
|
|
197
|
+
// sampling event, even one with a zero value, such as an idle sample's
|
|
198
|
+
// wall time.
|
|
199
|
+
const count = countIndex >= 0 ? sample.values[countIndex] : 0;
|
|
200
|
+
const isSamplingEvent = count > 0;
|
|
201
|
+
// Folding is lossless only if the period type's observations are exactly
|
|
202
|
+
// the sampling events, one each: every sampling event is a single
|
|
203
|
+
// timestamped observation, and no other sample adds an observation.
|
|
204
|
+
if (isSamplingEvent
|
|
205
|
+
? sample.timestamp === undefined || count !== 1
|
|
206
|
+
: countIndex >= 0 && sample.values[periodIndex] !== 0) {
|
|
207
|
+
layout.foldable = false;
|
|
208
|
+
}
|
|
209
|
+
let labels = sample.labels;
|
|
210
|
+
let linkIndex = 0;
|
|
211
|
+
if (this.options.extractLink) {
|
|
212
|
+
const extracted = this.options.extractLink(labels);
|
|
213
|
+
labels = extracted.labels;
|
|
214
|
+
if (extracted.link) {
|
|
215
|
+
linkIndex = this.linkIndex(extracted.link);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
// Attributes form a set, so order them for the sample's identity.
|
|
219
|
+
const attributeIndices = labels
|
|
220
|
+
.map(label => this.attributeIndex(label))
|
|
221
|
+
.sort((a, b) => a - b);
|
|
222
|
+
const timestamped = sample.timestamp !== undefined;
|
|
223
|
+
const identity = `${sample.stackId}|${attributeIndices.join(',')}|${linkIndex}|${timestamped}`;
|
|
224
|
+
sample.values.forEach((value, i) => {
|
|
225
|
+
const profile = profiles[i];
|
|
226
|
+
if (value === 0 && !(i === periodIndex && isSamplingEvent))
|
|
227
|
+
return;
|
|
228
|
+
const samples = samplesByIdentity[i];
|
|
229
|
+
let otelSample = samples.get(identity);
|
|
230
|
+
if (otelSample === undefined) {
|
|
231
|
+
otelSample = {
|
|
232
|
+
stackIndex: sample.stackId,
|
|
233
|
+
attributeIndices,
|
|
234
|
+
linkIndex,
|
|
235
|
+
values: [],
|
|
236
|
+
timestampsUnixNano: [],
|
|
237
|
+
};
|
|
238
|
+
samples.set(identity, otelSample);
|
|
239
|
+
profile.samples.push(otelSample);
|
|
240
|
+
}
|
|
241
|
+
if (timestamped) {
|
|
242
|
+
otelSample.values.push(value);
|
|
243
|
+
otelSample.timestampsUnixNano.push(sample.timestamp);
|
|
244
|
+
}
|
|
245
|
+
else if (otelSample.values.length === 0) {
|
|
246
|
+
otelSample.values.push(value);
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
otelSample.values[0] = Number(otelSample.values[0]) + value;
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
valueType({ type, unit }) {
|
|
254
|
+
return {
|
|
255
|
+
typeStrindex: this.strings.dedup(type),
|
|
256
|
+
unitStrindex: this.strings.dedup(unit),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
// Functions are identified by what they encode as: their display name and
|
|
260
|
+
// resolved file. (After source mapping, one script can map to several
|
|
261
|
+
// files, so a script ID doesn't identify a file.)
|
|
262
|
+
functionIndex(frame) {
|
|
263
|
+
const name = (0, profile_normalizer_1.frameFunctionName)(frame);
|
|
264
|
+
const key = `${frame.file.length}:${frame.file}:${name}`;
|
|
265
|
+
let index = this.functionIndices.get(key);
|
|
266
|
+
if (index === undefined) {
|
|
267
|
+
index =
|
|
268
|
+
this.functionTable.push({
|
|
269
|
+
nameStrindex: this.strings.dedup(name),
|
|
270
|
+
filenameStrindex: this.strings.dedup(frame.file),
|
|
271
|
+
}) - 1;
|
|
272
|
+
this.functionIndices.set(key, index);
|
|
273
|
+
}
|
|
274
|
+
return index;
|
|
275
|
+
}
|
|
276
|
+
locationIndex(frame) {
|
|
277
|
+
const functionIndex = this.functionIndex(frame);
|
|
278
|
+
const line = frame.line ?? 0;
|
|
279
|
+
const carryColumn = this.options.columns !== 'missing-source-maps' || frame.missingMapFile;
|
|
280
|
+
const column = carryColumn ? (frame.column ?? 0) : 0;
|
|
281
|
+
const key = `${functionIndex}:${line}:${column}`;
|
|
282
|
+
let index = this.locationIndices.get(key);
|
|
283
|
+
if (index === undefined) {
|
|
284
|
+
index =
|
|
285
|
+
this.locationTable.push({ lines: [{ functionIndex, line, column }] }) - 1;
|
|
286
|
+
this.locationIndices.set(key, index);
|
|
287
|
+
}
|
|
288
|
+
return index;
|
|
289
|
+
}
|
|
290
|
+
stackIndex(locationIndices) {
|
|
291
|
+
// A sample without frames, e.g. an event, has the zero-value stack
|
|
292
|
+
if (locationIndices.length === 0)
|
|
293
|
+
return 0;
|
|
294
|
+
const key = locationIndices.join(',');
|
|
295
|
+
let index = this.stackIndices.get(key);
|
|
296
|
+
if (index === undefined) {
|
|
297
|
+
index = this.stackTable.push({ locationIndices }) - 1;
|
|
298
|
+
this.stackIndices.set(key, index);
|
|
299
|
+
}
|
|
300
|
+
return index;
|
|
301
|
+
}
|
|
302
|
+
attributeIndex([key, value]) {
|
|
303
|
+
let byValue = this.labelAttributeIndices.get(key);
|
|
304
|
+
if (byValue === undefined) {
|
|
305
|
+
byValue = new Map();
|
|
306
|
+
this.labelAttributeIndices.set(key, byValue);
|
|
307
|
+
}
|
|
308
|
+
let index = byValue.get(value);
|
|
309
|
+
if (index === undefined) {
|
|
310
|
+
index =
|
|
311
|
+
this.attributeTable.push({
|
|
312
|
+
keyStrindex: this.strings.dedup(key),
|
|
313
|
+
value: anyValue(value),
|
|
314
|
+
}) - 1;
|
|
315
|
+
byValue.set(value, index);
|
|
316
|
+
}
|
|
317
|
+
return index;
|
|
318
|
+
}
|
|
319
|
+
profileAttributeIndex({ key, value }) {
|
|
320
|
+
// Profile attributes are few, and structured values need a canonical form
|
|
321
|
+
const internKey = JSON.stringify([key, canonicalValue(value)]);
|
|
322
|
+
let index = this.profileAttributeIndices.get(internKey);
|
|
323
|
+
if (index === undefined) {
|
|
324
|
+
index =
|
|
325
|
+
this.attributeTable.push({
|
|
326
|
+
keyStrindex: this.strings.dedup(key),
|
|
327
|
+
value,
|
|
328
|
+
}) - 1;
|
|
329
|
+
this.profileAttributeIndices.set(internKey, index);
|
|
330
|
+
}
|
|
331
|
+
return index;
|
|
332
|
+
}
|
|
333
|
+
linkIndex(link) {
|
|
334
|
+
if (link.traceId.length !== 16 || link.spanId.length !== 8) {
|
|
335
|
+
throw new TypeError(`A link needs a 16-byte trace ID and an 8-byte span ID, not ${link.traceId.length} and ${link.spanId.length} bytes`);
|
|
336
|
+
}
|
|
337
|
+
// A trace or span ID of all zeros is invalid, so either one means no
|
|
338
|
+
// link: index 0
|
|
339
|
+
if (isAllZero(link.traceId) || isAllZero(link.spanId))
|
|
340
|
+
return 0;
|
|
341
|
+
const key = `${hex(link.traceId)}:${hex(link.spanId)}`;
|
|
342
|
+
let index = this.linkIndices.get(key);
|
|
343
|
+
if (index === undefined) {
|
|
344
|
+
// Copy the IDs, in case the caller reuses its arrays
|
|
345
|
+
index =
|
|
346
|
+
this.linkTable.push({
|
|
347
|
+
traceId: Uint8Array.from(link.traceId),
|
|
348
|
+
spanId: Uint8Array.from(link.spanId),
|
|
349
|
+
}) - 1;
|
|
350
|
+
this.linkIndices.set(key, index);
|
|
351
|
+
}
|
|
352
|
+
return index;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
exports.OtelProfilesBuilder = OtelProfilesBuilder;
|
|
356
|
+
/**
|
|
357
|
+
* Converts a label value to an attribute value. Integers become int64
|
|
358
|
+
* attributes: like pprof's int64 labels, bigints of 2^63 and above (such as
|
|
359
|
+
* large unsigned 64-bit span IDs) wrap to negative values, and bits beyond 64
|
|
360
|
+
* are lost. Labels whose exact unsigned value matters should be strings, or
|
|
361
|
+
* links.
|
|
362
|
+
*/
|
|
363
|
+
function anyValue(value) {
|
|
364
|
+
if (typeof value === 'string')
|
|
365
|
+
return { stringValue: value };
|
|
366
|
+
if (typeof value === 'number' && !Number.isInteger(value)) {
|
|
367
|
+
return { doubleValue: value };
|
|
368
|
+
}
|
|
369
|
+
return { intValue: value };
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Returns the profiles, each with the union of their time ranges. The
|
|
373
|
+
* profiles are copied, so that each keeps its own range for later builds.
|
|
374
|
+
*/
|
|
375
|
+
function withCommonTimeRange(profiles) {
|
|
376
|
+
if (profiles.length < 2)
|
|
377
|
+
return profiles;
|
|
378
|
+
let start;
|
|
379
|
+
let end;
|
|
380
|
+
for (const profile of profiles) {
|
|
381
|
+
const profileStart = BigInt(profile.timeUnixNano ?? 0);
|
|
382
|
+
const profileEnd = profileStart + BigInt(profile.durationNano ?? 0);
|
|
383
|
+
if (start === undefined || profileStart < start)
|
|
384
|
+
start = profileStart;
|
|
385
|
+
if (end === undefined || profileEnd > end)
|
|
386
|
+
end = profileEnd;
|
|
387
|
+
}
|
|
388
|
+
const timeUnixNano = start;
|
|
389
|
+
const durationNano = end - start;
|
|
390
|
+
return profiles.map(profile => ({ ...profile, timeUnixNano, durationNano }));
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* A JSON-serializable form of an attribute value that is equal for equal
|
|
394
|
+
* values, and different for values of different types, e.g. int 1 and double
|
|
395
|
+
* 1.
|
|
396
|
+
*/
|
|
397
|
+
function canonicalValue(value) {
|
|
398
|
+
if (value.stringValue !== undefined)
|
|
399
|
+
return ['s', value.stringValue];
|
|
400
|
+
if (value.boolValue !== undefined)
|
|
401
|
+
return ['b', value.boolValue];
|
|
402
|
+
if (value.intValue !== undefined)
|
|
403
|
+
return ['i', String(value.intValue)];
|
|
404
|
+
if (value.doubleValue !== undefined)
|
|
405
|
+
return ['d', String(value.doubleValue)];
|
|
406
|
+
if (value.bytesValue !== undefined)
|
|
407
|
+
return ['y', hex(value.bytesValue)];
|
|
408
|
+
if (value.arrayValue !== undefined) {
|
|
409
|
+
return ['a', (value.arrayValue.values ?? []).map(canonicalValue)];
|
|
410
|
+
}
|
|
411
|
+
if (value.kvlistValue !== undefined) {
|
|
412
|
+
return [
|
|
413
|
+
'k',
|
|
414
|
+
(value.kvlistValue.values ?? []).map(({ key, value }) => [
|
|
415
|
+
key,
|
|
416
|
+
value === undefined ? null : canonicalValue(value),
|
|
417
|
+
]),
|
|
418
|
+
];
|
|
419
|
+
}
|
|
420
|
+
if (value.stringValueStrindex !== undefined) {
|
|
421
|
+
return ['si', value.stringValueStrindex];
|
|
422
|
+
}
|
|
423
|
+
return ['empty'];
|
|
424
|
+
}
|
|
425
|
+
function isAllZero(bytes) {
|
|
426
|
+
return bytes.every(byte => byte === 0);
|
|
427
|
+
}
|
|
428
|
+
function hex(bytes) {
|
|
429
|
+
return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length).toString('hex');
|
|
430
|
+
}
|
|
431
|
+
//# sourceMappingURL=otel-serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
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+
/**
|
|
2
|
+
* Copyright 2026 Datadog. All Rights Reserved.
|
|
3
|
+
*
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* Format-neutral normalization of V8 profiles.
|
|
18
|
+
*
|
|
19
|
+
* Turns the raw V8 time and heap profile trees into a stream of resolved
|
|
20
|
+
* frames and samples, applying everything that is a matter of profile
|
|
21
|
+
* semantics rather than of any particular output format: source mapping,
|
|
22
|
+
* synthetic runtime frames, per-context sample expansion, CPU time
|
|
23
|
+
* attribution, label generation and sample values. Output formats (pprof in
|
|
24
|
+
* profile-serializer.ts, and potentially others) are implemented as
|
|
25
|
+
* {@link ProfileVisitor}s on top of it.
|
|
26
|
+
*/
|
|
27
|
+
import { SourceMapper } from './sourcemapper/sourcemapper';
|
|
28
|
+
import { AllocationProfileNode, AllocationProfileNodeWithStats, GenerateAllocationLabelsFunction, GenerateTimeLabelsFunction, TimeProfile, TimeProfileNode } from './v8-types';
|
|
29
|
+
export declare const NON_JS_THREADS_FUNCTION_NAME = "Non JS threads activity";
|
|
30
|
+
export declare const GARBAGE_COLLECTION_FUNCTION_NAME = "Garbage Collection";
|
|
31
|
+
/**
|
|
32
|
+
* A resolved stack frame: `file://` prefix stripped and source map applied.
|
|
33
|
+
*/
|
|
34
|
+
export interface Frame {
|
|
35
|
+
/**
|
|
36
|
+
* Function name after source mapping. Empty or absent for anonymous
|
|
37
|
+
* functions; use {@link frameFunctionName} to get a display name.
|
|
38
|
+
*/
|
|
39
|
+
name?: string;
|
|
40
|
+
file: string;
|
|
41
|
+
line?: number;
|
|
42
|
+
column?: number;
|
|
43
|
+
scriptId?: number;
|
|
44
|
+
/**
|
|
45
|
+
* True when the frame's script declares a sourceMappingURL but the map
|
|
46
|
+
* could not be found locally, i.e. the frame is a candidate for server-side
|
|
47
|
+
* unminification.
|
|
48
|
+
*/
|
|
49
|
+
missingMapFile: boolean;
|
|
50
|
+
}
|
|
51
|
+
export type SampleLabelValue = string | number | bigint;
|
|
52
|
+
/** A label as a key/value pair. */
|
|
53
|
+
export type SampleLabel = [key: string, value: SampleLabelValue];
|
|
54
|
+
export interface NormalizedSample {
|
|
55
|
+
/** The stack, as the ID returned by {@link ProfileVisitor.stack}. */
|
|
56
|
+
stackId: number;
|
|
57
|
+
/** One value for each of {@link ProfileMetadata.sampleTypes}. */
|
|
58
|
+
values: number[];
|
|
59
|
+
labels: SampleLabel[];
|
|
60
|
+
/**
|
|
61
|
+
* Time at which the sample was taken, in nanoseconds since epoch (with
|
|
62
|
+
* microsecond precision). Only set on samples that represent a single
|
|
63
|
+
* observation with a known time, i.e. time profile samples for a context.
|
|
64
|
+
* Undefined for aggregated samples.
|
|
65
|
+
*/
|
|
66
|
+
timestamp?: bigint;
|
|
67
|
+
}
|
|
68
|
+
export interface SampleType {
|
|
69
|
+
type: string;
|
|
70
|
+
unit: string;
|
|
71
|
+
}
|
|
72
|
+
export interface ProfileMetadata {
|
|
73
|
+
sampleTypes: SampleType[];
|
|
74
|
+
periodType: SampleType;
|
|
75
|
+
period: number;
|
|
76
|
+
/**
|
|
77
|
+
* Time of collection in nanoseconds since epoch: when the profile was
|
|
78
|
+
* normalized. A bigint, because the value exceeds Number.MAX_SAFE_INTEGER.
|
|
79
|
+
*/
|
|
80
|
+
timeNanos: bigint;
|
|
81
|
+
/**
|
|
82
|
+
* Start of the profile's collection period, in nanoseconds since epoch,
|
|
83
|
+
* for profiles collected over a period (time profiles). Sample timestamps
|
|
84
|
+
* fall within [startTimeNanos, startTimeNanos + durationNanos].
|
|
85
|
+
*/
|
|
86
|
+
startTimeNanos?: bigint;
|
|
87
|
+
durationNanos?: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Receives a normalized profile. Calls arrive in this order: metadata() once,
|
|
91
|
+
* then frame(), stack() and sample() interleaved, then end() once. Each distinct frame and
|
|
92
|
+
* stack is delivered exactly once, before the first stack or sample that
|
|
93
|
+
* references it. Only stacks that have samples are delivered.
|
|
94
|
+
*
|
|
95
|
+
* All samples of a profile tree node are delivered consecutively. A stack ID
|
|
96
|
+
* normally belongs to a single node, but can recur later if two nodes resolve
|
|
97
|
+
* to the same frames (e.g. when source maps map distinct generated locations
|
|
98
|
+
* to the same original location).
|
|
99
|
+
*/
|
|
100
|
+
export interface ProfileVisitor {
|
|
101
|
+
metadata(metadata: ProfileMetadata): void;
|
|
102
|
+
/**
|
|
103
|
+
* @returns the ID by which stacks will refer to this frame.
|
|
104
|
+
*/
|
|
105
|
+
frame(frame: Frame): number;
|
|
106
|
+
/**
|
|
107
|
+
* @param frameIds - the stack, leaf first, as IDs returned by frame(). The
|
|
108
|
+
* array is not reused by the caller, so the visitor may retain it.
|
|
109
|
+
* @returns the ID by which samples will refer to this stack.
|
|
110
|
+
*/
|
|
111
|
+
stack(frameIds: number[]): number;
|
|
112
|
+
sample(sample: NormalizedSample): void;
|
|
113
|
+
/** Called after the last sample, with what the visit found. */
|
|
114
|
+
end?(result: VisitResult): void;
|
|
115
|
+
}
|
|
116
|
+
export interface VisitResult {
|
|
117
|
+
/** True if at least one frame has {@link Frame.missingMapFile} set. */
|
|
118
|
+
hasMissingMapFiles: boolean;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* A profile captured from a profiler, to be visited later: normalized, and
|
|
122
|
+
* passed to a visitor, which chooses the output (e.g. a PprofProfileBuilder's
|
|
123
|
+
* or an OtelProfilesBuilder's). Capturing does only what has to happen when
|
|
124
|
+
* the profile is taken; visiting does the rest, so a caller can defer that
|
|
125
|
+
* work, e.g. until it can yield to the event loop. The options of the
|
|
126
|
+
* normalization are set when the profile is captured. Visiting doesn't change
|
|
127
|
+
* the profile, so it can be visited more than once.
|
|
128
|
+
*/
|
|
129
|
+
export interface CapturedProfile {
|
|
130
|
+
visit(visitor: ProfileVisitor): VisitResult;
|
|
131
|
+
}
|
|
132
|
+
export interface TimeProfileNormalizerOptions {
|
|
133
|
+
/** Average time in microseconds between samples, as requested. */
|
|
134
|
+
intervalMicros: number;
|
|
135
|
+
sourceMapper?: SourceMapper;
|
|
136
|
+
/**
|
|
137
|
+
* Recompute the sampling interval from the profile duration and total hit
|
|
138
|
+
* count, bounded to [intervalMicros, 2 * intervalMicros].
|
|
139
|
+
*/
|
|
140
|
+
recomputeSamplingInterval?: boolean;
|
|
141
|
+
generateLabels?: GenerateTimeLabelsFunction;
|
|
142
|
+
/**
|
|
143
|
+
* Emit a separate, timestamped sample for every context, even when it has
|
|
144
|
+
* no labels. By default, the hits and CPU time of contexts without labels
|
|
145
|
+
* are aggregated into a single sample per node, without a timestamp. Set
|
|
146
|
+
* this when every sample should have a timestamp, e.g. for OTel profiles,
|
|
147
|
+
* whose samples should all have the same shape. Hits without a context
|
|
148
|
+
* (e.g. when contexts are not collected) are still aggregated.
|
|
149
|
+
*/
|
|
150
|
+
samplePerContext?: boolean;
|
|
151
|
+
}
|
|
152
|
+
export interface HeapProfileNormalizerOptions {
|
|
153
|
+
/** Start time of profile, in nanoseconds since epoch. */
|
|
154
|
+
startTimeNanos: bigint;
|
|
155
|
+
/** Bytes allocated between samples. */
|
|
156
|
+
intervalBytes: number;
|
|
157
|
+
ignoreSamplesPath?: string;
|
|
158
|
+
sourceMapper?: SourceMapper;
|
|
159
|
+
generateLabels?: GenerateAllocationLabelsFunction;
|
|
160
|
+
/** Root was collected in allocation mode (nodes carry allocation stats). */
|
|
161
|
+
allocations?: boolean;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* @returns the frame's function name, or a synthesized
|
|
165
|
+
* `(anonymous:L#line:C#column)` name for anonymous functions.
|
|
166
|
+
*/
|
|
167
|
+
export declare function frameFunctionName(frame: Frame): string;
|
|
168
|
+
export declare function computeTotalHitCount(root: TimeProfileNode): number;
|
|
169
|
+
/**
|
|
170
|
+
* Normalizes a V8 time profile and passes it to the visitor.
|
|
171
|
+
*
|
|
172
|
+
* Sample values are [sample count, wall nanoseconds] plus CPU nanoseconds if
|
|
173
|
+
* the profile has CPU time.
|
|
174
|
+
*/
|
|
175
|
+
export declare function visitTimeProfile(prof: TimeProfile, visitor: ProfileVisitor, options: TimeProfileNormalizerOptions): VisitResult;
|
|
176
|
+
/**
|
|
177
|
+
* Normalizes a V8 heap profile and passes it to the visitor.
|
|
178
|
+
*
|
|
179
|
+
* Sample values are [inuse objects, inuse bytes], or [inuse objects, alloc
|
|
180
|
+
* objects, inuse bytes, alloc bytes] in allocation mode.
|
|
181
|
+
*/
|
|
182
|
+
export declare function visitHeapProfile(prof: AllocationProfileNode | AllocationProfileNodeWithStats, visitor: ProfileVisitor, options: HeapProfileNormalizerOptions): VisitResult;
|