metrics: Report bot metrics if DEPOT_TOOLS_REPORT_BUILD is set
If the DEPOT_TOOLS_REPORT_BUILD envvar is set, Depot Tools will
report information about the builder running the command
(e.g. buildbucket project, bucket, builder and build id).
It will also authenticate to the metrics server, and ignore any
requests not made by ChOps service accounts.
Change-Id: I078a4c2170b4226086c42f289fa449bdebc87179
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/2861213
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
diff --git a/metrics_utils.py b/metrics_utils.py
index 281b786..5792118 100644
--- a/metrics_utils.py
+++ b/metrics_utils.py
@@ -6,6 +6,7 @@
from __future__ import print_function
import re
+import os
import scm
import subprocess2
import sys
@@ -19,10 +20,13 @@
# Current version of metrics recording.
# When we add new metrics, the version number will be increased, we display the
# user what has changed, and ask the user to agree again.
-CURRENT_VERSION = 1
+CURRENT_VERSION = 2
APP_URL = 'https://cit-cli-metrics.appspot.com'
+DEPOT_TOOLS_REPORT_BUILD = 'DEPOT_TOOLS_REPORT_BUILD'
+
+
def get_notice_countdown_header(countdown):
if countdown == 0:
yield ' METRICS COLLECTION IS TAKING PLACE'
@@ -43,15 +47,28 @@
def get_change_notice(version):
if version == 0:
- pass # No changes for version 0
+ return [] # No changes for version 0
elif version == 1:
- yield 'We want to collect the Git version.'
- yield 'We want to collect information about the HTTP'
- yield 'requests that depot_tools makes, and the git and'
- yield 'cipd commands it executes.'
- yield ''
- yield 'We only collect known strings to make sure we'
- yield 'don\'t record PII.'
+ return [
+ 'We want to collect the Git version.',
+ 'We want to collect information about the HTTP',
+ 'requests that depot_tools makes, and the git and',
+ 'cipd commands it executes.',
+ '',
+ 'We only collect known strings to make sure we',
+ 'don\'t record PII.',
+ ]
+ elif version == 2:
+ return [
+ 'We will start collecting metrics from bots.',
+ 'There are no changes for developers.',
+ 'If the DEPOT_TOOLS_REPORT_BUILD environment variable is set,',
+ 'we will report information about the current build',
+ '(e.g. buildbucket project, bucket, builder and build id),',
+ 'and authenticate to the metrics collection server.',
+ 'This information will only be recorded for requests',
+ 'authenticated as bot service accounts.',
+ ]
KNOWN_PROJECT_URLS = {
@@ -174,6 +191,22 @@
return '%s.%s.%s' % match.groups()
+def get_bot_metrics():
+ build = os.getenv(DEPOT_TOOLS_REPORT_BUILD)
+ if not build or build.count('/') != 3:
+ return None
+ project, bucket, builder, build = build.split('/')
+ return {
+ 'build_id': int(build),
+ 'builder': {
+ 'project': project,
+ 'bucket': bucket,
+ 'builder': builder,
+ },
+ }
+
+
+
def return_code_from_exception(exception):
"""Returns the exit code that would result of raising the exception."""
if exception is None:
@@ -289,5 +322,5 @@
lines = list(get_notice_version_change_header())
for version in range(config_version + 1, CURRENT_VERSION + 1):
lines.append('')
- lines += list(get_change_notice(version))
+ lines += get_change_notice(version)
print_boxed_text(sys.stderr.write, 49, lines)