-
-
Notifications
You must be signed in to change notification settings - Fork 360
Expand file tree
/
Copy pathutils.ts
More file actions
209 lines (181 loc) · 6.26 KB
/
utils.ts
File metadata and controls
209 lines (181 loc) · 6.26 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
207
208
209
/* oxlint-disable eslint(complexity) */
import type { Envelope, Event, ThreadCpuProfile } from '@sentry/core';
import { debug, forEachEnvelopeItem } from '@sentry/core';
import type {
AndroidCombinedProfileEvent,
AndroidProfileEvent,
CombinedProfileEvent,
HermesProfileEvent,
ProfileEvent,
RawThreadCpuProfile,
} from './types';
import { getDefaultEnvironment } from '../utils/environment';
import { getDebugMetadata } from './debugid';
/**
*
*/
export function isValidProfile(profile: ThreadCpuProfile): profile is RawThreadCpuProfile & { profile_id: string } {
if (profile.samples.length <= 1) {
if (__DEV__) {
// 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 because it contains less than 2 samples');
}
return false;
}
return true;
}
/**
* Finds transactions with profile_id context in the envelope
* @param envelope
* @returns
*/
export function findProfiledTransactionsFromEnvelope(envelope: Envelope): Event[] {
const events: Event[] = [];
forEachEnvelopeItem(envelope, (item, type) => {
if (type !== 'transaction') {
return;
}
// First item is the type
for (let j = 1; j < item.length; j++) {
const event = item[j];
// @ts-expect-error accessing private property
// oxlint-disable-next-line typescript-eslint(no-unsafe-member-access)
if (event.contexts?.trace?.data?.profile_id) {
events.push(item[j] as Event);
}
}
});
return events;
}
/**
* Creates a profiling envelope item, if the profile does not pass validation, returns null.
* @param event
* @returns {ProfileEvent | null}
*/
export function enrichCombinedProfileWithEventContext(
profile_id: string,
profile: CombinedProfileEvent | AndroidCombinedProfileEvent,
event: Event,
): ProfileEvent | null {
if ('js_profile' in profile) {
return enrichAndroidProfileWithEventContext(profile_id, profile, event);
}
if (!profile.profile || !isValidProfile(profile.profile)) {
return null;
}
const trace_id = event.contexts?.trace?.trace_id || '';
// 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) {
if (__DEV__) {
debug.log(`[Profiling] Invalid traceId: ${trace_id} on profiled event`);
}
}
const { profilingStartTimestampNs, ...profileWithoutInternalFields } = profile;
return {
...profileWithoutInternalFields,
event_id: profile_id,
runtime: {
name: 'hermes',
version: '', // TODO: get hermes version
},
timestamp: profilingStartTimestampNs
? new Date(profilingStartTimestampNs / 1e6).toISOString()
: event.start_timestamp
? new Date(event.start_timestamp * 1000).toISOString()
: new Date().toISOString(),
release: event.release || '',
environment: event.environment || getDefaultEnvironment(),
os: {
name: event.contexts?.os?.name || '',
version: event.contexts?.os?.version || '',
build_number: event.contexts?.os?.build || '',
},
device: {
locale: (event.contexts?.device && (event.contexts.device.locale as string)) || '',
model: event.contexts?.device?.model || '',
manufacturer: event.contexts?.device?.manufacturer || '',
architecture: event.contexts?.device?.arch || '',
is_emulator: event.contexts?.device?.simulator || false,
},
transaction: {
name: event.transaction || '',
id: event.event_id || '',
trace_id,
active_thread_id: profile.transaction?.active_thread_id || '',
},
debug_meta: {
images: [...getDebugMetadata(), ...(profile.debug_meta?.images || [])],
},
};
}
/**
* Creates Android profiling envelope item.
*/
export function enrichAndroidProfileWithEventContext(
profile_id: string,
profile: AndroidCombinedProfileEvent,
event: Event,
): AndroidProfileEvent | null {
const { profilingStartTimestampNs, ...profileWithoutInternalFields } = profile;
return {
...profileWithoutInternalFields,
debug_meta: {
images: getDebugMetadata(),
},
build_id: profile.build_id || '',
device_cpu_frequencies: [],
device_is_emulator: event.contexts?.device?.simulator || false,
device_locale: (event.contexts?.device && (event.contexts.device.locale as string)) || '',
device_manufacturer: event.contexts?.device?.manufacturer || '',
device_model: event.contexts?.device?.model || '',
device_os_name: event.contexts?.os?.name || '',
device_os_version: event.contexts?.os?.version || '',
device_physical_memory_bytes:
(event.contexts?.device?.memory_size && Number(event.contexts.device.memory_size).toString(10)) || '',
environment: event.environment || getDefaultEnvironment(),
profile_id,
timestamp: profilingStartTimestampNs
? new Date(profilingStartTimestampNs / 1e6).toISOString()
: event.start_timestamp
? new Date(event.start_timestamp * 1000).toISOString()
: new Date().toISOString(),
release: event.release || '',
dist: event.dist || '',
transaction_id: event.event_id || '',
transaction_name: event.transaction || '',
trace_id: event.contexts?.trace?.trace_id || '',
version_name: event.release || '',
version_code: event.dist || '',
};
}
/**
* Creates profiling event compatible carrier Object from raw Hermes profile.
*/
export function createHermesProfilingEvent(profile: RawThreadCpuProfile): HermesProfileEvent {
return {
platform: 'javascript',
version: '1',
profile,
transaction: {
active_thread_id: profile.active_thread_id,
},
};
}
/**
* Adds items to envelope if they are not already present - mutates the envelope.
* @param envelope
*/
export function addProfilesToEnvelope(envelope: Envelope, profiles: ProfileEvent[]): Envelope {
if (!profiles.length) {
return envelope;
}
for (const profile of profiles) {
// @ts-expect-error untyped envelope
envelope[1].push([{ type: 'profile' }, profile]);
}
return envelope;
}