blob: f3afd6c004ee93a16dcfc505b1a2c92bb1ad0db6 [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
9from __future__ import unicode_literals
10
Paul Hobbse46a42b2017-03-21 14:04:13 -070011import psutil
12
Allen Li24bf8182017-03-02 16:41:20 -080013import os
14import subprocess
15
Allen Li24bf8182017-03-02 16:41:20 -080016from chromite.lib import cros_logging as logging
17from infra_libs import ts_mon
18
19logger = logging.getLogger(__name__)
20
21
22class _GitRepo(object):
Paul Hobbse46a42b2017-03-21 14:04:13 -070023 """Helper class for running git commands."""
Allen Li24bf8182017-03-02 16:41:20 -080024
25 def __init__(self, gitdir):
26 self._gitdir = gitdir
27
28 def _get_git_command(self):
29 return ['git', '--git-dir', self._gitdir]
30
31 def _check_output(self, args, **kwargs):
32 return subprocess.check_output(
33 self._get_git_command() + list(args), **kwargs)
34
35 def get_commit_hash(self):
36 return self._check_output(['rev-parse', 'HEAD']).strip()
37
38 def get_commit_time(self):
39 return int(self._check_output(['show', '-s', '--format=%ct', 'HEAD'])
40 .strip())
41
42
43class _GitMetricCollector(object):
Paul Hobbse46a42b2017-03-21 14:04:13 -070044 """Class for collecting metrics about a git repository."""
Allen Li24bf8182017-03-02 16:41:20 -080045
46 _commit_hash_metric = ts_mon.StringMetric(
47 'git/hash',
48 description='Current Git commit hash.')
49
Paul Hobbse46a42b2017-03-21 14:04:13 -070050 _commit_time_metric = ts_mon.GaugeMetric(
Allen Li24bf8182017-03-02 16:41:20 -080051 'git/commit_time',
52 description='Current Git commit time as seconds since Unix Epoch.')
53
54 def __init__(self, gitdir, metric_path):
55 self._gitdir = gitdir
56 self._gitrepo = _GitRepo(gitdir)
57 self._fields = {'repo': gitdir}
58 self._metric_path = metric_path
59
60 def collect(self):
61 """Collect metrics."""
62 try:
63 self._collect_commit_hash_metric()
64 self._collect_commit_time_metric()
65 except subprocess.CalledProcessError as e:
66 logger.warning('Error collecting git metrics for %s: %s',
67 self._gitdir, e)
68
69 def _collect_commit_hash_metric(self):
70 commit_hash = self._gitrepo.get_commit_hash()
71 logger.debug('Collecting Git hash %r for %r', commit_hash, self._gitdir)
72 self._commit_hash_metric.set(commit_hash, self._fields)
73
74 def _collect_commit_time_metric(self):
75 commit_time = self._gitrepo.get_commit_time()
76 logger.debug('Collecting Git commit time %r for %r',
77 commit_time, self._gitdir)
78 self._commit_time_metric.set(commit_time, self._fields)
79
80
81_CHROMIUMOS_DIR = os.path.expanduser('~chromeos-test/chromiumos/')
82
83_repo_collectors = (
84 # TODO(ayatane): We cannot access chromeos-admin because we are
85 # running as non-root.
86 _GitMetricCollector(gitdir='/root/chromeos-admin/.git',
87 metric_path='chromeos-admin'),
88 _GitMetricCollector(gitdir=_CHROMIUMOS_DIR + 'chromite/.git',
89 metric_path='chromite'),
90 _GitMetricCollector(gitdir='/usr/local/autotest/.git',
91 metric_path='installed_autotest'),
92)
93
94
95def collect_git_metrics():
96 """Collect metrics for Git repository state."""
97 for collector in _repo_collectors:
98 collector.collect()