Example #1
0
def _generate_proto():
  """Generate MetricsCollection for global_monitor.send()."""
  proto = metrics_pb2.MetricsCollection()

  error_count = 0
  for target, metric, start_time, _, fields_values in state.store.get_all():
    for fields, value in fields_values.iteritems():
      if len(proto.data) >= METRICS_DATA_LENGTH_LIMIT:
        yield proto
        proto = metrics_pb2.MetricsCollection()

      try:
        metrics_pb = metrics_pb2.MetricsData()
        metric.serialize_to(metrics_pb, start_time, fields, value, target)
      except errors.MonitoringError:
        error_count += 1
        logging.exception('Failed to serialize a metric.')
        continue

      proto.data.add().CopyFrom(metrics_pb)

  if len(proto.data) > 0:
    yield proto

  if error_count:
    raise errors.MonitoringFailedToFlushAllMetricsError(error_count)
Example #2
0
def _generate_proto():
    """Generate MetricsCollection for global_monitor.send()."""
    proto = metrics_pb2.MetricsCollection()

    for target, metric, start_time, _, fields_values in state.store.get_all():
        for fields, value in fields_values.iteritems():
            if len(proto.data) >= METRICS_DATA_LENGTH_LIMIT:
                yield proto
                proto = metrics_pb2.MetricsCollection()

            metric.serialize_to(proto, start_time, fields, value, target)

    if len(proto.data) > 0:
        yield proto
Example #3
0
    def _wrap_proto(data):
        """Normalize MetricsData, list(MetricsData), and MetricsCollection.

    Args:
      input: A MetricsData, list of MetricsData, or a MetricsCollection.

    Returns:
      A MetricsCollection with the appropriate data attribute set.
    """
        if isinstance(data, metrics_pb2.MetricsCollection):
            ret = data
        elif isinstance(data, list):
            ret = metrics_pb2.MetricsCollection(data=data)
        else:
            ret = metrics_pb2.MetricsCollection(data=[data])
        return ret