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

mozilla / fx-private-relay / 7ff6a30e-219b-43d9-a14c-e4c86a54e858

28 Jan 2025 04:07PM CUT coverage: 85.092% (+0.008%) from 85.084%
7ff6a30e-219b-43d9-a14c-e4c86a54e858

push

circleci

web-flow
Merge pull request #5345 from mozilla/metrics-mpp-4022

MPP-4022: Change metrics namespace to `firefox_relay`

2434 of 3561 branches covered (68.35%)

Branch coverage included in aggregate %.

43 of 61 new or added lines in 7 files covered. (70.49%)

1 existing line in 1 file now uncovered.

17001 of 19279 relevant lines covered (88.18%)

9.92 hits per line

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

42.11
/privaterelay/apps.py
1
import base64
1✔
2
import json
1✔
3
import os
1✔
4
from pathlib import Path
1✔
5
from typing import TYPE_CHECKING, Any
1✔
6

7
from django.apps import AppConfig
1✔
8
from django.conf import settings
1✔
9
from django.utils.functional import cached_property
1✔
10

11
import markus
1✔
12
import requests
1✔
13

14
ROOT_DIR = os.path.abspath(os.curdir)
1✔
15

16

17
if TYPE_CHECKING:
18
    from allauth.socialaccount.models import SocialApp
19

20

21
def configure_google_profiler() -> None:
1✔
22
    if (
1!
23
        settings.GOOGLE_APPLICATION_CREDENTIALS == ""
24
        or settings.GOOGLE_CLOUD_PROFILER_CREDENTIALS_B64 == ""
25
    ):
26
        return
1✔
27

28
    # Set up Google Cloud Profiler
NEW
29
    service, version = get_profiler_startup_data()
×
NEW
30
    if service is None:
×
NEW
31
        return
×
32

NEW
33
    gcp_key_json_path = Path(settings.GOOGLE_APPLICATION_CREDENTIALS)
×
NEW
34
    if not gcp_key_json_path.exists():
×
NEW
35
        write_gcp_key_json_file(gcp_key_json_path)
×
NEW
36
    try:
×
NEW
37
        with gcp_key_json_path.open() as gcp_key_file:
×
NEW
38
            try:
×
39
                # Make sure the expected gcp_key.json file is valid json
NEW
40
                gcp_key_data = json.load(gcp_key_file)
×
NEW
41
                import googlecloudprofiler
×
42

NEW
43
                googlecloudprofiler.start(
×
44
                    service=service,
45
                    service_version=version,
46
                    project_id=gcp_key_data["project_id"],
47
                )
NEW
48
            except json.JSONDecodeError:
×
NEW
49
                print(f"error during json.load({gcp_key_json_path})")
×
NEW
50
    except Exception as exc:
×
NEW
51
        print(
×
52
            f"exception {repr(exc)}"
53
            " while starting google cloud profiler"
54
            f" with key file: {gcp_key_json_path}"
55
        )
56

57

58
def get_profiler_startup_data() -> tuple[str | None, str | None]:
1✔
59
    from .utils import get_version_info
×
60

61
    if settings.RELAY_CHANNEL not in ("dev", "stage", "prod"):
×
62
        return (None, None)
×
63

64
    if settings.RELAY_CHANNEL in ("dev", "stage"):
×
65
        service = f"fxprivaterelay-{settings.RELAY_CHANNEL}"
×
66
    if settings.RELAY_CHANNEL == "prod":
×
67
        service = "fxprivaterelay"
×
68

69
    version_info = get_version_info()
×
70
    version = version_info.get("version", "unknown")
×
71

72
    return service, version
×
73

74

75
def write_gcp_key_json_file(gcp_key_json_path: Path) -> None:
1✔
76
    """
77
    Create the gcp key json file from contents of GOOGLE_CLOUD_PROFILER_CREDENTIALS_B64
78
    """
79
    google_app_creds = base64.b64decode(settings.GOOGLE_CLOUD_PROFILER_CREDENTIALS_B64)
×
80
    if not google_app_creds == b"":
×
81
        with open(gcp_key_json_path, "w+") as gcp_key_file:
×
82
            gcp_key_file.write(google_app_creds.decode("utf-8"))
×
83

84

85
def configure_markus() -> None:
1✔
86
    backends: list[dict[str, Any]] = []
1✔
87
    if settings.DJANGO_STATSD_ENABLED and not settings.IN_PYTEST:
1!
NEW
88
        backends.append(
×
89
            {
90
                "class": "markus.backends.datadog.DatadogMetrics",
91
                "options": {
92
                    "statsd_host": settings.STATSD_HOST,
93
                    "statsd_port": settings.STATSD_PORT,
94
                    "statsd_namespace": settings.STATSD_PREFIX,
95
                },
96
            }
97
        )
98
    if settings.STATSD_DEBUG:
1!
NEW
99
        backends.append(
×
100
            {
101
                "class": "markus.backends.logging.LoggingMetrics",
102
                "options": {
103
                    "logger_name": "markus",
104
                    "leader": "METRICS",
105
                },
106
            }
107
        )
108
    markus.configure(backends=backends)
1✔
109

110

111
class PrivateRelayConfig(AppConfig):
1✔
112
    name = "privaterelay"
1✔
113

114
    def ready(self) -> None:
1✔
115
        configure_markus()
1✔
116
        configure_google_profiler()
1✔
117

118
        import privaterelay.signals  # noqa: F401 (imported but unused warning)
1✔
119

120
        try:
1✔
121
            del self.fxa_verifying_keys  # Clear cache
1✔
122
            del self.fxa_social_app  # Clear cache
×
123
        except AttributeError:
1✔
124
            pass
1✔
125

126
    @cached_property
1✔
127
    def fxa_verifying_keys(self) -> list[dict[str, Any]]:
1✔
128
        resp = requests.get(
×
129
            "{}/jwks".format(settings.SOCIALACCOUNT_PROVIDERS["fxa"]["OAUTH_ENDPOINT"]),
130
            timeout=10,
131
        )
132
        if resp.status_code == 200:
×
133
            keys: list[dict[str, Any]] = resp.json()["keys"]
×
134
            return keys
×
135
        return []
×
136

137
    @cached_property
1✔
138
    def fxa_social_app(self) -> "SocialApp":
1✔
139
        from allauth.socialaccount.models import SocialApp
1✔
140

141
        return SocialApp.objects.get(provider="fxa")
1✔
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