summaryrefslogtreecommitdiffstats
path: root/toolkit/crashreporter/client/app/src/net/legacy_telemetry.rs
blob: 680f1614b03a6de2577683db96bf2df4cef327aa (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

//! Support for legacy telemetry ping creation. The ping support serialization which should be used
//! when submitting.

use anyhow::Context;
use serde::Serialize;
use std::collections::BTreeMap;
use uuid::Uuid;

const TELEMETRY_VERSION: u64 = 4;
const PAYLOAD_VERSION: u64 = 1;

// Generated by `build.rs`.
// static PING_ANNOTATIONS: phf::Set<&'static str>;
include!(concat!(env!("OUT_DIR"), "/ping_annotations.rs"));

#[derive(Serialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum Ping<'a> {
    Crash {
        id: Uuid,
        version: u64,
        #[serde(with = "time::serde::rfc3339")]
        creation_date: time::OffsetDateTime,
        client_id: &'a str,
        #[serde(skip_serializing_if = "serde_json::Value::is_null")]
        environment: serde_json::Value,
        payload: Payload<'a>,
        application: Application<'a>,
    },
}

time::serde::format_description!(date_format, Date, "[year]-[month]-[day]");

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Payload<'a> {
    session_id: &'a str,
    version: u64,
    #[serde(with = "date_format")]
    crash_date: time::Date,
    #[serde(with = "time::serde::rfc3339")]
    crash_time: time::OffsetDateTime,
    has_crash_environment: bool,
    crash_id: &'a str,
    minidump_sha256_hash: Option<&'a str>,
    process_type: &'a str,
    #[serde(skip_serializing_if = "serde_json::Value::is_null")]
    stack_traces: serde_json::Value,
    metadata: BTreeMap<&'a str, &'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Application<'a> {
    vendor: &'a str,
    name: &'a str,
    build_id: &'a str,
    display_version: String,
    platform_version: String,
    version: &'a str,
    channel: &'a str,
    #[serde(skip_serializing_if = "Option::is_none")]
    architecture: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    xpcom_abi: Option<String>,
}

impl<'a> Ping<'a> {
    pub fn crash(
        extra: &'a serde_json::Value,
        crash_id: &'a str,
        minidump_sha256_hash: Option<&'a str>,
    ) -> anyhow::Result<Self> {
        let now: time::OffsetDateTime = crate::std::time::SystemTime::now().into();
        let environment: serde_json::Value = extra["TelemetryEnvironment"]
            .as_str()
            .and_then(|estr| serde_json::from_str(estr).ok())
            .unwrap_or_default();

        // The subset of extra file entries (crash annotations) which are allowed in pings.
        let metadata = extra
            .as_object()
            .map(|map| {
                map.iter()
                    .filter_map(|(k, v)| {
                        PING_ANNOTATIONS
                            .contains(k)
                            .then(|| k.as_str())
                            .zip(v.as_str())
                    })
                    .collect()
            })
            .unwrap_or_default();

        let display_version = environment
            .pointer("/build/displayVersion")
            .and_then(|s| s.as_str())
            .unwrap_or_default()
            .to_owned();
        let platform_version = environment
            .pointer("/build/platformVersion")
            .and_then(|s| s.as_str())
            .unwrap_or_default()
            .to_owned();
        let architecture = environment
            .pointer("/build/architecture")
            .and_then(|s| s.as_str())
            .map(ToOwned::to_owned);
        let xpcom_abi = environment
            .pointer("/build/xpcomAbi")
            .and_then(|s| s.as_str())
            .map(ToOwned::to_owned);

        Ok(Ping::Crash {
            id: crate::std::mock::hook(Uuid::new_v4(), "ping_uuid"),
            version: TELEMETRY_VERSION,
            creation_date: now,
            client_id: extra["TelemetryClientId"]
                .as_str()
                .context("missing TelemetryClientId")?,
            environment,
            payload: Payload {
                session_id: extra["TelemetrySessionId"]
                    .as_str()
                    .context("missing TelemetrySessionId")?,
                version: PAYLOAD_VERSION,
                crash_date: now.date(),
                crash_time: now,
                has_crash_environment: true,
                crash_id,
                minidump_sha256_hash,
                process_type: "main",
                stack_traces: extra["StackTraces"].clone(),
                metadata,
            },
            application: Application {
                vendor: extra["Vendor"].as_str().unwrap_or_default(),
                name: extra["ProductName"].as_str().unwrap_or_default(),
                build_id: extra["BuildID"].as_str().unwrap_or_default(),
                display_version,
                platform_version,
                version: extra["Version"].as_str().unwrap_or_default(),
                channel: extra["ReleaseChannel"].as_str().unwrap_or_default(),
                architecture,
                xpcom_abi,
            },
        })
    }

    /// Generate the telemetry URL for submitting this ping.
    pub fn submission_url(&self, extra: &serde_json::Value) -> anyhow::Result<String> {
        let url = extra["TelemetryServerURL"]
            .as_str()
            .context("missing TelemetryServerURL")?;
        let id = self.id();
        let name = extra["ProductName"]
            .as_str()
            .context("missing ProductName")?;
        let version = extra["Version"].as_str().context("missing Version")?;
        let channel = extra["ReleaseChannel"]
            .as_str()
            .context("missing ReleaseChannel")?;
        let buildid = extra["BuildID"].as_str().context("missing BuildID")?;
        Ok(format!("{url}/submit/telemetry/{id}/crash/{name}/{version}/{channel}/{buildid}?v={TELEMETRY_VERSION}"))
    }

    /// Get the ping identifier.
    pub fn id(&self) -> &Uuid {
        match self {
            Ping::Crash { id, .. } => id,
        }
    }
}