reclient_metrics.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #!/usr/bin/env python3
  2. # Copyright 2023 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """This script manages the counter for how many times developers should
  6. be notified before uploading reclient metrics."""
  7. import json
  8. import os
  9. import subprocess
  10. import sys
  11. import utils
  12. THIS_DIR = os.path.dirname(__file__)
  13. CONFIG = utils.depot_tools_config_path('reclient_metrics.cfg')
  14. VERSION = 1
  15. def default_config():
  16. return {
  17. 'is-googler': is_googler(),
  18. 'countdown': 10,
  19. 'version': VERSION,
  20. }
  21. def load_config():
  22. config = None
  23. try:
  24. with open(CONFIG) as f:
  25. raw_config = json.load(f)
  26. if raw_config['version'] == VERSION:
  27. raw_config['countdown'] = max(0, raw_config['countdown'] - 1)
  28. config = raw_config
  29. except Exception:
  30. pass
  31. if not config:
  32. config = default_config()
  33. save_config(config)
  34. return config
  35. def save_config(config):
  36. with open(CONFIG, 'w') as f:
  37. json.dump(config, f)
  38. def show_message(config, ninja_out):
  39. print("""
  40. Your reclient metrics will be uploaded to the chromium build metrics database. The uploaded metrics will be used to analyze user side build performance.
  41. We upload the contents of {ninja_out_abs}.
  42. This contains
  43. * Flags passed to reproxy
  44. * Auth related flags are filtered out by reproxy
  45. * Start and end time of build tasks
  46. * Aggregated durations and counts of events during remote build actions
  47. * OS (e.g. Win, Mac or Linux)
  48. * Number of cpu cores and the amount of RAM of the building machine
  49. Uploading reclient metrics will be started after you run autoninja another {config_count} time(s).
  50. If you don't want to upload reclient metrics, please run following command.
  51. $ python3 {file_path} opt-out
  52. If you want to allow upload reclient metrics from next autoninja run, please run the
  53. following command.
  54. $ python3 {file_path} opt-in
  55. If you have questions about this, please send an email to foundry-x@google.com
  56. You can find a more detailed explanation in
  57. {metrics_readme_path}
  58. or
  59. https://chromium.googlesource.com/chromium/tools/depot_tools/+/main/reclient_metrics.README.md
  60. """.format(
  61. ninja_out_abs=os.path.abspath(
  62. os.path.join(ninja_out, ".reproxy_tmp", "logs", "rbe_metrics.txt")),
  63. config_count=config.get("countdown", 0),
  64. file_path=__file__,
  65. metrics_readme_path=os.path.abspath(
  66. os.path.join(THIS_DIR, "reclient_metrics.README.md")),
  67. ))
  68. def is_googler(config=None):
  69. """Check whether this user is Googler or not."""
  70. if config is not None and 'is-googler' in config:
  71. return config['is-googler']
  72. # Use cipd auth-info to check for googler status as
  73. # downloading rewrapper configs already requires cipd to be logged in
  74. p = subprocess.run('cipd auth-info',
  75. stdout=subprocess.PIPE,
  76. stderr=subprocess.PIPE,
  77. text=True,
  78. shell=True)
  79. if p.returncode != 0:
  80. return False
  81. lines = p.stdout.splitlines()
  82. if len(lines) == 0:
  83. return False
  84. l = lines[0]
  85. # |l| will be like 'Logged in as <user>@google.com.' for googlers.
  86. return l.startswith('Logged in as ') and l.endswith('@google.com.')
  87. def check_status(ninja_out):
  88. """Checks metrics collections status and shows notice to user if needed.
  89. Returns True if metrics should be collected."""
  90. config = load_config()
  91. if not is_googler(config):
  92. return False
  93. if 'opt-in' in config:
  94. return config['opt-in']
  95. if config.get("countdown", 0) > 0:
  96. show_message(config, ninja_out)
  97. return False
  98. return True
  99. def main(argv):
  100. cfg = load_config()
  101. if not is_googler(cfg):
  102. save_config(cfg)
  103. return 0
  104. if len(argv) == 2 and argv[1] == 'opt-in':
  105. cfg['opt-in'] = True
  106. cfg['countdown'] = 0
  107. save_config(cfg)
  108. print('reclient metrics upload is opted in.')
  109. return 0
  110. if len(argv) == 2 and argv[1] == 'opt-out':
  111. cfg['opt-in'] = False
  112. save_config(cfg)
  113. print('reclient metrics upload is opted out.')
  114. return 0
  115. if __name__ == '__main__':
  116. sys.exit(main(sys.argv))