def __init__(self, name, description, field_spec, units=None, target_type=None): """Create an instance of a Metric. Args: name (str): the file-like name of this metric description (string): help string for the metric. Should be enough to know what the metric is about. field_spec (list): a list of Field subclasses to define the fields that are allowed on this metric. Pass a list of either StringField, IntegerField or BooleanField here. units (string): the unit used to measure data for given metric. Some common units are pre-defined in the MetricsDataUnits class. target_type (type): the subclass of google.protobuf.message.Message that represents the target type. """ field_spec = field_spec or [] self._name = name.lstrip('/') if not isinstance(description, basestring): raise errors.MetricDefinitionError( 'Metric description must be a string') if not description: raise errors.MetricDefinitionError( 'Metric must have a description') if (not isinstance(field_spec, (list, tuple)) or any(not isinstance(x, Field) for x in field_spec)): raise errors.MetricDefinitionError( 'Metric constructor takes a list of Fields, or None') if len(field_spec) > 12: # Monarch imposes a limit of a cardinality of 5000 for a single metric # (see http://shortn/_WBupjZf2of). # If a user set 12 fields, and each of those is just a boolean field # with two possible values, the _lower limit_ on the cardinality of # that metric is 2^12, or 4096. # Note that since a combination of 5 built-in fields is fixed, we do # not need to count them. raise errors.MonitoringTooManyFieldsError(self._name, field_spec) if target_type and not (inspect.isclass(target_type) and issubclass(target_type, message.Message)): raise errors.MetricDefinitionError( 'Metric target type must be a class (not an instance of a class) ' 'and that must be a subclass of google.protobuf.message.Message.' ) self._start_time = None self._field_spec = field_spec self._sorted_field_names = sorted(x.name for x in field_spec) self._description = description self._units = units self._target_type = target_type self._enable_cumulative_set = False interface.register(self)
def __init__(self, name, fields=None, description=None): """Create an instance of a Metric. Args: name (str): the file-like name of this metric fields (dict): a set of key-value pairs to be set as default metric fields description (string): help string for the metric. Should be enough to know what the metric is about. """ self._name = name.lstrip('/') self._start_time = None fields = fields or {} if len(fields) > 7: raise errors.MonitoringTooManyFieldsError(self._name, fields) self._fields = fields self._normalized_fields = self._normalize_fields(self._fields) self._description = description interface.register(self)
def __init__(self, name, description, field_spec, units=None): """Create an instance of a Metric. Args: name (str): the file-like name of this metric description (string): help string for the metric. Should be enough to know what the metric is about. field_spec (list): a list of Field subclasses to define the fields that are allowed on this metric. Pass a list of either StringField, IntegerField or BooleanField here. units (string): the unit used to measure data for given metric. Some common units are pre-defined in the MetricsDataUnits class. """ field_spec = field_spec or [] self._name = name.lstrip('/') if not isinstance(description, basestring): raise errors.MetricDefinitionError( 'Metric description must be a string') if not description: raise errors.MetricDefinitionError( 'Metric must have a description') if (not isinstance(field_spec, (list, tuple)) or any(not isinstance(x, Field) for x in field_spec)): raise errors.MetricDefinitionError( 'Metric constructor takes a list of Fields, or None') if len(field_spec) > 7: raise errors.MonitoringTooManyFieldsError(self._name, field_spec) self._start_time = None self._field_spec = field_spec self._sorted_field_names = sorted(x.name for x in field_spec) self._description = description self._units = units interface.register(self)
def _normalize_fields(self, fields): """Merges the fields with the default fields and returns something hashable. Args: fields (dict): A dict of fields passed by the user, or None. Returns: A tuple of (key, value) tuples, ordered by key. This whole tuple is used as the key in the self._values dict to identify the cell for a value. Raises: MonitoringTooManyFieldsError: if there are more than seven metric fields """ if fields is None: return self._normalized_fields all_fields = copy.copy(self._fields) all_fields.update(fields) if len(all_fields) > 7: raise errors.MonitoringTooManyFieldsError(self._name, all_fields) return tuple(sorted(all_fields.iteritems()))
def __init__(self, name, fields=None, description=None, units=None): """Create an instance of a Metric. Args: name (str): the file-like name of this metric fields (dict): a set of key-value pairs to be set as default metric fields description (string): help string for the metric. Should be enough to know what the metric is about. units (int): the unit used to measure data for given metric. Please use the attributes of MetricDataUnit to find valid integer values for this argument. """ self._name = name.lstrip('/') self._start_time = None fields = fields or {} if len(fields) > 7: raise errors.MonitoringTooManyFieldsError(self._name, fields) self._fields = fields self._normalized_fields = self._normalize_fields(self._fields) self._description = description self._units = units interface.register(self)