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): if not self.FIELD_NAME_PATTERN.match(name): raise errors.MetricDefinitionError( 'Invalid metric field name "%s" - must match the regex "%s"' % (name, self.FIELD_NAME_PATTERN.pattern)) self.name = name
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)