summaryrefslogtreecommitdiff
path: root/buildscripts/metrics/tooling_metrics_utils.py
blob: 29e4e475bc931b00345cd97f227926f6bc19ad8f (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
import logging
import os
from typing import Optional
from git import Repo
import pymongo

from buildscripts.metrics.metrics_datatypes import ToolingMetrics

logger = logging.getLogger('tooling_metrics_utils')

INTERNAL_TOOLING_METRICS_HOSTNAME = "mongodb+srv://dev-metrics-pl-0.kewhj.mongodb.net"
INTERNAL_TOOLING_METRICS_USERNAME = "internal_tooling_user"
INTERNAL_TOOLING_METRICS_PASSWORD = "internal_tooling_user"


def _get_internal_tooling_metrics_client():
    """Retrieve client for internal MongoDB tooling metrics cluster."""
    return pymongo.MongoClient(
        host=INTERNAL_TOOLING_METRICS_HOSTNAME,
        username=INTERNAL_TOOLING_METRICS_USERNAME,
        password=INTERNAL_TOOLING_METRICS_PASSWORD,
        socketTimeoutMS=1000,
        serverSelectionTimeoutMS=1000,
        connectTimeoutMS=1000,
        waitQueueTimeoutMS=1000,
        retryWrites=False,
    )


EXPECTED_TOOLCHAIN_LOCATION = "/opt/mongodbtoolchain"


def _toolchain_exists() -> bool:
    """Check if the internal MongoDB toolchain exists."""
    return os.path.exists(EXPECTED_TOOLCHAIN_LOCATION)


def _git_user_exists() -> Optional[str]:
    """Check if a git user email exists."""
    try:
        return Repo('.').config_reader().get_value("user", "email", None)
    except Exception:  # pylint: disable=broad-except
        return None


def _is_virtual_workstation() -> bool:
    """Detect whether this is a MongoDB internal virtual workstation."""
    return _toolchain_exists() and _git_user_exists()


TOOLING_METRICS_OPT_OUT = "TOOLING_METRICS_OPT_OUT"


def _has_metrics_opt_out() -> bool:
    """Check whether the opt out environment variable is set."""
    return os.environ.get(TOOLING_METRICS_OPT_OUT, None) == '1'


def should_collect_metrics() -> bool:
    """Determine whether to collect tooling metrics."""
    return _is_virtual_workstation() and not _has_metrics_opt_out()


def _save_metrics(metrics: ToolingMetrics) -> None:
    """Save tooling metrics data."""
    client = _get_internal_tooling_metrics_client()
    client.metrics.tooling_metrics.insert_one(metrics.dict())


def save_tooling_metrics(tooling_metrics: ToolingMetrics) -> None:
    """Persist tooling metrics data to MongoDB Internal Atlas Cluster."""
    try:
        _save_metrics(tooling_metrics)
    except Exception as exc:  # pylint: disable=broad-except
        logger.warning(
            "\n%s\n\nUnexpected: Tooling metrics collection is not available -- this is a non-issue.\nIf this message persists, feel free to reach out to #server-development-platform",
            exc)