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 |
| 8 | from __future__ import print_function |
| 9 | from __future__ import unicode_literals |
| 10 | |
| 11 | import os |
| 12 | import subprocess |
| 13 | |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 14 | from chromite.lib import cros_logging as logging |
| 15 | from infra_libs import ts_mon |
| 16 | |
| 17 | logger = logging.getLogger(__name__) |
| 18 | |
| 19 | |
| 20 | class _GitRepo(object): |
Paul Hobbs | e46a42b | 2017-03-21 14:04:13 -0700 | [diff] [blame] | 21 | """Helper class for running git commands.""" |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 22 | |
| 23 | def __init__(self, gitdir): |
| 24 | self._gitdir = gitdir |
| 25 | |
| 26 | def _get_git_command(self): |
| 27 | return ['git', '--git-dir', self._gitdir] |
| 28 | |
| 29 | def _check_output(self, args, **kwargs): |
| 30 | return subprocess.check_output( |
| 31 | self._get_git_command() + list(args), **kwargs) |
| 32 | |
| 33 | def get_commit_hash(self): |
Allen Li | d233398 | 2017-04-04 17:03:32 -0700 | [diff] [blame] | 34 | """Return commit hash string.""" |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 35 | return self._check_output(['rev-parse', 'HEAD']).strip() |
| 36 | |
| 37 | def get_commit_time(self): |
Allen Li | d233398 | 2017-04-04 17:03:32 -0700 | [diff] [blame] | 38 | """Return commit time as UNIX timestamp int.""" |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 39 | return int(self._check_output(['show', '-s', '--format=%ct', 'HEAD']) |
| 40 | .strip()) |
| 41 | |
| 42 | |
| 43 | class _GitMetricCollector(object): |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame^] | 44 | """Class for collecting metrics about a git repository. |
| 45 | |
| 46 | The constructor takes the arguments: `gitdir`, `metric_path`. |
| 47 | `gitdir` is the path to the Git directory to collect metrics for and |
| 48 | may start with a tilde (expanded to a user's home directory). |
| 49 | `metric_path` is the Monarch metric path to report to. |
| 50 | """ |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 51 | |
| 52 | _commit_hash_metric = ts_mon.StringMetric( |
| 53 | 'git/hash', |
| 54 | description='Current Git commit hash.') |
| 55 | |
Allen Li | d523d96 | 2017-04-04 16:48:36 -0700 | [diff] [blame] | 56 | _timestamp_metric = ts_mon.GaugeMetric( |
| 57 | 'git/timestamp', |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 58 | description='Current Git commit time as seconds since Unix Epoch.') |
| 59 | |
| 60 | def __init__(self, gitdir, metric_path): |
| 61 | self._gitdir = gitdir |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame^] | 62 | self._gitrepo = _GitRepo(os.path.expanduser(gitdir)) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 63 | self._fields = {'repo': gitdir} |
| 64 | self._metric_path = metric_path |
| 65 | |
| 66 | def collect(self): |
| 67 | """Collect metrics.""" |
| 68 | try: |
| 69 | self._collect_commit_hash_metric() |
Allen Li | d523d96 | 2017-04-04 16:48:36 -0700 | [diff] [blame] | 70 | self._collect_timestamp_metric() |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 71 | except subprocess.CalledProcessError as e: |
| 72 | logger.warning('Error collecting git metrics for %s: %s', |
| 73 | self._gitdir, e) |
| 74 | |
| 75 | def _collect_commit_hash_metric(self): |
| 76 | commit_hash = self._gitrepo.get_commit_hash() |
| 77 | logger.debug('Collecting Git hash %r for %r', commit_hash, self._gitdir) |
| 78 | self._commit_hash_metric.set(commit_hash, self._fields) |
| 79 | |
Allen Li | d523d96 | 2017-04-04 16:48:36 -0700 | [diff] [blame] | 80 | def _collect_timestamp_metric(self): |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 81 | commit_time = self._gitrepo.get_commit_time() |
Allen Li | d523d96 | 2017-04-04 16:48:36 -0700 | [diff] [blame] | 82 | logger.debug('Collecting Git timestamp %r for %r', |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 83 | commit_time, self._gitdir) |
Allen Li | d523d96 | 2017-04-04 16:48:36 -0700 | [diff] [blame] | 84 | self._timestamp_metric.set(commit_time, self._fields) |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 85 | |
| 86 | |
Allen Li | bdb9f04 | 2017-04-10 13:25:47 -0700 | [diff] [blame^] | 87 | _CHROMIUMOS_DIR = '~chromeos-test/chromiumos/' |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 88 | |
| 89 | _repo_collectors = ( |
Allen Li | a02d34a | 2017-04-04 17:13:46 -0700 | [diff] [blame] | 90 | # TODO(ayatane): We cannot access chromeos-admin because we are |
| 91 | # running as non-root. |
| 92 | _GitMetricCollector(gitdir='/root/chromeos-admin/.git', |
| 93 | metric_path='chromeos-admin'), |
| 94 | _GitMetricCollector(gitdir=_CHROMIUMOS_DIR + 'chromite/.git', |
| 95 | metric_path='chromite'), |
| 96 | _GitMetricCollector(gitdir='/usr/local/autotest/.git', |
| 97 | metric_path='installed_autotest'), |
Allen Li | 24bf818 | 2017-03-02 16:41:20 -0800 | [diff] [blame] | 98 | ) |
| 99 | |
| 100 | |
| 101 | def collect_git_metrics(): |
| 102 | """Collect metrics for Git repository state.""" |
| 103 | for collector in _repo_collectors: |
| 104 | collector.collect() |