-
-
Notifications
You must be signed in to change notification settings - Fork 427
/
ProfilingTraceData.java
620 lines (546 loc) · 20.8 KB
/
ProfilingTraceData.java
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
package io.sentry;
import io.sentry.profilemeasurements.ProfileMeasurement;
import io.sentry.vendor.gson.stream.JsonToken;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ApiStatus.Internal
public final class ProfilingTraceData implements JsonUnknown, JsonSerializable {
/**
* Default value for {@link SentryEvent#getEnvironment()} set when both event and {@link
* SentryOptions} do not have the environment field set.
*/
private static final String DEFAULT_ENVIRONMENT = "production";
@ApiStatus.Internal public static final String TRUNCATION_REASON_NORMAL = "normal";
@ApiStatus.Internal public static final String TRUNCATION_REASON_TIMEOUT = "timeout";
// Backgrounded reason is not used, yet, but it's one of the possible values
@ApiStatus.Internal public static final String TRUNCATION_REASON_BACKGROUNDED = "backgrounded";
private final @NotNull File traceFile;
private final @NotNull Callable<List<Integer>> deviceCpuFrequenciesReader;
// Device metadata
private int androidApiLevel;
private @NotNull String deviceLocale;
private @NotNull String deviceManufacturer;
private @NotNull String deviceModel;
private @NotNull String deviceOsBuildNumber;
private @NotNull String deviceOsName;
private @NotNull String deviceOsVersion;
private boolean deviceIsEmulator;
private @NotNull String cpuArchitecture;
private @NotNull List<Integer> deviceCpuFrequencies = new ArrayList<>();
private @NotNull String devicePhysicalMemoryBytes;
private @NotNull String platform;
private @NotNull String buildId;
// Transaction info
private @NotNull List<ProfilingTransactionData> transactions;
private @NotNull String transactionName;
// duration_ns is a String to avoid issues with numbers and json
private @NotNull String durationNs;
// App info
private @NotNull String versionCode;
private @NotNull String release;
// Stacktrace context
private @NotNull String transactionId;
private @NotNull String traceId;
private @NotNull String profileId;
private @NotNull String environment;
private @NotNull String truncationReason;
private final @NotNull Map<String, ProfileMeasurement> measurementsMap;
// Stacktrace (file)
/** Profile trace encoded with Base64 */
private @Nullable String sampledProfile = null;
private ProfilingTraceData() {
this(new File("dummy"), NoOpTransaction.getInstance());
}
public ProfilingTraceData(
final @NotNull File traceFile, final @NotNull ITransaction transaction) {
this(
traceFile,
new ArrayList<>(),
transaction,
"0",
0,
"",
// Don't use method reference. This can cause issues on Android
() -> new ArrayList<>(),
null,
null,
null,
null,
null,
null,
null,
null,
TRUNCATION_REASON_NORMAL,
new HashMap<>());
}
public ProfilingTraceData(
@NotNull File traceFile,
@NotNull List<ProfilingTransactionData> transactions,
@NotNull ITransaction transaction,
@NotNull String durationNanos,
int sdkInt,
@NotNull String cpuArchitecture,
@NotNull Callable<List<Integer>> deviceCpuFrequenciesReader,
@Nullable String deviceManufacturer,
@Nullable String deviceModel,
@Nullable String deviceOsVersion,
@Nullable Boolean deviceIsEmulator,
@Nullable String devicePhysicalMemoryBytes,
@Nullable String buildId,
@Nullable String release,
@Nullable String environment,
@NotNull String truncationReason,
final @NotNull Map<String, ProfileMeasurement> measurementsMap) {
this.traceFile = traceFile;
this.cpuArchitecture = cpuArchitecture;
this.deviceCpuFrequenciesReader = deviceCpuFrequenciesReader;
// Device metadata
this.androidApiLevel = sdkInt;
this.deviceLocale = Locale.getDefault().toString();
this.deviceManufacturer = deviceManufacturer != null ? deviceManufacturer : "";
this.deviceModel = deviceModel != null ? deviceModel : "";
this.deviceOsVersion = deviceOsVersion != null ? deviceOsVersion : "";
this.deviceIsEmulator = deviceIsEmulator != null ? deviceIsEmulator : false;
this.devicePhysicalMemoryBytes =
devicePhysicalMemoryBytes != null ? devicePhysicalMemoryBytes : "0";
this.deviceOsBuildNumber = "";
this.deviceOsName = "android";
this.platform = "android";
this.buildId = buildId != null ? buildId : "";
// Transaction info
this.transactions = transactions;
this.transactionName = transaction.getName();
this.durationNs = durationNanos;
// App info
this.versionCode = "";
this.release = release != null ? release : "";
// Stacktrace context
this.transactionId = transaction.getEventId().toString();
this.traceId = transaction.getSpanContext().getTraceId().toString();
this.profileId = UUID.randomUUID().toString();
this.environment = environment != null ? environment : DEFAULT_ENVIRONMENT;
this.truncationReason = truncationReason;
if (!isTruncationReasonValid()) {
this.truncationReason = TRUNCATION_REASON_NORMAL;
}
this.measurementsMap = measurementsMap;
}
private boolean isTruncationReasonValid() {
return truncationReason.equals(TRUNCATION_REASON_NORMAL)
|| truncationReason.equals(TRUNCATION_REASON_TIMEOUT)
|| truncationReason.equals(TRUNCATION_REASON_BACKGROUNDED);
}
private @Nullable Map<String, Object> unknown;
public @NotNull File getTraceFile() {
return traceFile;
}
public int getAndroidApiLevel() {
return androidApiLevel;
}
public @NotNull String getCpuArchitecture() {
return cpuArchitecture;
}
public @NotNull String getDeviceLocale() {
return deviceLocale;
}
public @NotNull String getDeviceManufacturer() {
return deviceManufacturer;
}
public @NotNull String getDeviceModel() {
return deviceModel;
}
public @NotNull String getDeviceOsBuildNumber() {
return deviceOsBuildNumber;
}
public @NotNull String getDeviceOsName() {
return deviceOsName;
}
public @NotNull String getDeviceOsVersion() {
return deviceOsVersion;
}
public boolean isDeviceIsEmulator() {
return deviceIsEmulator;
}
public @NotNull String getPlatform() {
return platform;
}
public @NotNull String getBuildId() {
return buildId;
}
public @NotNull String getTransactionName() {
return transactionName;
}
public @NotNull String getRelease() {
return release;
}
public @NotNull String getTransactionId() {
return transactionId;
}
public @NotNull List<ProfilingTransactionData> getTransactions() {
return transactions;
}
public @NotNull String getTraceId() {
return traceId;
}
public @NotNull String getProfileId() {
return profileId;
}
public @NotNull String getEnvironment() {
return environment;
}
public @Nullable String getSampledProfile() {
return sampledProfile;
}
public @NotNull String getDurationNs() {
return durationNs;
}
public @NotNull List<Integer> getDeviceCpuFrequencies() {
return deviceCpuFrequencies;
}
public @NotNull String getDevicePhysicalMemoryBytes() {
return devicePhysicalMemoryBytes;
}
public @NotNull String getTruncationReason() {
return truncationReason;
}
public @NotNull Map<String, ProfileMeasurement> getMeasurementsMap() {
return measurementsMap;
}
public void setAndroidApiLevel(final int androidApiLevel) {
this.androidApiLevel = androidApiLevel;
}
public void setCpuArchitecture(final @NotNull String cpuArchitecture) {
this.cpuArchitecture = cpuArchitecture;
}
public void setDeviceLocale(final @NotNull String deviceLocale) {
this.deviceLocale = deviceLocale;
}
public void setDeviceManufacturer(final @NotNull String deviceManufacturer) {
this.deviceManufacturer = deviceManufacturer;
}
public void setDeviceModel(final @NotNull String deviceModel) {
this.deviceModel = deviceModel;
}
public void setDeviceOsBuildNumber(final @NotNull String deviceOsBuildNumber) {
this.deviceOsBuildNumber = deviceOsBuildNumber;
}
public void setDeviceOsVersion(final @NotNull String deviceOsVersion) {
this.deviceOsVersion = deviceOsVersion;
}
public void setDeviceIsEmulator(final boolean deviceIsEmulator) {
this.deviceIsEmulator = deviceIsEmulator;
}
public void setDeviceCpuFrequencies(final @NotNull List<Integer> deviceCpuFrequencies) {
this.deviceCpuFrequencies = deviceCpuFrequencies;
}
public void setDevicePhysicalMemoryBytes(final @NotNull String devicePhysicalMemoryBytes) {
this.devicePhysicalMemoryBytes = devicePhysicalMemoryBytes;
}
public void setTruncationReason(final @NotNull String truncationReason) {
this.truncationReason = truncationReason;
}
public void setTransactions(final @NotNull List<ProfilingTransactionData> transactions) {
this.transactions = transactions;
}
public void setBuildId(final @NotNull String buildId) {
this.buildId = buildId;
}
public void setTransactionName(final @NotNull String transactionName) {
this.transactionName = transactionName;
}
public void setDurationNs(final @NotNull String durationNs) {
this.durationNs = durationNs;
}
public void setRelease(@NotNull String release) {
this.release = release;
}
public void setTransactionId(final @NotNull String transactionId) {
this.transactionId = transactionId;
}
public void setTraceId(final @NotNull String traceId) {
this.traceId = traceId;
}
public void setProfileId(final @NotNull String profileId) {
this.profileId = profileId;
}
public void setEnvironment(final @NotNull String environment) {
this.environment = environment;
}
public void setSampledProfile(final @Nullable String sampledProfile) {
this.sampledProfile = sampledProfile;
}
public void readDeviceCpuFrequencies() {
try {
this.deviceCpuFrequencies = deviceCpuFrequenciesReader.call();
} catch (Throwable ignored) {
// should never happen
}
}
// JsonSerializable
public static final class JsonKeys {
public static final String ANDROID_API_LEVEL = "android_api_level";
public static final String DEVICE_LOCALE = "device_locale";
public static final String DEVICE_MANUFACTURER = "device_manufacturer";
public static final String DEVICE_MODEL = "device_model";
public static final String DEVICE_OS_BUILD_NUMBER = "device_os_build_number";
public static final String DEVICE_OS_NAME = "device_os_name";
public static final String DEVICE_OS_VERSION = "device_os_version";
public static final String DEVICE_IS_EMULATOR = "device_is_emulator";
public static final String ARCHITECTURE = "architecture";
public static final String DEVICE_CPU_FREQUENCIES = "device_cpu_frequencies";
public static final String DEVICE_PHYSICAL_MEMORY_BYTES = "device_physical_memory_bytes";
public static final String PLATFORM = "platform";
public static final String BUILD_ID = "build_id";
public static final String TRANSACTION_NAME = "transaction_name";
public static final String DURATION_NS = "duration_ns";
public static final String RELEASE = "version_name";
public static final String VERSION_CODE = "version_code";
public static final String TRANSACTION_LIST = "transactions";
public static final String TRANSACTION_ID = "transaction_id";
public static final String TRACE_ID = "trace_id";
public static final String PROFILE_ID = "profile_id";
public static final String ENVIRONMENT = "environment";
public static final String SAMPLED_PROFILE = "sampled_profile";
public static final String TRUNCATION_REASON = "truncation_reason";
public static final String MEASUREMENTS = "measurements";
}
@Override
public void serialize(final @NotNull JsonObjectWriter writer, final @NotNull ILogger logger)
throws IOException {
writer.beginObject();
writer.name(JsonKeys.ANDROID_API_LEVEL).value(logger, androidApiLevel);
writer.name(JsonKeys.DEVICE_LOCALE).value(logger, deviceLocale);
writer.name(JsonKeys.DEVICE_MANUFACTURER).value(deviceManufacturer);
writer.name(JsonKeys.DEVICE_MODEL).value(deviceModel);
writer.name(JsonKeys.DEVICE_OS_BUILD_NUMBER).value(deviceOsBuildNumber);
writer.name(JsonKeys.DEVICE_OS_NAME).value(deviceOsName);
writer.name(JsonKeys.DEVICE_OS_VERSION).value(deviceOsVersion);
writer.name(JsonKeys.DEVICE_IS_EMULATOR).value(deviceIsEmulator);
writer.name(JsonKeys.ARCHITECTURE).value(logger, cpuArchitecture);
// Backend expects the list of frequencies, even if empty
writer.name(JsonKeys.DEVICE_CPU_FREQUENCIES).value(logger, deviceCpuFrequencies);
writer.name(JsonKeys.DEVICE_PHYSICAL_MEMORY_BYTES).value(devicePhysicalMemoryBytes);
writer.name(JsonKeys.PLATFORM).value(platform);
writer.name(JsonKeys.BUILD_ID).value(buildId);
writer.name(JsonKeys.TRANSACTION_NAME).value(transactionName);
writer.name(JsonKeys.DURATION_NS).value(durationNs);
writer.name(JsonKeys.RELEASE).value(release);
writer.name(JsonKeys.VERSION_CODE).value(versionCode);
if (!transactions.isEmpty()) {
writer.name(JsonKeys.TRANSACTION_LIST).value(logger, transactions);
}
writer.name(JsonKeys.TRANSACTION_ID).value(transactionId);
writer.name(JsonKeys.TRACE_ID).value(traceId);
writer.name(JsonKeys.PROFILE_ID).value(profileId);
writer.name(JsonKeys.ENVIRONMENT).value(environment);
writer.name(JsonKeys.TRUNCATION_REASON).value(truncationReason);
if (sampledProfile != null) {
writer.name(JsonKeys.SAMPLED_PROFILE).value(sampledProfile);
}
writer.name(JsonKeys.MEASUREMENTS).value(logger, measurementsMap);
if (unknown != null) {
for (String key : unknown.keySet()) {
Object value = unknown.get(key);
writer.name(key);
writer.value(logger, value);
}
}
writer.endObject();
}
@Nullable
@Override
public Map<String, Object> getUnknown() {
return unknown;
}
@Override
public void setUnknown(final @Nullable Map<String, Object> unknown) {
this.unknown = unknown;
}
public static final class Deserializer implements JsonDeserializer<ProfilingTraceData> {
@SuppressWarnings("unchecked")
@Override
public @NotNull ProfilingTraceData deserialize(
final @NotNull JsonObjectReader reader, final @NotNull ILogger logger) throws Exception {
reader.beginObject();
ProfilingTraceData data = new ProfilingTraceData();
Map<String, Object> unknown = null;
while (reader.peek() == JsonToken.NAME) {
final String nextName = reader.nextName();
switch (nextName) {
case JsonKeys.ANDROID_API_LEVEL:
Integer apiLevel = reader.nextIntegerOrNull();
if (apiLevel != null) {
data.androidApiLevel = apiLevel;
}
break;
case JsonKeys.DEVICE_LOCALE:
String deviceLocale = reader.nextStringOrNull();
if (deviceLocale != null) {
data.deviceLocale = deviceLocale;
}
break;
case JsonKeys.DEVICE_MANUFACTURER:
String deviceManufacturer = reader.nextStringOrNull();
if (deviceManufacturer != null) {
data.deviceManufacturer = deviceManufacturer;
}
break;
case JsonKeys.DEVICE_MODEL:
String deviceModel = reader.nextStringOrNull();
if (deviceModel != null) {
data.deviceModel = deviceModel;
}
break;
case JsonKeys.DEVICE_OS_BUILD_NUMBER:
String deviceOsBuildNumber = reader.nextStringOrNull();
if (deviceOsBuildNumber != null) {
data.deviceOsBuildNumber = deviceOsBuildNumber;
}
break;
case JsonKeys.DEVICE_OS_NAME:
String deviceOsName = reader.nextStringOrNull();
if (deviceOsName != null) {
data.deviceOsName = deviceOsName;
}
break;
case JsonKeys.DEVICE_OS_VERSION:
String deviceOsVersion = reader.nextStringOrNull();
if (deviceOsVersion != null) {
data.deviceOsVersion = deviceOsVersion;
}
break;
case JsonKeys.DEVICE_IS_EMULATOR:
Boolean deviceIsEmulator = reader.nextBooleanOrNull();
if (deviceIsEmulator != null) {
data.deviceIsEmulator = deviceIsEmulator;
}
break;
case JsonKeys.ARCHITECTURE:
String cpuArchitecture = reader.nextStringOrNull();
if (cpuArchitecture != null) {
data.cpuArchitecture = cpuArchitecture;
}
break;
case JsonKeys.DEVICE_CPU_FREQUENCIES:
List<Integer> deviceCpuFrequencies = (List<Integer>) reader.nextObjectOrNull();
if (deviceCpuFrequencies != null) {
data.deviceCpuFrequencies = deviceCpuFrequencies;
}
break;
case JsonKeys.DEVICE_PHYSICAL_MEMORY_BYTES:
String devicePhysicalMemoryBytes = reader.nextStringOrNull();
if (devicePhysicalMemoryBytes != null) {
data.devicePhysicalMemoryBytes = devicePhysicalMemoryBytes;
}
break;
case JsonKeys.PLATFORM:
String platform = reader.nextStringOrNull();
if (platform != null) {
data.platform = platform;
}
break;
case JsonKeys.BUILD_ID:
String buildId = reader.nextStringOrNull();
if (buildId != null) {
data.buildId = buildId;
}
break;
case JsonKeys.TRANSACTION_NAME:
String transactionName = reader.nextStringOrNull();
if (transactionName != null) {
data.transactionName = transactionName;
}
break;
case JsonKeys.DURATION_NS:
String durationNs = reader.nextStringOrNull();
if (durationNs != null) {
data.durationNs = durationNs;
}
break;
case JsonKeys.VERSION_CODE:
String versionCode = reader.nextStringOrNull();
if (versionCode != null) {
data.versionCode = versionCode;
}
break;
case JsonKeys.RELEASE:
String versionName = reader.nextStringOrNull();
if (versionName != null) {
data.release = versionName;
}
break;
case JsonKeys.TRANSACTION_LIST:
List<ProfilingTransactionData> transactions =
reader.nextList(logger, new ProfilingTransactionData.Deserializer());
if (transactions != null) {
data.transactions.addAll(transactions);
}
break;
case JsonKeys.TRANSACTION_ID:
String transactionId = reader.nextStringOrNull();
if (transactionId != null) {
data.transactionId = transactionId;
}
break;
case JsonKeys.TRACE_ID:
String traceId = reader.nextStringOrNull();
if (traceId != null) {
data.traceId = traceId;
}
break;
case JsonKeys.PROFILE_ID:
String profileId = reader.nextStringOrNull();
if (profileId != null) {
data.profileId = profileId;
}
break;
case JsonKeys.ENVIRONMENT:
String environment = reader.nextStringOrNull();
if (environment != null) {
data.environment = environment;
}
break;
case JsonKeys.TRUNCATION_REASON:
String truncationReason = reader.nextStringOrNull();
if (truncationReason != null) {
data.truncationReason = truncationReason;
}
break;
case JsonKeys.MEASUREMENTS:
Map<String, ProfileMeasurement> measurements =
reader.nextMapOrNull(logger, new ProfileMeasurement.Deserializer());
if (measurements != null) {
data.measurementsMap.putAll(measurements);
}
break;
case JsonKeys.SAMPLED_PROFILE:
String sampledProfile = reader.nextStringOrNull();
if (sampledProfile != null) {
data.sampledProfile = sampledProfile;
}
break;
default:
if (unknown == null) {
unknown = new ConcurrentHashMap<>();
}
reader.nextUnknown(logger, unknown, nextName);
break;
}
}
data.setUnknown(unknown);
reader.endObject();
return data;
}
}
}