示例#1
0
    def test_gauge(self):
        ag_interval = 2
        stats = MetricsBucketAggregator('myhost', interval=ag_interval)
        self.wait_for_bucket_boundary(ag_interval)

        # Track some counters.
        stats.submit_packets('my.first.gauge:1|g')
        stats.submit_packets('my.first.gauge:5|g')
        stats.submit_packets('my.second.gauge:1.5|g')

        # Ensure that gauges roll up correctly.
        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equals(len(metrics), 2)

        first, second = metrics

        nt.assert_equals(first['metric'], 'my.first.gauge')
        nt.assert_equals(first['points'][0][1], 5)
        nt.assert_equals(first['host'], 'myhost')

        nt.assert_equals(second['metric'], 'my.second.gauge')
        nt.assert_equals(second['points'][0][1], 1.5)

        # Ensure that old gauges get dropped due to old timestamps
        stats.submit_metric('my.first.gauge', 5, 'g')
        stats.submit_metric('my.first.gauge', 1, 'g', timestamp=1000000000)
        stats.submit_metric('my.second.gauge', 20, 'g', timestamp=1000000000)

        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equals(len(metrics), 1)

        first = metrics[0]

        nt.assert_equals(first['metric'], 'my.first.gauge')
        nt.assert_equals(first['points'][0][1], 5)
        nt.assert_equals(first['host'], 'myhost')
    def test_gauge(self):
        ag_interval = 2
        stats = MetricsBucketAggregator('myhost', interval=ag_interval)
        self.wait_for_bucket_boundary(ag_interval)

        # Track some counters.
        stats.submit_packets('my.first.gauge:1|g')
        stats.submit_packets('my.first.gauge:5|g')
        stats.submit_packets('my.second.gauge:1.5|g')

        # Ensure that gauges roll up correctly.
        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equals(len(metrics), 2)

        first, second = metrics

        nt.assert_equals(first['metric'], 'my.first.gauge')
        nt.assert_equals(first['points'][0][1], 5)
        nt.assert_equals(first['host'], 'myhost')

        nt.assert_equals(second['metric'], 'my.second.gauge')
        nt.assert_equals(second['points'][0][1], 1.5)

        # Ensure that old gauges get dropped due to old timestamps
        stats.submit_metric('my.first.gauge', 5, 'g')
        stats.submit_metric('my.first.gauge', 1, 'g', timestamp=1000000000)
        stats.submit_metric('my.second.gauge', 20, 'g', timestamp=1000000000)

        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equals(len(metrics), 1)

        first = metrics[0]

        nt.assert_equals(first['metric'], 'my.first.gauge')
        nt.assert_equals(first['points'][0][1], 5)
        nt.assert_equals(first['host'], 'myhost')
    def test_gauge(self):
        ag_interval = 2
        stats = MetricsBucketAggregator("myhost", interval=ag_interval)
        self.wait_for_bucket_boundary(ag_interval)

        # Track some counters.
        stats.submit_packets("my.first.gauge:1|g")
        stats.submit_packets("my.first.gauge:5|g")
        stats.submit_packets("my.second.gauge:1.5|g")

        # Ensure that gauges roll up correctly.
        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equals(len(metrics), 2)

        first, second = metrics

        nt.assert_equals(first["metric"], "my.first.gauge")
        nt.assert_equals(first["points"][0][1], 5)
        nt.assert_equals(first["host"], "myhost")

        nt.assert_equals(second["metric"], "my.second.gauge")
        nt.assert_equals(second["points"][0][1], 1.5)

        # Ensure that old gauges get dropped due to old timestamps
        stats.submit_metric("my.first.gauge", 5, "g")
        stats.submit_metric("my.first.gauge", 1, "g", timestamp=1000000000)
        stats.submit_metric("my.second.gauge", 20, "g", timestamp=1000000000)

        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equals(len(metrics), 1)

        first = metrics[0]

        nt.assert_equals(first["metric"], "my.first.gauge")
        nt.assert_equals(first["points"][0][1], 5)
        nt.assert_equals(first["host"], "myhost")
示例#4
0
    def test_recent_point_threshold(self):
        ag_interval = 1
        threshold = 100
        stats = MetricsBucketAggregator('myhost',
                                        recent_point_threshold=threshold,
                                        interval=ag_interval)
        timestamp_beyond_threshold = time.time() - threshold * 2

        # Ensure that old gauges get dropped due to old timestamps
        stats.submit_metric('my.first.gauge', 5, 'g')
        stats.submit_metric('my.first.gauge',
                            1,
                            'g',
                            timestamp=timestamp_beyond_threshold)
        stats.submit_metric('my.second.gauge',
                            20,
                            'g',
                            timestamp=timestamp_beyond_threshold)

        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        assert len(metrics) == 1

        first = metrics[0]
        nt.assert_equals(first['metric'], 'my.first.gauge')
        nt.assert_equals(first['points'][0][1], 5)
        nt.assert_equals(first['host'], 'myhost')

        timestamp_within_threshold = time.time() - threshold / 2
        bucket_for_timestamp_within_threshold = timestamp_within_threshold - (
            timestamp_within_threshold % ag_interval)
        stats.submit_metric('my.1.gauge', 5, 'g')
        stats.submit_metric('my.1.gauge',
                            1,
                            'g',
                            timestamp=timestamp_within_threshold)
        stats.submit_metric('my.2.counter',
                            20,
                            'c',
                            timestamp=timestamp_within_threshold)
        stats.submit_metric('my.3.set',
                            20,
                            's',
                            timestamp=timestamp_within_threshold)
        stats.submit_metric('my.4.histogram',
                            20,
                            'h',
                            timestamp=timestamp_within_threshold)

        self.sleep_for_interval_length(ag_interval)
        flush_timestamp = time.time()
        # The bucket timestamp is the beginning of the bucket that ended before we flushed
        bucket_timestamp = flush_timestamp - (flush_timestamp %
                                              ag_interval) - ag_interval
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equal(len(metrics), 10)

        first, first_b, second, second_b, third, h1, h2, h3, h4, h5 = metrics
        nt.assert_equals(first['metric'], 'my.1.gauge')
        nt.assert_equals(first['points'][0][1], 1)
        nt.assert_equals(first['host'], 'myhost')
        self.assert_almost_equal(first['points'][0][0],
                                 bucket_for_timestamp_within_threshold, 0.1)
        nt.assert_equals(first_b['metric'], 'my.1.gauge')
        nt.assert_equals(first_b['points'][0][1], 5)
        self.assert_almost_equal(first_b['points'][0][0], bucket_timestamp,
                                 0.1)

        nt.assert_equals(second['metric'], 'my.2.counter')
        nt.assert_equals(second['points'][0][1], 20)
        self.assert_almost_equal(second['points'][0][0],
                                 bucket_for_timestamp_within_threshold, 0.1)
        nt.assert_equals(second_b['metric'], 'my.2.counter')
        nt.assert_equals(second_b['points'][0][1], 0)
        self.assert_almost_equal(second_b['points'][0][0], bucket_timestamp,
                                 0.1)

        nt.assert_equals(third['metric'], 'my.3.set')
        nt.assert_equals(third['points'][0][1], 1)
        self.assert_almost_equal(third['points'][0][0],
                                 bucket_for_timestamp_within_threshold, 0.1)

        nt.assert_equals(h1['metric'], 'my.4.histogram.95percentile')
        nt.assert_equals(h1['points'][0][1], 20)
        self.assert_almost_equal(h1['points'][0][0],
                                 bucket_for_timestamp_within_threshold, 0.1)
        nt.assert_equal(h1['points'][0][0], h2['points'][0][0])
        nt.assert_equal(h1['points'][0][0], h3['points'][0][0])
        nt.assert_equal(h1['points'][0][0], h4['points'][0][0])
        nt.assert_equal(h1['points'][0][0], h5['points'][0][0])
    def test_recent_point_threshold(self):
        ag_interval = 1
        threshold = 100
        # The min is not enabled by default
        stats = MetricsBucketAggregator(
            'myhost',
            recent_point_threshold=threshold,
            interval=ag_interval,
            histogram_aggregates=DEFAULT_HISTOGRAM_AGGREGATES+['min']
        )
        timestamp_beyond_threshold = time.time() - threshold*2

        # Ensure that old gauges get dropped due to old timestamps
        stats.submit_metric('my.first.gauge', 5, 'g')
        stats.submit_metric('my.first.gauge', 1, 'g', timestamp=timestamp_beyond_threshold)
        stats.submit_metric('my.second.gauge', 20, 'g', timestamp=timestamp_beyond_threshold)

        self.sleep_for_interval_length(ag_interval)
        metrics = self.sort_metrics(stats.flush())
        assert len(metrics) == 1

        first = metrics[0]
        nt.assert_equals(first['metric'], 'my.first.gauge')
        nt.assert_equals(first['points'][0][1], 5)
        nt.assert_equals(first['host'], 'myhost')

        timestamp_within_threshold = time.time() - threshold/2
        bucket_for_timestamp_within_threshold = timestamp_within_threshold - (timestamp_within_threshold % ag_interval)
        stats.submit_metric('my.1.gauge', 5, 'g')
        stats.submit_metric('my.1.gauge', 1, 'g', timestamp=timestamp_within_threshold)
        stats.submit_metric('my.2.counter', 20, 'c', timestamp=timestamp_within_threshold)
        stats.submit_metric('my.3.set', 20, 's', timestamp=timestamp_within_threshold)
        stats.submit_metric('my.4.histogram', 20, 'h', timestamp=timestamp_within_threshold)

        self.sleep_for_interval_length(ag_interval)
        flush_timestamp = time.time()
        # The bucket timestamp is the beginning of the bucket that ended before we flushed
        bucket_timestamp = flush_timestamp - (flush_timestamp % ag_interval) - ag_interval
        metrics = self.sort_metrics(stats.flush())
        nt.assert_equal(len(metrics), 11)

        first, first_b, second, second_b, third, h1, h2, h3, h4, h5, h6 = metrics
        nt.assert_equals(first['metric'], 'my.1.gauge')
        nt.assert_equals(first['points'][0][1], 1)
        nt.assert_equals(first['host'], 'myhost')
        self.assert_almost_equal(first['points'][0][0], bucket_for_timestamp_within_threshold, 0.1)
        nt.assert_equals(first_b['metric'], 'my.1.gauge')
        nt.assert_equals(first_b['points'][0][1], 5)
        self.assert_almost_equal(first_b['points'][0][0], bucket_timestamp, 0.1)

        nt.assert_equals(second['metric'], 'my.2.counter')
        nt.assert_equals(second['points'][0][1], 20)
        self.assert_almost_equal(second['points'][0][0], bucket_for_timestamp_within_threshold, 0.1)
        nt.assert_equals(second_b['metric'], 'my.2.counter')
        nt.assert_equals(second_b['points'][0][1], 0)
        self.assert_almost_equal(second_b['points'][0][0], bucket_timestamp, 0.1)

        nt.assert_equals(third['metric'], 'my.3.set')
        nt.assert_equals(third['points'][0][1], 1)
        self.assert_almost_equal(third['points'][0][0], bucket_for_timestamp_within_threshold, 0.1)

        nt.assert_equals(h1['metric'], 'my.4.histogram.95percentile')
        nt.assert_equals(h1['points'][0][1], 20)
        self.assert_almost_equal(h1['points'][0][0], bucket_for_timestamp_within_threshold, 0.1)
        nt.assert_equal(h1['points'][0][0], h2['points'][0][0])
        nt.assert_equal(h1['points'][0][0], h3['points'][0][0])
        nt.assert_equal(h1['points'][0][0], h4['points'][0][0])
        nt.assert_equal(h1['points'][0][0], h5['points'][0][0])