def test_valid_metrics_by_components(self):
     metrics = [{
         "name": "name1",
         "dimensions": {
             "key1": "value1",
             "key2": "value2"
         },
         "timestamp": 1405630174123,
         "value": 1.0
     }, {
         "name": "name2",
         "dimensions": {
             "key1": "value1",
             "key2": "value2"
         },
         "value_meta": {
             "key1": "value1",
             "key2": "value2"
         },
         "timestamp": 1405630174123,
         "value": 2.0
     }]
     for i in six.moves.range(len(metrics)):
         metric_validator.validate_name(metrics[i]['name'])
         metric_validator.validate_value(metrics[i]['value'])
         metric_validator.validate_timestamp(metrics[i]['timestamp'])
         if 'dimensions' in metrics[i]:
             metric_validator.validate_dimensions(metrics[i]['dimensions'])
         if 'value_meta' in metrics[i]:
             metric_validator.validate_value_meta(metrics[i]['value_meta'])
Beispiel #2
0
    def submit_metric(self,
                      name,
                      value,
                      metric_class,
                      dimensions=None,
                      delegated_tenant=None,
                      hostname=None,
                      device_name=None,
                      value_meta=None,
                      timestamp=None,
                      sample_rate=1):
        # validate dimensions, name, value and value meta
        if dimensions:
            metric_validator.validate_dimensions(dimensions)

        metric_validator.validate_name(name)

        metric_validator.validate_value(value)

        if value_meta:
            metric_validator.validate_value_meta(value_meta)

        hostname_to_post = self.get_hostname_to_post(hostname)

        tenant_to_post = delegated_tenant or self.global_delegated_tenant

        dimensions_copy = dimensions.copy()

        if 'hostname' not in dimensions_copy and hostname_to_post:
            dimensions_copy.update({'hostname': hostname_to_post})

        # TODO(joe): Shouldn't device_name be added to dimensions in the check
        #            plugin?  Why is it special cased through so many layers?
        if device_name:
            dimensions_copy.update({'device': device_name})

        # TODO(joe): Decide if hostname_to_post and device_name are necessary
        #            for the context tuple
        context = (name, tuple(dimensions_copy.items()), tenant_to_post,
                   hostname_to_post, device_name)

        if context not in self.metrics:
            self.metrics[context] = metric_class(name,
                                                 dimensions_copy,
                                                 tenant=tenant_to_post)
        cur_time = time()
        if timestamp is not None:
            if cur_time - int(timestamp) > self.recent_point_threshold:
                log.debug(
                    "Discarding {0} - ts = {1}, current ts = {2} ".format(
                        name, timestamp, cur_time))
                self.num_discarded_old_points += 1
                return
        else:
            timestamp = cur_time
        self.metrics[context].value_meta = value_meta
        self.metrics[context].sample(value, sample_rate, timestamp)
Beispiel #3
0
    def submit_metric(self, name, value, metric_class, dimensions=None,
                      delegated_tenant=None, hostname=None, device_name=None,
                      value_meta=None, timestamp=None, sample_rate=1):
        # validate dimensions, name, value and value meta
        if dimensions:
            metric_validator.validate_dimensions(dimensions)

        metric_validator.validate_name(name)

        metric_validator.validate_value(value)

        if value_meta:
            metric_validator.validate_value_meta(value_meta)

        hostname_to_post = self.get_hostname_to_post(hostname)

        tenant_to_post = delegated_tenant or self.global_delegated_tenant

        dimensions_copy = dimensions.copy()

        if 'hostname' not in dimensions_copy and hostname_to_post:
            dimensions_copy.update({'hostname': hostname_to_post})

        # TODO(joe): Shouldn't device_name be added to dimensions in the check
        #            plugin?  Why is it special cased through so many layers?
        if device_name:
            dimensions_copy.update({'device': device_name})

        # TODO(joe): Decide if hostname_to_post and device_name are necessary
        #            for the context tuple
        context = (name, tuple(dimensions_copy.items()), tenant_to_post,
                   hostname_to_post, device_name)

        if context not in self.metrics:
            self.metrics[context] = metric_class(name,
                                                 dimensions_copy,
                                                 tenant=tenant_to_post)
        cur_time = time()
        if timestamp is not None:
            if cur_time - int(timestamp) > self.recent_point_threshold:
                log.debug(
                    "Discarding {0} - ts = {1}, current ts = {2} ".format(name,
                                                                          timestamp,
                                                                          cur_time))
                self.num_discarded_old_points += 1
                return
        else:
            timestamp = cur_time
        self.metrics[context].value_meta = value_meta
        self.metrics[context].sample(value, sample_rate, timestamp)
 def test_valid_metrics_by_components(self):
     metrics = [
         {"name": "name1",
          "dimensions": {"key1": "value1",
                         "key2": "value2"},
          "timestamp": 1405630174123,
          "value": 1.0},
         {"name": "name2",
          "dimensions": {"key1": "value1",
                         "key2": "value2"},
          "value_meta": {"key1": "value1",
                         "key2": "value2"},
          "timestamp": 1405630174123,
          "value": 2.0}
     ]
     for i in six.moves.range(len(metrics)):
         metric_validator.validate_name(metrics[i]['name'])
         metric_validator.validate_value(metrics[i]['value'])
         metric_validator.validate_timestamp(metrics[i]['timestamp'])
         if 'dimensions' in metrics[i]:
             metric_validator.validate_dimensions(metrics[i]['dimensions'])
         if 'value_meta' in metrics[i]:
             metric_validator.validate_value_meta(metrics[i]['value_meta'])