|
1 | 1 | /* eslint-disable max-lines */ |
2 | | -import type { DebugImage, Envelope, Event, EventEnvelope, Profile, Span, ThreadCpuProfile } from '@sentry/core'; |
| 2 | +import type { |
| 3 | + Client, |
| 4 | + ContinuousThreadCpuProfile, |
| 5 | + DebugImage, |
| 6 | + Envelope, |
| 7 | + Event, |
| 8 | + EventEnvelope, |
| 9 | + Profile, |
| 10 | + ProfileChunk, |
| 11 | + Span, |
| 12 | + ThreadCpuProfile, |
| 13 | +} from '@sentry/core'; |
3 | 14 | import { |
4 | 15 | browserPerformanceTimeOrigin, |
5 | 16 | debug, |
@@ -196,6 +207,109 @@ export function createProfilePayload( |
196 | 207 | return profile; |
197 | 208 | } |
198 | 209 |
|
| 210 | +/** |
| 211 | + * Create a profile chunk envelope item |
| 212 | + */ |
| 213 | +export function createProfileChunkPayload( |
| 214 | + jsSelfProfile: JSSelfProfile, |
| 215 | + client: Client, |
| 216 | + profilerId?: string, |
| 217 | +): ProfileChunk { |
| 218 | + if (jsSelfProfile === undefined || jsSelfProfile === null) { |
| 219 | + throw new TypeError( |
| 220 | + `Cannot construct profiling event envelope without a valid profile. Got ${jsSelfProfile} instead.`, |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + const continuousProfile = convertToContinuousProfile(jsSelfProfile); |
| 225 | + |
| 226 | + const options = client.getOptions(); |
| 227 | + const sdk = client.getSdkMetadata?.()?.sdk; |
| 228 | + |
| 229 | + return { |
| 230 | + chunk_id: uuid4(), |
| 231 | + client_sdk: { |
| 232 | + name: sdk?.name ?? 'sentry.javascript.browser', |
| 233 | + version: sdk?.version ?? '0.0.0', |
| 234 | + }, |
| 235 | + profiler_id: profilerId || uuid4(), |
| 236 | + platform: 'javascript', |
| 237 | + version: '2', |
| 238 | + release: options.release ?? '', |
| 239 | + environment: options.environment ?? 'production', |
| 240 | + debug_meta: { images: applyDebugMetadata([]) }, |
| 241 | + profile: continuousProfile, |
| 242 | + }; |
| 243 | +} |
| 244 | + |
| 245 | +/** |
| 246 | + * Convert from JSSelfProfile format to ContinuousThreadCpuProfile format. |
| 247 | + */ |
| 248 | +function convertToContinuousProfile(input: { |
| 249 | + frames: { name: string; resourceId?: number; line?: number; column?: number }[]; |
| 250 | + stacks: { frameId: number; parentId?: number }[]; |
| 251 | + samples: { timestamp: number; stackId?: number }[]; |
| 252 | + resources: string[]; |
| 253 | +}): ContinuousThreadCpuProfile { |
| 254 | + // Frames map 1:1 by index; fill only when present to avoid sparse writes |
| 255 | + const frames: ContinuousThreadCpuProfile['frames'] = []; |
| 256 | + for (let i = 0; i < input.frames.length; i++) { |
| 257 | + const frame = input.frames[i]; |
| 258 | + if (!frame) { |
| 259 | + continue; |
| 260 | + } |
| 261 | + frames[i] = { |
| 262 | + function: frame.name, |
| 263 | + abs_path: typeof frame.resourceId === 'number' ? input.resources[frame.resourceId] : undefined, |
| 264 | + lineno: frame.line, |
| 265 | + colno: frame.column, |
| 266 | + }; |
| 267 | + } |
| 268 | + |
| 269 | + // Build stacks by following parent links, top->down order (root last) |
| 270 | + const stacks: ContinuousThreadCpuProfile['stacks'] = []; |
| 271 | + for (let i = 0; i < input.stacks.length; i++) { |
| 272 | + const stackHead = input.stacks[i]; |
| 273 | + if (!stackHead) { |
| 274 | + continue; |
| 275 | + } |
| 276 | + const list: number[] = []; |
| 277 | + let current: { frameId: number; parentId?: number } | undefined = stackHead; |
| 278 | + while (current) { |
| 279 | + list.push(current.frameId); |
| 280 | + current = current.parentId === undefined ? undefined : input.stacks[current.parentId]; |
| 281 | + } |
| 282 | + stacks[i] = list; |
| 283 | + } |
| 284 | + |
| 285 | + // Align timestamps to SDK time origin to match span/event timelines |
| 286 | + const perfOrigin = browserPerformanceTimeOrigin(); |
| 287 | + const origin = typeof performance.timeOrigin === 'number' ? performance.timeOrigin : perfOrigin || 0; |
| 288 | + const adjustForOriginChange = origin - (perfOrigin || origin); |
| 289 | + |
| 290 | + const samples: ContinuousThreadCpuProfile['samples'] = []; |
| 291 | + for (let i = 0; i < input.samples.length; i++) { |
| 292 | + const sample = input.samples[i]; |
| 293 | + if (!sample) { |
| 294 | + continue; |
| 295 | + } |
| 296 | + // Convert ms to seconds epoch-based timestamp |
| 297 | + const timestampSeconds = (origin + (sample.timestamp - adjustForOriginChange)) / 1000; |
| 298 | + samples[i] = { |
| 299 | + stack_id: sample.stackId ?? 0, |
| 300 | + thread_id: THREAD_ID_STRING, |
| 301 | + timestamp: timestampSeconds, |
| 302 | + }; |
| 303 | + } |
| 304 | + |
| 305 | + return { |
| 306 | + frames, |
| 307 | + stacks, |
| 308 | + samples, |
| 309 | + thread_metadata: { [THREAD_ID_STRING]: { name: THREAD_NAME } }, |
| 310 | + }; |
| 311 | +} |
| 312 | + |
199 | 313 | /** |
200 | 314 | * |
201 | 315 | */ |
|
0 commit comments