summaryrefslogtreecommitdiff
path: root/buildscripts/metrics/tooling_metrics.py
blob: 1a890083de4657179ac6501dcc2a9f11b678a4b0 (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
import dataclasses
import datetime
import logging
import os
import asyncio
from typing import Optional
from git import Repo
import pymongo

from buildscripts.metrics.metrics_datatypes import ToolingMetrics

logger = logging.getLogger('tooling_metrics_collection')

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,
    )


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()


async 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(utc_starttime: datetime) -> None:
    """Persist tooling metrics data to MongoDB Internal Atlas Cluster."""
    try:
        if not _is_virtual_workstation():
            return
        asyncio.run(
            asyncio.wait_for(
                _save_metrics(ToolingMetrics.get_tooling_metrics(utc_starttime)), timeout=1.0))
    except asyncio.TimeoutError as exc:
        logger.warning(
            "%s\nTimeout: 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)
    except Exception as exc:  # pylint: disable=broad-except
        logger.warning(
            "%s\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)