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>
This commit is contained in:
Edward Lesmes
2021-04-30 18:38:25 +00:00
committed by LUCI CQ
parent cc7ab346b0
commit 1e59a247ab
3 changed files with 66 additions and 19 deletions

View File

@@ -1,21 +1,27 @@
#!/usr/bin/env python
#!/usr/bin/env vpython3
# Copyright (c) 2018 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
import sys
import urllib.error
import urllib.request
from third_party.six.moves import urllib
from third_party.six.moves import input # pylint: disable=redefined-builtin
import auth
import metrics_utils
def main():
metrics = input()
try:
urllib.request.urlopen(
metrics_utils.APP_URL + '/upload', metrics.encode('utf-8'))
headers = {}
if 'bot_metrics' in metrics:
token = auth.Authenticator().get_access_token().token
headers = {'Authorization': 'Bearer ' + token}
urllib.request.urlopen(urllib.request.Request(
url=metrics_utils.APP_URL + '/upload',
data=metrics.encode('utf-8'),
headers=headers))
except (urllib.error.HTTPError, urllib.error.URLError):
pass