blob: 219f486c93a6f93b5728fbafd51747dcd74487a4 [file] [log] [blame]
Allen Li24bf8182017-03-02 16:41:20 -08001# 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
7from __future__ import absolute_import
8from __future__ import print_function
Allen Li24bf8182017-03-02 16:41:20 -08009
10import os
11import subprocess
12
Allen Li24bf8182017-03-02 16:41:20 -080013from chromite.lib import cros_logging as logging
14from infra_libs import ts_mon
15
16logger = logging.getLogger(__name__)
17
18
19class _GitRepo(object):
Paul Hobbse46a42b2017-03-21 14:04:13 -070020 """Helper class for running git commands."""
Allen Li24bf8182017-03-02 16:41:20 -080021
22 def __init__(self, gitdir):
23 self._gitdir = gitdir
24
25 def _get_git_command(self):
26 return ['git', '--git-dir', self._gitdir]
27
28 def _check_output(self, args, **kwargs):
29 return subprocess.check_output(
30 self._get_git_command() + list(args), **kwargs)
31
32 def get_commit_hash(self):
Allen Lid2333982017-04-04 17:03:32 -070033 """Return commit hash string."""
Allen Li24bf8182017-03-02 16:41:20 -080034 return self._check_output(['rev-parse', 'HEAD']).strip()
35
36 def get_commit_time(self):
Allen Lid2333982017-04-04 17:03:32 -070037 """Return commit time as UNIX timestamp int."""
Allen Li24bf8182017-03-02 16:41:20 -080038 return int(self._check_output(['show', '-s', '--format=%ct', 'HEAD'])
39 .strip())
40
41
42class _GitMetricCollector(object):
Allen Libdb9f042017-04-10 13:25:47 -070043 """Class for collecting metrics about a git repository.
44
45 The constructor takes the arguments: `gitdir`, `metric_path`.
46 `gitdir` is the path to the Git directory to collect metrics for and
47 may start with a tilde (expanded to a user's home directory).
48 `metric_path` is the Monarch metric path to report to.
49 """
Allen Li24bf8182017-03-02 16:41:20 -080050
51 _commit_hash_metric = ts_mon.StringMetric(
52 'git/hash',
53 description='Current Git commit hash.')
54
Allen Lid523d962017-04-04 16:48:36 -070055 _timestamp_metric = ts_mon.GaugeMetric(
56 'git/timestamp',
Allen Li24bf8182017-03-02 16:41:20 -080057 description='Current Git commit time as seconds since Unix Epoch.')
58
59 def __init__(self, gitdir, metric_path):
60 self._gitdir = gitdir
Allen Libdb9f042017-04-10 13:25:47 -070061 self._gitrepo = _GitRepo(os.path.expanduser(gitdir))
Allen Li24bf8182017-03-02 16:41:20 -080062 self._fields = {'repo': gitdir}
63 self._metric_path = metric_path
64
65 def collect(self):
66 """Collect metrics."""
67 try:
68 self._collect_commit_hash_metric()
Allen Lid523d962017-04-04 16:48:36 -070069 self._collect_timestamp_metric()
Allen Li24bf8182017-03-02 16:41:20 -080070 except subprocess.CalledProcessError as e:
Allen Li867d4582017-05-24 18:00:43 -070071 logger.warning(u'Error collecting git metrics for %s: %s',
Allen Li24bf8182017-03-02 16:41:20 -080072 self._gitdir, e)
73
74 def _collect_commit_hash_metric(self):
75 commit_hash = self._gitrepo.get_commit_hash()
Allen Li867d4582017-05-24 18:00:43 -070076 logger.debug(u'Collecting Git hash %r for %r', commit_hash, self._gitdir)
Allen Li24bf8182017-03-02 16:41:20 -080077 self._commit_hash_metric.set(commit_hash, self._fields)
78
Allen Lid523d962017-04-04 16:48:36 -070079 def _collect_timestamp_metric(self):
Allen Li24bf8182017-03-02 16:41:20 -080080 commit_time = self._gitrepo.get_commit_time()
Allen Li867d4582017-05-24 18:00:43 -070081 logger.debug(u'Collecting Git timestamp %r for %r',
Allen Li24bf8182017-03-02 16:41:20 -080082 commit_time, self._gitdir)
Allen Lid523d962017-04-04 16:48:36 -070083 self._timestamp_metric.set(commit_time, self._fields)
Allen Li24bf8182017-03-02 16:41:20 -080084
85
Allen Libdb9f042017-04-10 13:25:47 -070086_CHROMIUMOS_DIR = '~chromeos-test/chromiumos/'
Allen Li24bf8182017-03-02 16:41:20 -080087
88_repo_collectors = (
Allen Lia02d34a2017-04-04 17:13:46 -070089 # TODO(ayatane): We cannot access chromeos-admin because we are
90 # running as non-root.
91 _GitMetricCollector(gitdir='/root/chromeos-admin/.git',
92 metric_path='chromeos-admin'),
93 _GitMetricCollector(gitdir=_CHROMIUMOS_DIR + 'chromite/.git',
94 metric_path='chromite'),
95 _GitMetricCollector(gitdir='/usr/local/autotest/.git',
96 metric_path='installed_autotest'),
Allen Li24bf8182017-03-02 16:41:20 -080097)
98
99
100def collect_git_metrics():
101 """Collect metrics for Git repository state."""
102 for collector in _repo_collectors:
103 collector.collect()