blob: 9318f363a393c19167591a2f7d7f52045758abd1 [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
Mike Frysinger9f9fe912019-09-09 16:19:55 -040011from chromite.lib import cros_logging as logging
Will Bradley7e5b8c12019-07-30 12:44:15 -060012from chromite.utils import metrics
13
14
15def deserialize_metrics_log(output_events, prefix=None):
16 """Read the current metrics events, adding to output_events.
17
18 This layer facilitates converting between the internal
19 chromite.utils.metrics representation of metric events and the
20 infra/proto/src/chromiumos/metrics.proto output type.
21
22 Args:
23 output_events: A chromiumos.MetricEvent protobuf message.
24 prefix: A string to prepend to all metric event names.
25 """
26 timers = {}
27
28 def make_name(name):
29 """Prepend a closed-over prefix to the given name."""
30 if prefix:
31 return '%s.%s' % (prefix, name)
32 else:
33 return name
34
35 # Reduce over the input events to append output_events.
36 for input_event in metrics.read_metrics_events():
37 if input_event.op == metrics.OP_START_TIMER:
Will Bradley333d22c2019-09-11 15:04:59 -060038 timers[input_event.arg] = (input_event.name,
Will Bradley7e5b8c12019-07-30 12:44:15 -060039 input_event.timestamp_epoch_millis)
40 elif input_event.op == metrics.OP_STOP_TIMER:
Mike Frysinger9f9fe912019-09-09 16:19:55 -040041 # TODO(wbbradley): Drop the None fallback https://crbug.com/1001909.
Will Bradley333d22c2019-09-11 15:04:59 -060042 timer = timers.pop(input_event.arg, None)
Mike Frysinger9f9fe912019-09-09 16:19:55 -040043 if timer is None:
44 logging.error('%s: stop timer recorded, but missing start timer!?',
Will Bradley333d22c2019-09-11 15:04:59 -060045 input_event.arg)
Will Bradley7e5b8c12019-07-30 12:44:15 -060046 if timer:
47 assert input_event.name == timer[0]
48 output_event = output_events.add()
49 output_event.name = make_name(timer[0])
50 output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
51 output_event.duration_milliseconds = (
52 output_event.timestamp_milliseconds - timer[1])
53 elif input_event.op == metrics.OP_NAMED_EVENT:
54 output_event = output_events.add()
55 output_event.name = make_name(input_event.name)
56 output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
Will Bradley333d22c2019-09-11 15:04:59 -060057 elif input_event.op == metrics.OP_GAUGE:
58 output_event = output_events.add()
59 output_event.name = make_name(input_event.name)
60 output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
61 output_event.gauge = input_event.arg
Will Bradley7e5b8c12019-07-30 12:44:15 -060062 else:
63 raise ValueError('unexpected op "%s" found in metric event: %s' % (
64 input_event.op, input_event))
65
66 # This is a sanity-check for unclosed timers.
Mike Frysinger5adbdab2019-09-10 14:01:39 -040067 # TODO(wbbradley): Turn this back into an assert https://crbug.com/1001909.
68 if timers:
69 logging.error('excess timer metric data left over: %s', timers)