blob: a4bb2ee403b83127d4a253eede25e453a2fd66a2 [file] [log] [blame]
Mike Frysingerf1ba7ad2022-09-12 05:42:57 -04001# Copyright 2019 The ChromiumOS Authors
Will Bradley88b1ffc2019-08-19 15:09:39 -06002# 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 Bradley88b1ffc2019-08-19 15:09:39 -06007from chromite.lib import commandline
Alex Kleinaef41942022-04-19 14:13:17 -06008from chromite.lib import metrics_lib
Will Bradley88b1ffc2019-08-19 15:09:39 -06009
10
11def main(argv):
Alex Klein1699fab2022-09-08 08:46:06 -060012 """Emit a metric event."""
13 parser = commandline.ArgumentParser(description=__doc__)
14 parser.add_argument(
15 "op",
16 choices=sorted(metrics_lib.VALID_OPS),
17 help="Which metric event operator to emit.",
18 )
19 parser.add_argument(
20 "name",
21 help="The name of the metric event as you would like it "
22 "to appear downstream in data stores.",
23 )
24 parser.add_argument(
25 "arg", nargs="?", help='An accessory argument dependent upon the "op".'
26 )
27 opts = parser.parse_args(argv)
Will Bradley88b1ffc2019-08-19 15:09:39 -060028
Alex Klein1699fab2022-09-08 08:46:06 -060029 if opts.arg and not metrics_lib.OP_EXPECTS_ARG[opts.op]:
30 # We do not expect to get an |arg| for this |op|.
31 parser.error(
32 'Unexpected arg "%s" given for op "%s"' % (opts.arg, opts.op)
33 )
Will Bradley333d22c2019-09-11 15:04:59 -060034
Alex Klein1699fab2022-09-08 08:46:06 -060035 timestamp = metrics_lib.current_milli_time()
36 metrics_lib.append_metrics_log(timestamp, opts.name, opts.op, arg=opts.arg)