-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathutils.ts
More file actions
206 lines (188 loc) · 5.94 KB
/
Copy pathutils.ts
File metadata and controls
206 lines (188 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import type {
Client,
ContinuousThreadCpuProfile,
DebugImage,
DsnComponents,
EventEnvelopeHeaders,
ProfileChunk,
ProfileChunkEnvelope,
ProfileChunkItem,
SdkInfo,
} from '@sentry/core';
import { createEnvelope, debug, dsnToString, getDebugImagesForResources, uuid4 } from '@sentry/core';
import type { RawChunkCpuProfile } from '@sentry/node-cpu-profiler';
import { isMainThread, threadId } from 'worker_threads';
import { DEBUG_BUILD } from './debug-build';
// We require the file because if we import it, it will be included in the bundle.
// I guess tsc does not check file contents when it's imported.
export const PROFILER_THREAD_ID_STRING = String(threadId);
export const PROFILER_THREAD_NAME = isMainThread ? 'main' : 'worker';
const CONTINUOUS_FORMAT_VERSION = '2';
/**
* Checks if the profile is a raw profile or a profile enriched with thread information.
* @param {ContinuousThreadCpuProfile | RawChunkCpuProfile} profile
* @returns {boolean}
*/
function isRawThreadCpuProfile(
profile: ContinuousThreadCpuProfile | RawChunkCpuProfile,
): profile is RawChunkCpuProfile {
return !('thread_metadata' in profile);
}
/**
* Enriches the profile with threadId of the current thread.
* This is done in node as we seem to not be able to get the info from C native code.
*
* @param {ContinuousThreadCpuProfile | RawChunkCpuProfile} profile
* @returns {ContinuousThreadCpuProfile}
*/
export function enrichWithThreadInformation(
profile: ContinuousThreadCpuProfile | RawChunkCpuProfile,
): ContinuousThreadCpuProfile {
if (!isRawThreadCpuProfile(profile)) {
return profile;
}
return {
samples: profile.samples,
frames: profile.frames,
stacks: profile.stacks,
thread_metadata: {
[PROFILER_THREAD_ID_STRING]: {
name: PROFILER_THREAD_NAME,
},
},
};
}
/**
* Create a profile chunk from raw thread profile
* @param {RawChunkCpuProfile} cpuProfile
* @returns {ProfileChunk}
*/
function createProfileChunkPayload(
client: Client,
cpuProfile: RawChunkCpuProfile,
{
release,
environment,
trace_id,
profiler_id,
chunk_id,
sdk,
}: {
release: string;
environment: string;
trace_id: string | undefined;
chunk_id: string;
profiler_id: string;
sdk: SdkInfo | undefined;
},
): ProfileChunk {
// Log a warning if the profile has an invalid traceId (should be uuidv4).
// All profiles and transactions are rejected if this is the case and we want to
// warn users that this is happening if they enable debug flag
if (trace_id?.length !== 32) {
DEBUG_BUILD && debug.log(`[Profiling] Invalid traceId: ${trace_id} on profiled event`);
}
const enrichedThreadProfile = enrichWithThreadInformation(cpuProfile);
const profile: ProfileChunk = {
chunk_id: chunk_id,
client_sdk: {
name: sdk?.name ?? 'sentry.javascript.node',
version: sdk?.version ?? '0.0.0',
},
profiler_id: profiler_id,
platform: 'node',
version: CONTINUOUS_FORMAT_VERSION,
release: release,
environment: environment,
measurements: cpuProfile.measurements,
debug_meta: {
images: applyDebugMetadata(client, cpuProfile.resources),
},
profile: enrichedThreadProfile,
};
return profile;
}
/**
* Creates a profiling chunk envelope item, if the profile does not pass validation, returns null.
*/
export function createProfilingChunkEvent(
client: Client,
options: { release?: string; environment?: string },
profile: RawChunkCpuProfile,
sdk: SdkInfo | undefined,
identifiers: { trace_id: string | undefined; chunk_id: string; profiler_id: string },
): ProfileChunk | null {
if (!isValidProfileChunk(profile)) {
return null;
}
return createProfileChunkPayload(client, profile, {
release: options.release ?? '',
environment: options.environment ?? '',
trace_id: identifiers.trace_id ?? '',
chunk_id: identifiers.chunk_id,
profiler_id: identifiers.profiler_id,
sdk,
});
}
/**
* Checks if the profile chunk is valid and can be sent to Sentry.
* @param profile
* @returns
*/
export function isValidProfileChunk(profile: RawChunkCpuProfile): profile is RawChunkCpuProfile {
if (profile.samples.length <= 1) {
DEBUG_BUILD &&
// Log a warning if the profile has less than 2 samples so users can know why
// they are not seeing any profiling data and we cant avoid the back and forth
// of asking them to provide us with a dump of the profile data.
debug.log('[Profiling] Discarding profile chunk because it contains less than 2 samples');
return false;
}
return true;
}
/**
* Creates event envelope headers for a profile chunk. This is separate from createEventEnvelopeHeaders util
* as the profile chunk does not conform to the sentry event type
*/
export function createEventEnvelopeHeaders(
sdkInfo: SdkInfo | undefined,
tunnel: string | undefined,
dsn?: DsnComponents,
): EventEnvelopeHeaders {
return {
event_id: uuid4(),
sent_at: new Date().toISOString(),
...(sdkInfo && { sdk: sdkInfo }),
...(!!tunnel && dsn && { dsn: dsnToString(dsn) }),
};
}
/**
* Creates a standalone profile_chunk envelope.
*/
export function makeProfileChunkEnvelope(
platform: 'node',
chunk: ProfileChunk,
sdkInfo: SdkInfo | undefined,
tunnel: string | undefined,
dsn?: DsnComponents,
): ProfileChunkEnvelope {
const profileChunkHeader: ProfileChunkItem[0] = {
type: 'profile_chunk',
platform,
};
return createEnvelope<ProfileChunkEnvelope>(createEventEnvelopeHeaders(sdkInfo, tunnel, dsn), [
[profileChunkHeader, chunk],
]);
}
/**
* Cross reference profile collected resources with debug_ids and return a list of debug images.
* @param {string[]} resource_paths
* @returns {DebugImage[]}
*/
export function applyDebugMetadata(client: Client, resource_paths: ReadonlyArray<string>): DebugImage[] {
const options = client.getOptions();
if (!options?.stackParser) {
return [];
}
return getDebugImagesForResources(options.stackParser, resource_paths);
}