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