Exemplo n.º 1
0
    def test_gauge(self):
        # Create some fake metrics.
        dog = DogStatsApi()
        dog.start(roll_up_interval=10, flush_in_thread=False)
        reporter = dog.reporter = MemoryReporter()

        dog.gauge('test.gauge.1', 20, 100.0)
        dog.gauge('test.gauge.1', 22, 105.0)
        dog.gauge('test.gauge.2', 30, 115.0)
        dog.gauge('test.gauge.3', 30, 125.0)
        dog.flush(120.0)

        # Assert they've been properly flushed.
        metrics = self.sort_metrics(reporter.metrics)
        nt.assert_equal(len(metrics), 2)

        (first, second) = metrics
        nt.assert_equal(first['metric'], 'test.gauge.1')
        nt.assert_equal(first['points'][0][0], 100.0)
        nt.assert_equal(first['points'][0][1], 22)
        nt.assert_equal(second['metric'], 'test.gauge.2')

        # Flush again and make sure we're progressing.
        reporter.metrics = []
        dog.flush(130.0)
        nt.assert_equal(len(reporter.metrics), 1)

        # Finally, make sure we've flushed all metrics.
        reporter.metrics = []
        dog.flush(150.0)
        nt.assert_equal(len(reporter.metrics), 0)
Exemplo n.º 2
0
 def test_disabled_mode(self):
     dog = DogStatsApi()
     reporter = dog.reporter = MemoryReporter()
     dog.start(disabled=True, flush_interval=1, roll_up_interval=1)
     dog.gauge('testing', 1, timestamp=1000)
     dog.gauge('testing', 2, timestamp=1000)
     dog.flush(2000.0)
     assert not reporter.metrics
Exemplo n.º 3
0
    def test_threadsafe_correctness(self):
        raise SkipTest("Passing on threadsafe for now")
        # A test to ensure we flush the expected values
        # when we have lots of threads writing to dog api
        dog = DogStatsApi()
        dog.start(flush_interval=1, roll_up_interval=1)
        reporter = dog.reporter = MemoryReporter()

        class MetricProducer(threading.Thread):

            def id(self):
                return threading.current_thread().ident

            def run(self):
                print('running %s' % self.id())
                self.gauges = []
                self.count = 0
                end_time = time.time() + random.randint(0, 5)
                while time.time() < end_time:
                    m = 'gauge.%s.%s' % (time.time(), self.id())
                    self.gauges.append(m)
                    dog.gauge(m, 1)
                    # Also, increment a counter and ensure it works ok.
                    dog.increment('metric.count')
                    self.count += 1
                    time.sleep(0.01)

        # Start writing to dog api in a bunch of threads.
        num_threads = 10
        threads = [MetricProducer() for i in range(num_threads)]
        [t.start() for t in threads]
        # Also write a few metrics in the main thread.
        expected_gauges = ['gauge.%s' % i for i in range(100)]
        for g in expected_gauges:
            dog.gauge(g, 1)
        print('waiting for threads to finish')
        [t.join() for t in threads]

        # Wait for the flush/ roll up to complete.
        time.sleep(3)

        metrics = reporter.metrics
        metric_names = sorted((m['metric'] for m in metrics))

        #
        # Make sure we have the correct number of gauges.
        #
        for t in threads:
            expected_gauges += t.gauges
        expected_gauges.sort()
        gauges = [m for m in metric_names if 'gauge' in m]
        nt.assert_equal(gauges, expected_gauges)

        # assert the count is correct
        expected_count = sum((t.count for t in threads))
        actual_count = (sum((m['points'][0][1] for m in metrics if
                                    m['metric'] == 'metric.count')))
        nt.assert_equal(actual_count, expected_count)
Exemplo n.º 4
0
 def test_custom_host_and_device(self):
     dog = DogStatsApi()
     dog.start(roll_up_interval=1, flush_in_thread=False, host='host', device='dev')
     reporter = dog.reporter = MemoryReporter()
     dog.gauge('my.gauge', 1, 100.0)
     dog.flush(1000)
     metric = reporter.metrics[0]
     nt.assert_equal(metric['device'], 'dev')
     nt.assert_equal(metric['host'], 'host')
Exemplo n.º 5
0
 def test_default_host_and_device(self):
     dog = DogStatsApi()
     dog.start(roll_up_interval=1, flush_in_thread=False)
     reporter = dog.reporter = MemoryReporter()
     dog.gauge('my.gauge', 1, 100.0)
     dog.flush(1000)
     metric = reporter.metrics[0]
     assert not metric['device']
     assert metric['host']
Exemplo n.º 6
0
    def test_flushing_in_thread(self):
        dog = DogStatsApi()
        dog.start(roll_up_interval=1,
                  flush_interval=1,
                  flush_in_greenlet=True,
                  api_key=API_KEY)

        now = time.time()
        dog.gauge('test.dogapi.greenlet.gauge.%s' % now, 3)
        dog.increment('test.dogapi.greenlet.counter.%s' % now)
        dog.increment('test.dogapi.greenlet.counter.%s' % now)
        dog.histogram('test.dogapi.greenlet.histogram.%s' % now, 20)
        dog.histogram('test.dogapi.greenlet.histogram.%s' % now, 30)
        time.sleep(3)
        assert 1 <= dog.flush_count <= 5
Exemplo n.º 7
0
    def test_flushing_in_thread(self):
        dog = DogStatsApi()
        dog.start(roll_up_interval=1,
                  flush_interval=1,
                  flush_in_greenlet=True,
                  api_key=API_KEY)

        now = time.time()
        dog.gauge('test.dogapi.greenlet.gauge.%s' % now , 3)
        dog.increment('test.dogapi.greenlet.counter.%s' % now)
        dog.increment('test.dogapi.greenlet.counter.%s' % now)
        dog.histogram('test.dogapi.greenlet.histogram.%s' % now, 20)
        dog.histogram('test.dogapi.greenlet.histogram.%s' % now, 30)
        time.sleep(3)
        assert 1 <= dog.flush_count <= 5
Exemplo n.º 8
0
def measure_thousands_of_metrics():
    dog = DogStatsApi()
    dog.start(api_key='apikey_3', api_host="https://app.datad0g.com")
    yappi.start()
    @dog.timed('timed')
    def timed():
        pass
    for i in range(100):
        for j in range(1000):
            name = j % 100
            dog.gauge('gauge.%s' % name, j)
            dog.increment('counter.%s' % name, j)
            dog.histogram('histogram.%s' % name, j)
            timed()
        print('run %s' % i)
    yappi.print_stats(sort_type=yappi.SORTTYPE_TSUB, sort_order=yappi.SORTORDER_DESC)
Exemplo n.º 9
0
def measure_thousands_of_metrics():
    dog = DogStatsApi()
    dog.start(api_key='apikey_3', api_host="https://app.datad0g.com")
    yappi.start()

    @dog.timed('timed')
    def timed():
        pass

    for i in range(100):
        for j in range(1000):
            name = j % 100
            dog.gauge('gauge.%s' % name, j)
            dog.increment('counter.%s' % name, j)
            dog.histogram('histogram.%s' % name, j)
            timed()
        print('run %s' % i)
    yappi.print_stats(sort_type=yappi.SORTTYPE_TSUB,
                      sort_order=yappi.SORTORDER_DESC)
Exemplo n.º 10
0
    def test_host(self):
        dog = DogStatsApi()
        dog.start(roll_up_interval=10, flush_in_thread=False, host='default')
        reporter = dog.reporter = MemoryReporter()

        # Post the same metric with different tags.
        dog.gauge('gauge', 12, timestamp=100.0, host='') # unset the host
        dog.gauge('gauge', 10, timestamp=100.0)
        dog.gauge('gauge', 15, timestamp=100.0, host='test')
        dog.gauge('gauge', 15, timestamp=100.0, host='test')

        dog.increment('counter', timestamp=100.0)
        dog.increment('counter', timestamp=100.0)
        dog.increment('counter', timestamp=100.0, host='test')
        dog.increment('counter', timestamp=100.0, host='test', tags=['tag'])
        dog.increment('counter', timestamp=100.0, host='test', tags=['tag'])

        dog.flush(200.0)

        metrics = self.sort_metrics(reporter.metrics)
        nt.assert_equal(len(metrics), 6)

        [c1, c2, c3, g1, g2, g3] = metrics
        (nt.assert_equal(c['metric'], 'counter') for c in [c1, c2, c3])
        nt.assert_equal(c1['host'], 'default')
        nt.assert_equal(c1['tags'], None)
        nt.assert_equal(c1['points'][0][1], 2)
        nt.assert_equal(c2['host'], 'test')
        nt.assert_equal(c2['tags'], None)
        nt.assert_equal(c2['points'][0][1], 1)
        nt.assert_equal(c3['host'], 'test')
        nt.assert_equal(c3['tags'], ['tag'])
        nt.assert_equal(c3['points'][0][1], 2)

        (nt.assert_equal(g['metric'], 'gauge')   for g in [g1, g2, g3])
        nt.assert_equal(g1['host'], '')
        nt.assert_equal(g1['points'][0][1], 12)
        nt.assert_equal(g2['host'], 'default')
        nt.assert_equal(g2['points'][0][1], 10)
        nt.assert_equal(g3['host'], 'test')
        nt.assert_equal(g3['points'][0][1], 15)


        # Ensure histograms work as well.
        @dog.timed('timed', host='test')
        def test():
            pass
        test()
        dog.histogram('timed', 20, timestamp=300.0, host='test')
        reporter.metrics = []
        dog.flush(400)
        for metric in reporter.metrics:
            assert metric['host'] == 'test'
Exemplo n.º 11
0
    def test_tags(self):
        dog = DogStatsApi()
        dog.start(roll_up_interval=10, flush_in_thread=False)
        reporter = dog.reporter = MemoryReporter()

        # Post the same metric with different tags.
        dog.gauge('gauge', 10, timestamp=100.0)
        dog.gauge('gauge', 15, timestamp=100.0, tags=['env:production', 'db'])
        dog.gauge('gauge', 20, timestamp=100.0, tags=['env:staging'])

        dog.increment('counter', timestamp=100.0)
        dog.increment('counter',
                      timestamp=100.0,
                      tags=['env:production', 'db'])
        dog.increment('counter', timestamp=100.0, tags=['env:staging'])

        dog.flush(200.0)

        metrics = self.sort_metrics(reporter.metrics)
        nt.assert_equal(len(metrics), 6)

        [c1, c2, c3, g1, g2, g3] = metrics
        (nt.assert_equal(c['metric'], 'counter') for c in [c1, c2, c3])
        nt.assert_equal(c1['tags'], None)
        nt.assert_equal(c1['points'][0][1], 1)
        nt.assert_equal(c2['tags'], ['env:production', 'db'])
        nt.assert_equal(c2['points'][0][1], 1)
        nt.assert_equal(c3['tags'], ['env:staging'])
        nt.assert_equal(c3['points'][0][1], 1)

        (nt.assert_equal(c['metric'], 'gauge') for c in [g1, g2, g3])
        nt.assert_equal(g1['tags'], None)
        nt.assert_equal(g1['points'][0][1], 10)
        nt.assert_equal(g2['tags'], ['env:production', 'db'])
        nt.assert_equal(g2['points'][0][1], 15)
        nt.assert_equal(g3['tags'], ['env:staging'])
        nt.assert_equal(g3['points'][0][1], 20)

        # Ensure histograms work as well.
        @dog.timed('timed', tags=['version:1'])
        def test():
            pass

        test()
        dog.histogram('timed', 20, timestamp=300.0, tags=['db', 'version:2'])
        reporter.metrics = []
        dog.flush(400)
        for metric in reporter.metrics:
            assert metric['tags']  # this is enough
Exemplo n.º 12
0
 def test_stop(self):
     dog = DogStatsApi()
     reporter = dog.reporter = MemoryReporter()
     dog.start(flush_interval=1, roll_up_interval=1)
     for i in range(10):
         dog.gauge('metric', i)
     time.sleep(2)
     flush_count = dog.flush_count
     assert flush_count
     dog.stop()
     for i in range(10):
         dog.gauge('metric', i)
     time.sleep(2)
     for i in range(10):
         dog.gauge('metric', i)
     time.sleep(2)
     assert dog.flush_count in [flush_count, flush_count+1]
Exemplo n.º 13
0
    def test_tags(self):
        dog = DogStatsApi()
        dog.start(roll_up_interval=10, flush_in_thread=False)
        reporter = dog.reporter = MemoryReporter()

        # Post the same metric with different tags.
        dog.gauge('gauge', 10, timestamp=100.0)
        dog.gauge('gauge', 15, timestamp=100.0, tags=['env:production', 'db'])
        dog.gauge('gauge', 20, timestamp=100.0, tags=['env:staging'])

        dog.increment('counter', timestamp=100.0)
        dog.increment('counter', timestamp=100.0, tags=['env:production', 'db'])
        dog.increment('counter', timestamp=100.0, tags=['env:staging'])

        dog.flush(200.0)

        metrics = self.sort_metrics(reporter.metrics)
        nt.assert_equal(len(metrics), 6)

        [c1, c2, c3, g1, g2, g3] = metrics
        (nt.assert_equal(c['metric'], 'counter') for c in [c1, c2, c3])
        nt.assert_equal(c1['tags'], None)
        nt.assert_equal(c1['points'][0][1], 1)
        nt.assert_equal(c2['tags'], ['env:production', 'db'])
        nt.assert_equal(c2['points'][0][1], 1)
        nt.assert_equal(c3['tags'], ['env:staging'])
        nt.assert_equal(c3['points'][0][1], 1)

        (nt.assert_equal(c['metric'], 'gauge')   for c in [g1, g2, g3])
        nt.assert_equal(g1['tags'], None)
        nt.assert_equal(g1['points'][0][1], 10)
        nt.assert_equal(g2['tags'], ['env:production', 'db'])
        nt.assert_equal(g2['points'][0][1], 15)
        nt.assert_equal(g3['tags'], ['env:staging'])
        nt.assert_equal(g3['points'][0][1], 20)

        # Ensure histograms work as well.
        @dog.timed('timed', tags=['version:1'])
        def test():
            pass
        test()
        dog.histogram('timed', 20, timestamp=300.0, tags=['db', 'version:2'])
        reporter.metrics = []
        dog.flush(400)
        for metric in reporter.metrics:
            assert metric['tags'] # this is enough