Will Bradley | 88b1ffc | 2019-08-19 15:09:39 -0600 | [diff] [blame] | 1 | # Copyright 2019 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 | """The build API Metrics Emit entry point.""" |
| 6 | |
Will Bradley | 88b1ffc | 2019-08-19 15:09:39 -0600 | [diff] [blame] | 7 | from chromite.lib import commandline |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 8 | from chromite.lib import metrics_lib |
Will Bradley | 88b1ffc | 2019-08-19 15:09:39 -0600 | [diff] [blame] | 9 | |
| 10 | |
| 11 | def main(argv): |
| 12 | """Emit a metric event.""" |
| 13 | parser = commandline.ArgumentParser(description=__doc__) |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 14 | parser.add_argument( |
| 15 | 'op', |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 16 | choices=sorted(metrics_lib.VALID_OPS), |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 17 | help='Which metric event operator to emit.') |
| 18 | parser.add_argument( |
| 19 | 'name', |
| 20 | help='The name of the metric event as you would like it ' |
| 21 | 'to appear downstream in data stores.') |
| 22 | parser.add_argument( |
| 23 | 'arg', nargs='?', help='An accessory argument dependent upon the "op".') |
Will Bradley | 88b1ffc | 2019-08-19 15:09:39 -0600 | [diff] [blame] | 24 | opts = parser.parse_args(argv) |
| 25 | |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 26 | if opts.arg and not metrics_lib.OP_EXPECTS_ARG[opts.op]: |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 27 | # We do not expect to get an |arg| for this |op|. |
Alex Klein | 8718c03 | 2022-04-19 14:06:03 -0600 | [diff] [blame] | 28 | parser.error('Unexpected arg "%s" given for op "%s"' % (opts.arg, opts.op)) |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 29 | |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame] | 30 | timestamp = metrics_lib.current_milli_time() |
| 31 | metrics_lib.append_metrics_log(timestamp, opts.name, opts.op, arg=opts.arg) |