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

mozilla / fx-private-relay / b2e067fe-ce4e-4099-9bef-07b368e99782

15 Apr 2024 04:18PM CUT coverage: 75.544% (+0.002%) from 75.542%
b2e067fe-ce4e-4099-9bef-07b368e99782

push

circleci

jwhitlock
Enable pyupgrade, fix issues

2443 of 3405 branches covered (71.75%)

Branch coverage included in aggregate %.

56 of 59 new or added lines in 14 files covered. (94.92%)

234 existing lines in 24 files now uncovered.

6793 of 8821 relevant lines covered (77.01%)

20.04 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

13
import json
1✔
14
from datetime import UTC, datetime
1✔
15
from typing import Any
1✔
16
from uuid import uuid4
1✔
17

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

20

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

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

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

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

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

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

UNCOV
91
        print(ping_envelope_serialized)
×
92

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

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

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

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

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

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

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

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

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

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

428

429
def create_events_server_event_logger(
1✔
430
    application_id: str,
431
    app_display_version: str,
432
    channel: str,
433
) -> EventsServerEventLogger:
434
    """
435
    Factory function that creates an instance of Glean Server Event Logger to record
436
    `events` ping events.
437
    :param str application_id: The application ID.
438
    :param str app_display_version: The application display version.
439
    :param str channel: The channel.
440
    :return: An instance of EventsServerEventLogger.
441
    :rtype: EventsServerEventLogger
442
    """
UNCOV
443
    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