relay_profiling/android/
legacy.rs

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
//! Android Format
//!
//! Relay is expecting a JSON object with some mandatory metadata and a `sampled_profile` key
//! containing the raw Android profile.
//!
//! `android` has a specific binary representation of its profile and Relay is responsible to
//! unpack it before it's forwarded down the line.
//!
use std::collections::{BTreeMap, HashMap};

use android_trace_log::chrono::{DateTime, Utc};
use android_trace_log::{AndroidTraceLog, Clock, Vm};
use data_encoding::BASE64_NOPAD;
use relay_event_schema::protocol::{EventId, SpanId};
use serde::{Deserialize, Serialize};

use crate::measurements::LegacyMeasurement;
use crate::sample::v1::SampleProfile;
use crate::transaction_metadata::TransactionMetadata;
use crate::types::{ClientSdk, DebugMeta};
use crate::utils::{default_client_sdk, deserialize_number_from_string, is_zero};
use crate::{ProfileError, MAX_PROFILE_DURATION};

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ProfileMetadata {
    android_api_level: u16,

    #[serde(default, skip_serializing_if = "String::is_empty")]
    build_id: String,

    device_cpu_frequencies: Vec<u32>,
    device_is_emulator: bool,
    device_locale: String,
    device_manufacturer: String,
    device_model: String,
    device_os_name: String,
    device_os_version: String,

    #[serde(deserialize_with = "deserialize_number_from_string")]
    device_physical_memory_bytes: u64,

    #[serde(default, skip_serializing_if = "String::is_empty")]
    environment: String,

    platform: String,
    profile_id: EventId,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    timestamp: Option<DateTime<Utc>>,

    #[serde(default, skip_serializing_if = "String::is_empty")]
    release: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    dist: String,

    version_code: String,
    version_name: String,

    #[serde(default, skip_serializing_if = "EventId::is_nil")]
    transaction_id: EventId,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    transaction_name: String,
    #[serde(default, skip_serializing_if = "EventId::is_nil")]
    trace_id: EventId,
    #[serde(
        default,
        deserialize_with = "deserialize_number_from_string",
        skip_serializing_if = "is_zero"
    )]
    duration_ns: u64,
    #[serde(
        default,
        deserialize_with = "deserialize_number_from_string",
        skip_serializing_if = "is_zero"
    )]
    active_thread_id: u64,

    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    transactions: Vec<TransactionMetadata>,
    #[serde(skip_serializing_if = "Option::is_none")]
    transaction: Option<TransactionMetadata>,

    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    transaction_metadata: BTreeMap<String, String>,

    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    transaction_tags: BTreeMap<String, String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    debug_meta: Option<DebugMeta>,

    #[serde(skip_serializing_if = "Option::is_none")]
    client_sdk: Option<ClientSdk>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
struct AndroidProfilingEvent {
    #[serde(flatten)]
    metadata: ProfileMetadata,

    #[serde(default, skip_serializing)]
    sampled_profile: String,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    js_profile: Option<SampleProfile>,

    #[serde(default = "AndroidProfilingEvent::default")]
    profile: AndroidTraceLog,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    measurements: Option<HashMap<String, LegacyMeasurement>>,
}

impl AndroidProfilingEvent {
    fn default() -> AndroidTraceLog {
        AndroidTraceLog {
            data_file_overflow: Default::default(),
            clock: Clock::Global,
            elapsed_time: Default::default(),
            total_method_calls: Default::default(),
            clock_call_overhead: Default::default(),
            vm: Vm::Dalvik,
            start_time: Utc::now(),
            pid: Default::default(),
            gc_trace: Default::default(),
            threads: Default::default(),
            methods: Default::default(),
            events: Default::default(),
        }
    }

    fn parse(&mut self) -> Result<(), ProfileError> {
        let profile_bytes = match BASE64_NOPAD.decode(self.sampled_profile.as_bytes()) {
            Ok(profile) => profile,
            Err(_) => return Err(ProfileError::InvalidBase64Value),
        };
        self.profile = match android_trace_log::parse(&profile_bytes) {
            Ok(profile) => profile,
            Err(_) => return Err(ProfileError::InvalidSampledProfile),
        };
        Ok(())
    }

    fn has_transaction_metadata(&self) -> bool {
        !self.metadata.transaction_name.is_empty()
    }
}

fn parse_profile(payload: &[u8]) -> Result<AndroidProfilingEvent, ProfileError> {
    let d = &mut serde_json::Deserializer::from_slice(payload);
    let mut profile: AndroidProfilingEvent =
        serde_path_to_error::deserialize(d).map_err(ProfileError::InvalidJson)?;

    let transaction_opt = profile.metadata.transactions.drain(..).next();
    if let Some(transaction) = transaction_opt {
        if !transaction.valid() {
            return Err(ProfileError::InvalidTransactionMetadata);
        }

        // this is for compatibility
        profile.metadata.active_thread_id = transaction.active_thread_id;
        profile.metadata.trace_id = transaction.trace_id;
        profile.metadata.transaction_id = transaction.id;
        profile
            .metadata
            .transaction_name
            .clone_from(&transaction.name);

        profile.metadata.transaction = Some(transaction);
    } else if profile.has_transaction_metadata() {
        profile.metadata.transaction = Some(TransactionMetadata {
            active_thread_id: profile.metadata.active_thread_id,
            id: profile.metadata.transaction_id,
            name: profile.metadata.transaction_name.clone(),
            trace_id: profile.metadata.trace_id,
            ..Default::default()
        });
    } else {
        return Err(ProfileError::NoTransactionAssociated);
    }

    if let Some(ref mut js_profile) = profile.js_profile {
        js_profile.normalize(profile.metadata.platform.as_str(), "")?;
    }

    if !profile.sampled_profile.is_empty() {
        profile.parse()?;
    }

    if profile.profile.events.is_empty() {
        return Err(ProfileError::NotEnoughSamples);
    }

    if profile.profile.elapsed_time > MAX_PROFILE_DURATION {
        return Err(ProfileError::DurationIsTooLong);
    }

    if profile.profile.elapsed_time.is_zero() {
        return Err(ProfileError::DurationIsZero);
    }

    // Use duration given by the profiler and not reported by the SDK.
    profile.metadata.duration_ns = profile.profile.elapsed_time.as_nanos() as u64;

    Ok(profile)
}

pub fn parse_android_profile(
    payload: &[u8],
    transaction_metadata: BTreeMap<String, String>,
    transaction_tags: BTreeMap<String, String>,
) -> Result<Vec<u8>, ProfileError> {
    let mut profile = parse_profile(payload)?;

    if let Some(transaction_name) = transaction_metadata.get("transaction") {
        transaction_name.clone_into(&mut profile.metadata.transaction_name);

        if let Some(ref mut transaction) = profile.metadata.transaction {
            profile
                .metadata
                .transaction_name
                .clone_into(&mut transaction.name);
        }
    }

    if let Some(release) = transaction_metadata.get("release") {
        release.clone_into(&mut profile.metadata.release);
    }

    if let Some(dist) = transaction_metadata.get("dist") {
        dist.clone_into(&mut profile.metadata.dist);
    }

    if let Some(environment) = transaction_metadata.get("environment") {
        environment.clone_into(&mut profile.metadata.environment);
    }

    if let Some(segment_id) = transaction_metadata.get("segment_id") {
        if let Some(transaction_metadata) = profile.metadata.transaction.as_mut() {
            transaction_metadata.segment_id = Some(SpanId(segment_id.to_owned()));
        }
    }

    profile.metadata.client_sdk = match (
        transaction_metadata.get("client_sdk.name"),
        transaction_metadata.get("client_sdk.version"),
    ) {
        (Some(name), Some(version)) => Some(ClientSdk {
            name: name.to_owned(),
            version: version.to_owned(),
        }),
        _ => default_client_sdk(profile.metadata.platform.as_str()),
    };
    profile.metadata.transaction_metadata = transaction_metadata;
    profile.metadata.transaction_tags = transaction_tags;

    serde_json::to_vec(&profile).map_err(|_| ProfileError::CannotSerializePayload)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_roundtrip_android() {
        let payload = include_bytes!("../../tests/fixtures/android/legacy/roundtrip.json");
        let profile = parse_profile(payload);
        assert!(profile.is_ok());
        let data = serde_json::to_vec(&profile.unwrap());
        assert!(
            parse_android_profile(&(data.unwrap())[..], BTreeMap::new(), BTreeMap::new()).is_ok()
        );
    }

    #[test]
    fn test_roundtrip_react_native() {
        let payload = include_bytes!("../../tests/fixtures/android/legacy/roundtrip.rn.json");
        let profile = parse_profile(payload);
        assert!(profile.is_ok());
        let data = serde_json::to_vec(&profile.unwrap());
        assert!(
            parse_android_profile(&(data.unwrap())[..], BTreeMap::new(), BTreeMap::new()).is_ok()
        );
    }

    #[test]
    fn test_no_transaction() {
        let payload = include_bytes!("../../tests/fixtures/android/legacy/no_transaction.json");
        let data = parse_android_profile(payload, BTreeMap::new(), BTreeMap::new());
        assert!(data.is_err());
    }

    #[test]
    fn test_remove_invalid_events() {
        let payload =
            include_bytes!("../../tests/fixtures/android/legacy/remove_invalid_events.json");
        let data = parse_android_profile(payload, BTreeMap::new(), BTreeMap::new());
        assert!(data.is_err());
    }

    #[test]
    fn test_transactions_to_top_level() {
        let payload =
            include_bytes!("../../tests/fixtures/android/legacy/multiple_transactions.json");

        let profile = match parse_profile(payload) {
            Err(err) => panic!("cannot parse profile: {err:?}"),
            Ok(profile) => profile,
        };
        assert_eq!(
            profile.metadata.transaction_id,
            "7d6db784355e4d7dbf98671c69cbcc77".parse().unwrap()
        );
        assert_eq!(
            profile.metadata.trace_id,
            "7d6db784355e4d7dbf98671c69cbcc77".parse().unwrap()
        );
        assert_eq!(profile.metadata.active_thread_id, 12345);
        assert_eq!(profile.metadata.transaction_name, "transaction1");
    }

    #[test]
    fn test_extract_transaction_metadata() {
        let transaction_metadata = BTreeMap::from([
            ("release".to_string(), "some-random-release".to_string()),
            (
                "transaction".to_string(),
                "some-random-transaction".to_string(),
            ),
        ]);

        let payload = include_bytes!("../../tests/fixtures/android/legacy/valid.json");
        let profile_json = parse_android_profile(payload, transaction_metadata, BTreeMap::new());
        assert!(profile_json.is_ok());

        let payload = profile_json.unwrap();
        let d = &mut serde_json::Deserializer::from_slice(&payload[..]);
        let output: AndroidProfilingEvent = serde_path_to_error::deserialize(d)
            .map_err(ProfileError::InvalidJson)
            .unwrap();
        assert_eq!(output.metadata.release, "some-random-release".to_string());
        assert_eq!(
            output.metadata.transaction_name,
            "some-random-transaction".to_string()
        );

        if let Some(transaction) = output.metadata.transaction {
            assert_eq!(transaction.name, "some-random-transaction".to_string());
        }
    }

    #[test]
    fn test_timestamp() {
        let payload = include_bytes!("../../tests/fixtures/android/legacy/roundtrip.json");
        let profile = parse_profile(payload);

        assert!(profile.is_ok());
        assert!(profile.as_ref().unwrap().metadata.timestamp.is_none());

        let mut ap = profile.unwrap();
        let now = Some(Utc::now());
        ap.metadata.timestamp = now;
        let data = serde_json::to_vec(&ap);
        let updated = parse_profile(&(data.unwrap())[..]);

        assert!(updated.is_ok());
        assert_eq!(updated.unwrap().metadata.timestamp, now);
    }
}