blob: c61c0df30b9be4363ab6b7415c5ad2b0e280dc37 [file] [log] [blame]
Will Bradley7e5b8c12019-07-30 12:44:15 -06001# 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"""Metrics for general consumption.
6
7See infra/proto/metrics.proto for a description of the type of record that this
8module will be creating.
9"""
10
Chris McDonald1672ddb2021-07-21 11:48:23 -060011import logging
12
Will Bradley7e5b8c12019-07-30 12:44:15 -060013from chromite.utils import metrics
14
15
16def deserialize_metrics_log(output_events, prefix=None):
17 """Read the current metrics events, adding to output_events.
18
19 This layer facilitates converting between the internal
20 chromite.utils.metrics representation of metric events and the
21 infra/proto/src/chromiumos/metrics.proto output type.
22
23 Args:
24 output_events: A chromiumos.MetricEvent protobuf message.
25 prefix: A string to prepend to all metric event names.
26 """
27 timers = {}
28
29 def make_name(name):
30 """Prepend a closed-over prefix to the given name."""
31 if prefix:
32 return '%s.%s' % (prefix, name)
33 else:
34 return name
35
36 # Reduce over the input events to append output_events.
37 for input_event in metrics.read_metrics_events():
38 if input_event.op == metrics.OP_START_TIMER:
Will Bradley333d22c2019-09-11 15:04:59 -060039 timers[input_event.arg] = (input_event.name,
Will Bradley7e5b8c12019-07-30 12:44:15 -060040 input_event.timestamp_epoch_millis)
41 elif input_event.op == metrics.OP_STOP_TIMER:
Mike Frysinger9f9fe912019-09-09 16:19:55 -040042 # TODO(wbbradley): Drop the None fallback https://crbug.com/1001909.
Will Bradley333d22c2019-09-11 15:04:59 -060043 timer = timers.pop(input_event.arg, None)
Mike Frysinger9f9fe912019-09-09 16:19:55 -040044 if timer is None:
45 logging.error('%s: stop timer recorded, but missing start timer!?',
Will Bradley333d22c2019-09-11 15:04:59 -060046 input_event.arg)
Will Bradley7e5b8c12019-07-30 12:44:15 -060047 if timer:
48 assert input_event.name == timer[0]
49 output_event = output_events.add()
50 output_event.name = make_name(timer[0])
51 output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
52 output_event.duration_milliseconds = (
53 output_event.timestamp_milliseconds - timer[1])
54 elif input_event.op == metrics.OP_NAMED_EVENT:
55 output_event = output_events.add()
56 output_event.name = make_name(input_event.name)
57 output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
Will Bradley333d22c2019-09-11 15:04:59 -060058 elif input_event.op == metrics.OP_GAUGE:
59 output_event = output_events.add()
60 output_event.name = make_name(input_event.name)
61 output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
62 output_event.gauge = input_event.arg
Will Bradley7e5b8c12019-07-30 12:44:15 -060063 else:
64 raise ValueError('unexpected op "%s" found in metric event: %s' % (
65 input_event.op, input_event))
66
67 # This is a sanity-check for unclosed timers.
Mike Frysinger5adbdab2019-09-10 14:01:39 -040068 # TODO(wbbradley): Turn this back into an assert https://crbug.com/1001909.
69 if timers:
70 logging.error('excess timer metric data left over: %s', timers)