• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / fx-private-relay / c261d5a6-4482-49d5-8f64-a949e5295e9d

18 Apr 2024 02:57PM UTC coverage: 75.479% (-0.1%) from 75.611%
c261d5a6-4482-49d5-8f64-a949e5295e9d

Pull #4612

circleci

rafeerahman
Linter and more test fixes
Pull Request #4612: MPP3779: E2E test fixes and additions

2443 of 3406 branches covered (71.73%)

Branch coverage included in aggregate %.

6767 of 8796 relevant lines covered (76.93%)

20.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/privaterelay/glean/server_events.py
1
"""
2
This Source Code Form is subject to the terms of the Mozilla Public
3
License, v. 2.0. If a copy of the MPL was not distributed with this
4
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5

6
AUTOGENERATED BY glean_parser v13.0.1. DO NOT EDIT. To recreate, run:
7

8
bash .circleci/python_job.bash run build_glean
9
"""
10

11
from __future__ import annotations
1✔
12
from datetime import datetime, timezone
1✔
13
from typing import Any
1✔
14
from uuid import uuid4
1✔
15
import json
1✔
16

17
GLEAN_EVENT_MOZLOG_TYPE = "glean-server-event"
1✔
18

19

20
class EventsServerEventLogger:
1✔
21
    def __init__(
1✔
22
        self, application_id: str, app_display_version: str, channel: str
23
    ) -> None:
24
        """
25
        Create EventsServerEventLogger instance.
26

27
        :param str application_id: The application ID.
28
        :param str app_display_version: The application display version.
29
        :param str channel: The channel.
30
        """
31
        self._application_id = application_id
1✔
32
        self._app_display_version = app_display_version
1✔
33
        self._channel = channel
1✔
34

35
    def _record(self, user_agent: str, ip_address: str, event: dict[str, Any]) -> None:
1✔
36
        now = datetime.now(timezone.utc)
1✔
37
        timestamp = now.isoformat()
1✔
38
        event["timestamp"] = int(1000.0 * now.timestamp())  # Milliseconds since epoch
1✔
39
        event_payload = {
1✔
40
            "metrics": {},
41
            "events": [event],
42
            "ping_info": {
43
                # seq is required in the Glean schema, however is not useful in server context
44
                "seq": 0,
45
                "start_time": timestamp,
46
                "end_time": timestamp,
47
            },
48
            # `Unknown` fields below are required in the Glean schema, however they are
49
            # not useful in server context
50
            "client_info": {
51
                "telemetry_sdk_build": "glean_parser v13.0.1",
52
                "first_run_date": "Unknown",
53
                "os": "Unknown",
54
                "os_version": "Unknown",
55
                "architecture": "Unknown",
56
                "app_build": "Unknown",
57
                "app_display_version": self._app_display_version,
58
                "app_channel": self._channel,
59
            },
60
        }
61
        event_payload_serialized = json.dumps(event_payload)
1✔
62

63
        # This is the message structure that Decoder expects:
64
        # https://github.com/mozilla/gcp-ingestion/pull/2400
65
        ping = {
1✔
66
            "document_namespace": self._application_id,
67
            "document_type": "events",
68
            "document_version": "1",
69
            "document_id": str(uuid4()),
70
            "user_agent": user_agent,
71
            "ip_address": ip_address,
72
            "payload": event_payload_serialized,
73
        }
74

75
        self.emit_record(now, ping)
1✔
76

77
    def emit_record(self, now: datetime, ping: dict[str, Any]) -> None:
1✔
78
        """Log the ping to STDOUT.
79
        Applications might want to override this method to use their own logging.
80
        If doing so, make sure to log the ping as JSON, and to include the
81
        `Type: GLEAN_EVENT_MOZLOG_TYPE`."""
82
        ping_envelope = {
×
83
            "Timestamp": now.isoformat(),
84
            "Logger": "glean",
85
            "Type": GLEAN_EVENT_MOZLOG_TYPE,
86
            "Fields": ping,
87
        }
88
        ping_envelope_serialized = json.dumps(ping_envelope)
×
89

90
        print(ping_envelope_serialized)
×
91

92
    def record_email_blocked(
1✔
93
        self,
94
        user_agent: str,
95
        ip_address: str,
96
        client_id: str,
97
        fxa_id: str,
98
        platform: str,
99
        n_random_masks: int,
100
        n_domain_masks: int,
101
        n_deleted_random_masks: int,
102
        n_deleted_domain_masks: int,
103
        date_joined_relay: int,
104
        premium_status: str,
105
        date_joined_premium: int,
106
        has_extension: bool,
107
        date_got_extension: int,
108
        mask_id: str,
109
        is_random_mask: bool,
110
        is_reply: bool,
111
        reason: str,
112
    ) -> None:
113
        """
114
        Record and submit a email_blocked event:
115
        Relay receives but does not forward an email for a Relay user.
116
        Event is logged to STDOUT via `print`.
117

118
        :param str user_agent: The user agent.
119
        :param str ip_address: The IP address. Will be used to decode Geo information
120
            and scrubbed at ingestion.
121
        :param str client_id: Firefox client ID
122
        :param str fxa_id: Mozilla accounts user ID
123
        :param str platform: Relay client platform
124
        :param int n_random_masks: Number of random masks
125
        :param int n_domain_masks: Number of premium subdomain masks
126
        :param int n_deleted_random_masks: Number of deleted random masks
127
        :param int n_deleted_domain_masks: Number of deleted domain masks
128
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
129
        :param str premium_status: Subscription type and term
130
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
131
        :param bool has_extension: The user has the Relay Add-on
132
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
133
        :param str mask_id: Mask ID, 'R' (random mask) or 'D' (domain mask) followed by a number.
134
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
135
        :param bool is_reply: The email is a reply from the Relay user
136
        :param str reason: Code describing why the email was blocked
137
        """
138
        event = {
1✔
139
            "category": "email",
140
            "name": "blocked",
141
            "extra": {
142
                "client_id": str(client_id),
143
                "fxa_id": str(fxa_id),
144
                "platform": str(platform),
145
                "n_random_masks": str(n_random_masks),
146
                "n_domain_masks": str(n_domain_masks),
147
                "n_deleted_random_masks": str(n_deleted_random_masks),
148
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
149
                "date_joined_relay": str(date_joined_relay),
150
                "premium_status": str(premium_status),
151
                "date_joined_premium": str(date_joined_premium),
152
                "has_extension": str(has_extension).lower(),
153
                "date_got_extension": str(date_got_extension),
154
                "mask_id": str(mask_id),
155
                "is_random_mask": str(is_random_mask).lower(),
156
                "is_reply": str(is_reply).lower(),
157
                "reason": str(reason),
158
            },
159
        }
160
        self._record(user_agent, ip_address, event)
1✔
161

162
    def record_email_forwarded(
1✔
163
        self,
164
        user_agent: str,
165
        ip_address: str,
166
        client_id: str,
167
        fxa_id: str,
168
        platform: str,
169
        n_random_masks: int,
170
        n_domain_masks: int,
171
        n_deleted_random_masks: int,
172
        n_deleted_domain_masks: int,
173
        date_joined_relay: int,
174
        premium_status: str,
175
        date_joined_premium: int,
176
        has_extension: bool,
177
        date_got_extension: int,
178
        mask_id: str,
179
        is_random_mask: bool,
180
        is_reply: bool,
181
    ) -> None:
182
        """
183
        Record and submit a email_forwarded event:
184
        Relay receives and forwards an email for a Relay user.
185
        Event is logged to STDOUT via `print`.
186

187
        :param str user_agent: The user agent.
188
        :param str ip_address: The IP address. Will be used to decode Geo information
189
            and scrubbed at ingestion.
190
        :param str client_id: Firefox client ID
191
        :param str fxa_id: Mozilla accounts user ID
192
        :param str platform: Relay client platform
193
        :param int n_random_masks: Number of random masks
194
        :param int n_domain_masks: Number of premium subdomain masks
195
        :param int n_deleted_random_masks: Number of deleted random masks
196
        :param int n_deleted_domain_masks: Number of deleted domain masks
197
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
198
        :param str premium_status: Subscription type and term
199
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
200
        :param bool has_extension: The user has the Relay Add-on
201
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
202
        :param str mask_id: Mask ID, 'R' (random mask) or 'D' (domain mask) followed by a number.
203
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
204
        :param bool is_reply: The email is a reply from the Relay user
205
        """
206
        event = {
1✔
207
            "category": "email",
208
            "name": "forwarded",
209
            "extra": {
210
                "client_id": str(client_id),
211
                "fxa_id": str(fxa_id),
212
                "platform": str(platform),
213
                "n_random_masks": str(n_random_masks),
214
                "n_domain_masks": str(n_domain_masks),
215
                "n_deleted_random_masks": str(n_deleted_random_masks),
216
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
217
                "date_joined_relay": str(date_joined_relay),
218
                "premium_status": str(premium_status),
219
                "date_joined_premium": str(date_joined_premium),
220
                "has_extension": str(has_extension).lower(),
221
                "date_got_extension": str(date_got_extension),
222
                "mask_id": str(mask_id),
223
                "is_random_mask": str(is_random_mask).lower(),
224
                "is_reply": str(is_reply).lower(),
225
            },
226
        }
227
        self._record(user_agent, ip_address, event)
1✔
228

229
    def record_email_mask_created(
1✔
230
        self,
231
        user_agent: str,
232
        ip_address: str,
233
        client_id: str,
234
        fxa_id: str,
235
        platform: str,
236
        n_random_masks: int,
237
        n_domain_masks: int,
238
        n_deleted_random_masks: int,
239
        n_deleted_domain_masks: int,
240
        date_joined_relay: int,
241
        premium_status: str,
242
        date_joined_premium: int,
243
        has_extension: bool,
244
        date_got_extension: int,
245
        mask_id: str,
246
        is_random_mask: bool,
247
        created_by_api: bool,
248
        has_website: bool,
249
    ) -> None:
250
        """
251
        Record and submit a email_mask_created event:
252
        A Relay user creates an email mask.
253
        Event is logged to STDOUT via `print`.
254

255
        :param str user_agent: The user agent.
256
        :param str ip_address: The IP address. Will be used to decode Geo information
257
            and scrubbed at ingestion.
258
        :param str client_id: Firefox client ID
259
        :param str fxa_id: Mozilla accounts user ID
260
        :param str platform: Relay client platform
261
        :param int n_random_masks: Number of random masks
262
        :param int n_domain_masks: Number of premium subdomain masks
263
        :param int n_deleted_random_masks: Number of deleted random masks
264
        :param int n_deleted_domain_masks: Number of deleted domain masks
265
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
266
        :param str premium_status: Subscription type and term
267
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
268
        :param bool has_extension: The user has the Relay Add-on
269
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
270
        :param str mask_id: Mask ID, 'R' (random mask) or 'D' (domain mask) followed by a number.
271
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
272
        :param bool created_by_api: The mask was created via the API, rather than an incoming email
273
        :param bool has_website: The mask was created by the Add-on or integration on a website
274
        """
275
        event = {
1✔
276
            "category": "email_mask",
277
            "name": "created",
278
            "extra": {
279
                "client_id": str(client_id),
280
                "fxa_id": str(fxa_id),
281
                "platform": str(platform),
282
                "n_random_masks": str(n_random_masks),
283
                "n_domain_masks": str(n_domain_masks),
284
                "n_deleted_random_masks": str(n_deleted_random_masks),
285
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
286
                "date_joined_relay": str(date_joined_relay),
287
                "premium_status": str(premium_status),
288
                "date_joined_premium": str(date_joined_premium),
289
                "has_extension": str(has_extension).lower(),
290
                "date_got_extension": str(date_got_extension),
291
                "mask_id": str(mask_id),
292
                "is_random_mask": str(is_random_mask).lower(),
293
                "created_by_api": str(created_by_api).lower(),
294
                "has_website": str(has_website).lower(),
295
            },
296
        }
297
        self._record(user_agent, ip_address, event)
1✔
298

299
    def record_email_mask_deleted(
1✔
300
        self,
301
        user_agent: str,
302
        ip_address: str,
303
        client_id: str,
304
        fxa_id: str,
305
        platform: str,
306
        n_random_masks: int,
307
        n_domain_masks: int,
308
        n_deleted_random_masks: int,
309
        n_deleted_domain_masks: int,
310
        date_joined_relay: int,
311
        premium_status: str,
312
        date_joined_premium: int,
313
        has_extension: bool,
314
        date_got_extension: int,
315
        mask_id: str,
316
        is_random_mask: bool,
317
    ) -> None:
318
        """
319
        Record and submit a email_mask_deleted event:
320
        A Relay user deletes an email mask.
321
        Event is logged to STDOUT via `print`.
322

323
        :param str user_agent: The user agent.
324
        :param str ip_address: The IP address. Will be used to decode Geo information
325
            and scrubbed at ingestion.
326
        :param str client_id: Firefox client ID
327
        :param str fxa_id: Mozilla accounts user ID
328
        :param str platform: Relay client platform
329
        :param int n_random_masks: Number of random masks
330
        :param int n_domain_masks: Number of premium subdomain masks
331
        :param int n_deleted_random_masks: Number of deleted random masks
332
        :param int n_deleted_domain_masks: Number of deleted domain masks
333
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
334
        :param str premium_status: Subscription type and term
335
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
336
        :param bool has_extension: The user has the Relay Add-on
337
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
338
        :param str mask_id: Mask ID, 'R' (random mask) or 'D' (domain mask) followed by a number.
339
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
340
        """
341
        event = {
1✔
342
            "category": "email_mask",
343
            "name": "deleted",
344
            "extra": {
345
                "client_id": str(client_id),
346
                "fxa_id": str(fxa_id),
347
                "platform": str(platform),
348
                "n_random_masks": str(n_random_masks),
349
                "n_domain_masks": str(n_domain_masks),
350
                "n_deleted_random_masks": str(n_deleted_random_masks),
351
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
352
                "date_joined_relay": str(date_joined_relay),
353
                "premium_status": str(premium_status),
354
                "date_joined_premium": str(date_joined_premium),
355
                "has_extension": str(has_extension).lower(),
356
                "date_got_extension": str(date_got_extension),
357
                "mask_id": str(mask_id),
358
                "is_random_mask": str(is_random_mask).lower(),
359
            },
360
        }
361
        self._record(user_agent, ip_address, event)
1✔
362

363
    def record_email_mask_label_updated(
1✔
364
        self,
365
        user_agent: str,
366
        ip_address: str,
367
        client_id: str,
368
        fxa_id: str,
369
        platform: str,
370
        n_random_masks: int,
371
        n_domain_masks: int,
372
        n_deleted_random_masks: int,
373
        n_deleted_domain_masks: int,
374
        date_joined_relay: int,
375
        premium_status: str,
376
        date_joined_premium: int,
377
        has_extension: bool,
378
        date_got_extension: int,
379
        mask_id: str,
380
        is_random_mask: bool,
381
    ) -> None:
382
        """
383
        Record and submit a email_mask_label_updated event:
384
        A Relay user updates an email mask's label.
385
        Event is logged to STDOUT via `print`.
386

387
        :param str user_agent: The user agent.
388
        :param str ip_address: The IP address. Will be used to decode Geo information
389
            and scrubbed at ingestion.
390
        :param str client_id: Firefox client ID
391
        :param str fxa_id: Mozilla accounts user ID
392
        :param str platform: Relay client platform
393
        :param int n_random_masks: Number of random masks
394
        :param int n_domain_masks: Number of premium subdomain masks
395
        :param int n_deleted_random_masks: Number of deleted random masks
396
        :param int n_deleted_domain_masks: Number of deleted domain masks
397
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
398
        :param str premium_status: Subscription type and term
399
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
400
        :param bool has_extension: The user has the Relay Add-on
401
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
402
        :param str mask_id: Mask ID, 'R' (random mask) or 'D' (domain mask) followed by a number.
403
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
404
        """
405
        event = {
1✔
406
            "category": "email_mask",
407
            "name": "label_updated",
408
            "extra": {
409
                "client_id": str(client_id),
410
                "fxa_id": str(fxa_id),
411
                "platform": str(platform),
412
                "n_random_masks": str(n_random_masks),
413
                "n_domain_masks": str(n_domain_masks),
414
                "n_deleted_random_masks": str(n_deleted_random_masks),
415
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
416
                "date_joined_relay": str(date_joined_relay),
417
                "premium_status": str(premium_status),
418
                "date_joined_premium": str(date_joined_premium),
419
                "has_extension": str(has_extension).lower(),
420
                "date_got_extension": str(date_got_extension),
421
                "mask_id": str(mask_id),
422
                "is_random_mask": str(is_random_mask).lower(),
423
            },
424
        }
425
        self._record(user_agent, ip_address, event)
1✔
426

427

428
def create_events_server_event_logger(
1✔
429
    application_id: str,
430
    app_display_version: str,
431
    channel: str,
432
) -> EventsServerEventLogger:
433
    """
434
    Factory function that creates an instance of Glean Server Event Logger to record
435
    `events` ping events.
436
    :param str application_id: The application ID.
437
    :param str app_display_version: The application display version.
438
    :param str channel: The channel.
439
    :return: An instance of EventsServerEventLogger.
440
    :rtype: EventsServerEventLogger
441
    """
442
    return EventsServerEventLogger(application_id, app_display_version, channel)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc