Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -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 | """Metrics for general consumption. |
| 6 | |
| 7 | See infra/proto/metrics.proto for a description of the type of record that this |
| 8 | module will be creating. |
| 9 | """ |
| 10 | |
Alex Klein | 9964a1c | 2022-04-19 09:42:13 -0600 | [diff] [blame] | 11 | import collections |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 12 | import logging |
| 13 | |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 14 | from chromite.lib import metrics_lib |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 15 | |
| 16 | |
| 17 | def deserialize_metrics_log(output_events, prefix=None): |
| 18 | """Read the current metrics events, adding to output_events. |
| 19 | |
| 20 | This layer facilitates converting between the internal |
| 21 | chromite.utils.metrics representation of metric events and the |
| 22 | infra/proto/src/chromiumos/metrics.proto output type. |
| 23 | |
| 24 | Args: |
| 25 | output_events: A chromiumos.MetricEvent protobuf message. |
| 26 | prefix: A string to prepend to all metric event names. |
| 27 | """ |
Alex Klein | 9964a1c | 2022-04-19 09:42:13 -0600 | [diff] [blame] | 28 | counters = collections.defaultdict(int) |
| 29 | counter_times = {} |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 30 | timers = {} |
| 31 | |
| 32 | def make_name(name): |
| 33 | """Prepend a closed-over prefix to the given name.""" |
| 34 | if prefix: |
| 35 | return '%s.%s' % (prefix, name) |
| 36 | else: |
| 37 | return name |
| 38 | |
| 39 | # Reduce over the input events to append output_events. |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 40 | for input_event in metrics_lib.read_metrics_events(): |
| 41 | if input_event.op == metrics_lib.OP_START_TIMER: |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 42 | timers[input_event.arg] = (input_event.name, |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 43 | input_event.timestamp_epoch_millis) |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 44 | elif input_event.op == metrics_lib.OP_STOP_TIMER: |
Mike Frysinger | 9f9fe91 | 2019-09-09 16:19:55 -0400 | [diff] [blame] | 45 | # TODO(wbbradley): Drop the None fallback https://crbug.com/1001909. |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 46 | timer = timers.pop(input_event.arg, None) |
Mike Frysinger | 9f9fe91 | 2019-09-09 16:19:55 -0400 | [diff] [blame] | 47 | if timer is None: |
| 48 | logging.error('%s: stop timer recorded, but missing start timer!?', |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 49 | input_event.arg) |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 50 | if timer: |
| 51 | assert input_event.name == timer[0] |
| 52 | output_event = output_events.add() |
| 53 | output_event.name = make_name(timer[0]) |
| 54 | output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis |
| 55 | output_event.duration_milliseconds = ( |
| 56 | output_event.timestamp_milliseconds - timer[1]) |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 57 | elif input_event.op == metrics_lib.OP_NAMED_EVENT: |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 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 |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 61 | elif input_event.op == metrics_lib.OP_GAUGE: |
Will Bradley | 333d22c | 2019-09-11 15:04:59 -0600 | [diff] [blame] | 62 | output_event = output_events.add() |
| 63 | output_event.name = make_name(input_event.name) |
| 64 | output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis |
| 65 | output_event.gauge = input_event.arg |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 66 | elif input_event.op == metrics_lib.OP_INCREMENT_COUNTER: |
Alex Klein | 9964a1c | 2022-04-19 09:42:13 -0600 | [diff] [blame] | 67 | counters[input_event.name] += input_event.arg |
| 68 | counter_times[input_event.name] = max( |
| 69 | input_event.timestamp_epoch_millis, |
| 70 | counter_times.get(input_event.name, 0)) |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 71 | elif input_event.op == metrics_lib.OP_DECREMENT_COUNTER: |
Alex Klein | 9964a1c | 2022-04-19 09:42:13 -0600 | [diff] [blame] | 72 | counters[input_event.name] -= input_event.arg |
| 73 | counter_times[input_event.name] = max( |
| 74 | input_event.timestamp_epoch_millis, |
| 75 | counter_times.get(input_event.name, 0)) |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 76 | else: |
Alex Klein | aef4194 | 2022-04-19 14:13:17 -0600 | [diff] [blame^] | 77 | raise ValueError('unexpected op "%s" found in metric event: %s' % |
| 78 | (input_event.op, input_event)) |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 79 | |
Alex Klein | 9964a1c | 2022-04-19 09:42:13 -0600 | [diff] [blame] | 80 | for counter, value in counters.items(): |
| 81 | output_event = output_events.add() |
| 82 | output_event.name = make_name(counter) |
| 83 | output_event.gauge = value |
| 84 | output_event.timestamp_milliseconds = counter_times[counter] |
| 85 | |
Will Bradley | 7e5b8c1 | 2019-07-30 12:44:15 -0600 | [diff] [blame] | 86 | # This is a sanity-check for unclosed timers. |
Mike Frysinger | 5adbdab | 2019-09-10 14:01:39 -0400 | [diff] [blame] | 87 | # TODO(wbbradley): Turn this back into an assert https://crbug.com/1001909. |
| 88 | if timers: |
| 89 | logging.error('excess timer metric data left over: %s', timers) |