reclient_metrics.py 4.2 KB

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