blob: b0929499e645ac68166f713c1b4c72ad3bf334b1 [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright 2019 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Metrics for general consumption.
See infra/proto/metrics.proto for a description of the type of record that this
module will be creating.
from __future__ import print_function
from chromite.utils import metrics
def deserialize_metrics_log(output_events, prefix=None):
"""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.
output_events: A chromiumos.MetricEvent protobuf message.
prefix: A string to prepend to all metric event names.
timers = {}
def make_name(name):
"""Prepend a closed-over prefix to the given name."""
if prefix:
return '%s.%s' % (prefix, name)
return name
# Reduce over the input events to append output_events.
for input_event in metrics.read_metrics_events():
if input_event.op == metrics.OP_START_TIMER:
timers[input_event.key] = (,
elif input_event.op == metrics.OP_STOP_TIMER:
timer = timers.pop(input_event.key)
if timer:
assert == timer[0]
output_event = output_events.add() = 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.OP_NAMED_EVENT:
output_event = output_events.add() = make_name(
output_event.timestamp_milliseconds = input_event.timestamp_epoch_millis
raise ValueError('unexpected op "%s" found in metric event: %s' % (
input_event.op, input_event))
# This is a sanity-check for unclosed timers.
assert len(timers.values()) == 0