Formatting: Format all python code with black.
This CL is probably not what you're looking for, it's only
automated formatting. Ignore it with
`git blame --ignore-rev <revision>` for this commit.
BUG=b:233893248
TEST=CQ
Change-Id: I66591d7a738d241aed3290138c0f68065ab10a6d
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/3879174
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Tested-by: Alex Klein <saklein@chromium.org>
diff --git a/api/metrics.py b/api/metrics.py
index 653d295..c0c5b26 100644
--- a/api/metrics.py
+++ b/api/metrics.py
@@ -15,75 +15,90 @@
def deserialize_metrics_log(output_events, prefix=None):
- """Read the current metrics events, adding to output_events.
+ """Read the current metrics events, adding to output_events.
- This layer facilitates converting between the internal
- chromite.utils.metrics representation of metric events and the
- infra/proto/src/chromiumos/metrics.proto output type.
+ This layer facilitates converting between the internal
+ chromite.utils.metrics representation of metric events and the
+ infra/proto/src/chromiumos/metrics.proto output type.
- Args:
- output_events: A chromiumos.MetricEvent protobuf message.
- prefix: A string to prepend to all metric event names.
- """
- counters = collections.defaultdict(int)
- counter_times = {}
- timers = {}
+ Args:
+ output_events: A chromiumos.MetricEvent protobuf message.
+ prefix: A string to prepend to all metric event names.
+ """
+ counters = collections.defaultdict(int)
+ counter_times = {}
+ timers = {}
- def make_name(name):
- """Prepend a closed-over prefix to the given name."""
- if prefix:
- return '%s.%s' % (prefix, name)
- else:
- return name
+ def make_name(name):
+ """Prepend a closed-over prefix to the given name."""
+ if prefix:
+ return "%s.%s" % (prefix, name)
+ else:
+ return name
- # Reduce over the input events to append output_events.
- for input_event in metrics_lib.read_metrics_events():
- if input_event.op == metrics_lib.OP_START_TIMER:
- timers[input_event.arg] = (input_event.name,
- input_event.timestamp_epoch_millis)
- elif input_event.op == metrics_lib.OP_STOP_TIMER:
- # TODO(wbbradley): Drop the None fallback https://crbug.com/1001909.
- timer = timers.pop(input_event.arg, None)
- if timer is None:
- logging.error('%s: stop timer recorded, but missing start timer!?',
- input_event.arg)
- if timer:
- assert input_event.name == timer[0]
+ # Reduce over the input events to append output_events.
+ for input_event in metrics_lib.read_metrics_events():
+ if input_event.op == metrics_lib.OP_START_TIMER:
+ timers[input_event.arg] = (
+ input_event.name,
+ input_event.timestamp_epoch_millis,
+ )
+ elif input_event.op == metrics_lib.OP_STOP_TIMER:
+ # TODO(wbbradley): Drop the None fallback https://crbug.com/1001909.
+ timer = timers.pop(input_event.arg, None)
+ if timer is None:
+ logging.error(
+ "%s: stop timer recorded, but missing start timer!?",
+ input_event.arg,
+ )
+ if timer:
+ assert input_event.name == timer[0]
+ output_event = output_events.add()
+ output_event.name = make_name(timer[0])
+ output_event.timestamp_milliseconds = (
+ input_event.timestamp_epoch_millis
+ )
+ output_event.duration_milliseconds = (
+ output_event.timestamp_milliseconds - timer[1]
+ )
+ elif input_event.op == metrics_lib.OP_NAMED_EVENT:
+ output_event = output_events.add()
+ output_event.name = make_name(input_event.name)
+ output_event.timestamp_milliseconds = (
+ input_event.timestamp_epoch_millis
+ )
+ elif input_event.op == metrics_lib.OP_GAUGE:
+ output_event = output_events.add()
+ output_event.name = make_name(input_event.name)
+ output_event.timestamp_milliseconds = (
+ input_event.timestamp_epoch_millis
+ )
+ output_event.gauge = input_event.arg
+ elif input_event.op == metrics_lib.OP_INCREMENT_COUNTER:
+ counters[input_event.name] += input_event.arg
+ counter_times[input_event.name] = max(
+ input_event.timestamp_epoch_millis,
+ counter_times.get(input_event.name, 0),
+ )
+ elif input_event.op == metrics_lib.OP_DECREMENT_COUNTER:
+ counters[input_event.name] -= input_event.arg
+ counter_times[input_event.name] = max(
+ input_event.timestamp_epoch_millis,
+ counter_times.get(input_event.name, 0),
+ )
+ else:
+ raise ValueError(
+ 'unexpected op "%s" found in metric event: %s'
+ % (input_event.op, input_event)
+ )
+
+ for counter, value in counters.items():
output_event = output_events.add()
- output_event.name = make_name(timer[0])
- output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
- output_event.duration_milliseconds = (
- output_event.timestamp_milliseconds - timer[1])
- elif input_event.op == metrics_lib.OP_NAMED_EVENT:
- output_event = output_events.add()
- output_event.name = make_name(input_event.name)
- output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
- elif input_event.op == metrics_lib.OP_GAUGE:
- output_event = output_events.add()
- output_event.name = make_name(input_event.name)
- output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
- output_event.gauge = input_event.arg
- elif input_event.op == metrics_lib.OP_INCREMENT_COUNTER:
- counters[input_event.name] += input_event.arg
- counter_times[input_event.name] = max(
- input_event.timestamp_epoch_millis,
- counter_times.get(input_event.name, 0))
- elif input_event.op == metrics_lib.OP_DECREMENT_COUNTER:
- counters[input_event.name] -= input_event.arg
- counter_times[input_event.name] = max(
- input_event.timestamp_epoch_millis,
- counter_times.get(input_event.name, 0))
- else:
- raise ValueError('unexpected op "%s" found in metric event: %s' %
- (input_event.op, input_event))
+ output_event.name = make_name(counter)
+ output_event.gauge = value
+ output_event.timestamp_milliseconds = counter_times[counter]
- for counter, value in counters.items():
- output_event = output_events.add()
- output_event.name = make_name(counter)
- output_event.gauge = value
- output_event.timestamp_milliseconds = counter_times[counter]
-
- # Check for any unhandled timers.
- # TODO(wbbradley): Turn this back into an assert https://crbug.com/1001909.
- if timers:
- logging.error('excess timer metric data left over: %s', timers)
+ # Check for any unhandled timers.
+ # TODO(wbbradley): Turn this back into an assert https://crbug.com/1001909.
+ if timers:
+ logging.error("excess timer metric data left over: %s", timers)