Exemplo n.º 1
0
def makeService(host='127.0.0.1', port=2003, sample_rate=1.0, prefix=''):
    metric_collector = MetricCollector()
    metric_collector.start()
    reporting = ReportingService()

    metric_reporter = MetricReporter((host, port), metric_collector, prefix)
    reporting.schedule(metric_reporter.report_metrics, sample_rate)

    return reporting
Exemplo n.º 2
0
def makeService(host='127.0.0.1', port=8125, known_fields=None):
    metric_collector = MetricCollector()
    metric_collector.start()

    root = Stats(metric_collector, known_fields=known_fields)
    site = Site(root)

    def sample_metrics():
        metric_collector.sample()

    s = service.MultiService()
    internet.TCPServer(port, site, interface=host).setServiceParent(s)
    internet.TimerService(1, sample_metrics).setServiceParent(s)
    return s
Exemplo n.º 3
0
class MetricCollectorTest(unittest.TestCase):
    def setUp(self):
        self.c = MetricCollector()

    def test_init(self):
        c = MetricCollector(10)
        self.assertEqual(c.sample_size, 10)
        self.assertEqual(c.current, {})
        self.assertEqual(c.totals, {})
        self.assertEqual(c.samples, {})

        c = MetricCollector(20)
        self.assertEqual(c.sample_size, 20)

    def test_emit(self):
        self.c.emit({'metric': 'some_metric'})
        self.assertEqual(self.c.current['some_metric'], 1)

        self.c.emit({'metric': 'some_metric', 'count': 10})
        self.assertEqual(self.c.current['some_metric'], 11)

    def test_add_metric(self):
        self.c.add_metric('some_metric')
        self.assertEqual(self.c.current['some_metric'], 1)
        self.assertEqual(self.c.totals['some_metric'], 1)

        self.c.add_metric('some_metric', count=10)
        self.assertEqual(self.c.current['some_metric'], 11)
        self.assertEqual(self.c.totals['some_metric'], 11)

    def test_sample(self):
        self.c.add_metric('some_metric')
        self.c.sample()
        self.assertEqual(self.c.samples['some_metric'], [1])

        self.c.add_metric('some_metric')
        self.c.sample()
        self.assertEqual(self.c.samples['some_metric'], [1, 1])

        for i in range(15):
            self.c.add_metric('some_metric', count=i)
            self.c.sample()
        self.assertEqual(self.c.samples['some_metric'], range(4, 15))

    def test_attach_logger(self):
        self.c.start()
        self.assertIn(self.c.emit, log.theLogPublisher.observers)
        self.c.stop()
        self.assertNotIn(self.c.emit, log.theLogPublisher.observers)
Exemplo n.º 4
0
class MetricCollectorTest(unittest.TestCase):
    def setUp(self):
        self.c = MetricCollector()

    def test_init(self):
        c = MetricCollector(10)
        self.assertEqual(c.sample_size, 10)
        self.assertEqual(c.current, {})
        self.assertEqual(c.totals, {})
        self.assertEqual(c.samples, {})

        c = MetricCollector(20)
        self.assertEqual(c.sample_size, 20)

    def test_emit(self):
        self.c.emit({'metric': 'some_metric'})
        self.assertEqual(self.c.current['some_metric'], 1)

        self.c.emit({'metric': 'some_metric', 'count': 10})
        self.assertEqual(self.c.current['some_metric'], 11)

    def test_add_metric(self):
        self.c.add_metric('some_metric')
        self.assertEqual(self.c.current['some_metric'], 1)
        self.assertEqual(self.c.totals['some_metric'], 1)

        self.c.add_metric('some_metric', count=10)
        self.assertEqual(self.c.current['some_metric'], 11)
        self.assertEqual(self.c.totals['some_metric'], 11)

    def test_sample(self):
        self.c.add_metric('some_metric')
        self.c.sample()
        self.assertEqual(self.c.samples['some_metric'], [1])

        self.c.add_metric('some_metric')
        self.c.sample()
        self.assertEqual(self.c.samples['some_metric'], [1, 1])

        for i in range(15):
            self.c.add_metric('some_metric', count=i)
            self.c.sample()
        self.assertEqual(self.c.samples['some_metric'], range(4, 15))

    def test_attach_logger(self):
        self.c.start()
        self.assertIn(self.c.emit, log.theLogPublisher.observers)
        self.c.stop()
        self.assertNotIn(self.c.emit, log.theLogPublisher.observers)
Exemplo n.º 5
0
def makeService(host='127.0.0.1', port=8125, known_fields=None):
    metric_collector = MetricCollector()
    metric_collector.start()

    root = Stats(metric_collector, known_fields=known_fields)
    site = Site(root)

    def sample_metrics():
        metric_collector.sample()

    s = service.MultiService()
    internet.TCPServer(
        port,
        site,
        interface=host).setServiceParent(s)
    internet.TimerService(1, sample_metrics).setServiceParent(s)
    return s
Exemplo n.º 6
0
def makeService(host='127.0.0.1', port=8125, sample_rate=1.0, prefix=''):
    client = TwistedStatsDClient(host, port)
    metrics = Metrics(connection=client, namespace=prefix)
    reporting = ReportingService()
    if "bsd" not in sys.platform:
        for report in PROCESS_STATS:
            reporting.schedule(report, sample_rate, metrics.gauge)

        for report in COUNTER_STATS:
            reporting.schedule(report, sample_rate, metrics.gauge)

    # Attach log observer to collect metrics for us
    metric_collector = MetricCollector()
    metric_collector.start()

    metric_reporter = MetricReporter(metrics, metric_collector)
    reporting.schedule(metric_reporter.report_metrics, sample_rate, None)

    protocol = StatsDClientProtocol(client)
    reactor.listenUDP(0, protocol)
    return reporting
Exemplo n.º 7
0
def makeService(host='127.0.0.1', port=8125, sample_rate=1.0, prefix=''):
    client = TwistedStatsDClient(host, port)
    metrics = Metrics(connection=client, namespace=prefix)
    reporting = ReportingService()

    for report in PROCESS_STATS:
        reporting.schedule(report, sample_rate, metrics.gauge)

    for report in COUNTER_STATS:
        reporting.schedule(report, sample_rate, metrics.gauge)

    # Attach log observer to collect metrics for us
    metric_collector = MetricCollector()
    metric_collector.start()

    metric_reporter = MetricReporter(metrics, metric_collector)
    reporting.schedule(metric_reporter.report_metrics, sample_rate, None)

    protocol = StatsDClientProtocol(client)
    reactor.listenUDP(0, protocol)
    return reporting