Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame^] | 1 | # Copyright 2017 The ChromiumOS Authors |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Git repo metrics.""" |
| 6 | |
| 7 | from __future__ import absolute_import |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 8 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 9 | import logging |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 10 | import os |
| 11 | import subprocess |
| 12 | |
Allen Li | a9c6e80 | 2017-07-11 15:42:47 -0700 | [diff] [blame] | 13 | from chromite.lib import metrics |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 14 | |
Chris McDonald | 59650c3 | 2021-07-20 15:29:28 -0600 | [diff] [blame] | 15 | |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 16 | logger = logging.getLogger(__name__) |
| 17 | |
| 18 | |
| 19 | class _GitRepo(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 20 | """Helper class for running git commands.""" |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 21 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 22 | def __init__(self, gitdir): |
| 23 | self._gitdir = gitdir |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 24 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 25 | def _get_git_command(self): |
| 26 | return ["git", "--git-dir", self._gitdir] |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | def _check_output(self, args, **kwargs): |
| 29 | return subprocess.check_output( |
| 30 | self._get_git_command() + list(args), **kwargs |
| 31 | ).decode("utf-8") |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 33 | def get_commit_hash(self): |
| 34 | """Return commit hash string.""" |
| 35 | return self._check_output(["rev-parse", "HEAD"]).strip() |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 36 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 37 | def get_commit_time(self): |
| 38 | """Return commit time as UNIX timestamp int.""" |
| 39 | return int( |
| 40 | self._check_output(["show", "-s", "--format=%ct", "HEAD"]).strip() |
| 41 | ) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 42 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 43 | def get_unstaged_changes(self): |
| 44 | """Return number of unstaged changes as (added, deleted).""" |
| 45 | added_total, deleted_total = 0, 0 |
| 46 | # output looks like: |
| 47 | # '1\t2\tfoo\n3\t4\tbar\n' |
| 48 | # '-\t-\tbinary_file\n' |
| 49 | output = self._check_output(["diff-index", "--numstat", "HEAD"]) |
| 50 | stats_strings = (line.split() for line in output.splitlines()) |
| 51 | for added, deleted, _path in stats_strings: |
| 52 | if added != "-": |
| 53 | added_total += int(added) |
| 54 | if deleted != "-": |
| 55 | deleted_total += int(deleted) |
| 56 | return added_total, deleted_total |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame] | 57 | |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 58 | |
| 59 | class _GitMetricCollector(object): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 60 | """Class for collecting metrics about a git repository. |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | The constructor takes the arguments: `gitdir`, `metric_path`. |
| 63 | `gitdir` is the path to the Git directory to collect metrics for and |
| 64 | may start with a tilde (expanded to a user's home directory). |
| 65 | `metric_path` is the Monarch metric path to report to. |
| 66 | """ |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 67 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 68 | _commit_hash_metric = metrics.StringMetric( |
| 69 | "git/hash", description="Current Git commit hash." |
| 70 | ) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 71 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 72 | _timestamp_metric = metrics.GaugeMetric( |
| 73 | "git/timestamp", |
| 74 | description="Current Git commit time as seconds since Unix Epoch.", |
| 75 | ) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | _unstaged_changes_metric = metrics.GaugeMetric( |
| 78 | "git/unstaged_changes", description="Unstaged Git changes." |
| 79 | ) |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame] | 80 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 81 | def __init__(self, gitdir, metric_path): |
| 82 | self._gitdir = gitdir |
| 83 | self._gitrepo = _GitRepo(os.path.expanduser(gitdir)) |
| 84 | self._fields = {"repo": gitdir} |
| 85 | self._metric_path = metric_path |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 86 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 87 | def collect(self): |
| 88 | """Collect metrics.""" |
| 89 | try: |
| 90 | self._collect_commit_hash_metric() |
| 91 | self._collect_timestamp_metric() |
| 92 | self._collect_unstaged_changes_metric() |
| 93 | except subprocess.CalledProcessError as e: |
| 94 | logger.warning( |
| 95 | "Error collecting git metrics for %s: %s", self._gitdir, e |
| 96 | ) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 97 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 98 | def _collect_commit_hash_metric(self): |
| 99 | commit_hash = self._gitrepo.get_commit_hash() |
| 100 | logger.debug("Collecting Git hash %r for %r", commit_hash, self._gitdir) |
| 101 | self._commit_hash_metric.set(commit_hash, self._fields) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 102 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 103 | def _collect_timestamp_metric(self): |
| 104 | commit_time = self._gitrepo.get_commit_time() |
| 105 | logger.debug( |
| 106 | "Collecting Git timestamp %r for %r", commit_time, self._gitdir |
| 107 | ) |
| 108 | self._timestamp_metric.set(commit_time, self._fields) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 109 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 110 | def _collect_unstaged_changes_metric(self): |
| 111 | added, deleted = self._gitrepo.get_unstaged_changes() |
| 112 | self._unstaged_changes_metric.set( |
| 113 | added, fields=dict(change_type="added", **self._fields) |
| 114 | ) |
| 115 | self._unstaged_changes_metric.set( |
| 116 | deleted, fields=dict(change_type="deleted", **self._fields) |
| 117 | ) |
Allen Li | 8934e04 | 2017-06-21 15:54:42 -0700 | [diff] [blame] | 118 | |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 119 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 120 | _CHROMIUMOS_DIR = "~chromeos-test/chromiumos/" |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 121 | |
| 122 | _repo_collectors = ( |
Allen Li | a02d34a | 2017-04-04 17:13:46 -0700 | [diff] [blame] | 123 | # TODO(ayatane): We cannot access chromeos-admin because we are |
| 124 | # running as non-root. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | _GitMetricCollector( |
| 126 | gitdir="/root/chromeos-admin/.git", metric_path="chromeos-admin" |
| 127 | ), |
| 128 | _GitMetricCollector( |
| 129 | gitdir=_CHROMIUMOS_DIR + "chromite/.git", metric_path="chromite" |
| 130 | ), |
| 131 | _GitMetricCollector( |
| 132 | gitdir="/usr/local/autotest/.git", metric_path="installed_autotest" |
| 133 | ), |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 134 | ) |
| 135 | |
| 136 | |
| 137 | def collect_git_metrics(): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 138 | """Collect metrics for Git repository state.""" |
| 139 | for collector in _repo_collectors: |
| 140 | collector.collect() |