def test_sets_client_transport_when_connected(self): """Set the transport as an attribute of the client.""" self.client = TwistedStatsDClient('localhost', 8000) transport = DummyTransport() self.client.connect(transport) self.assertEqual(self.client.transport, transport)
def test_sets_ip_when_host_resolves(self): """As soon as the host is resolved, set the IP as the host.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() self.assertEqual(self.client.host, 'localhost') self.client.host_resolved('127.0.0.1') self.assertEqual(self.client.host, '127.0.0.1')
def test_sets_gateway_transport_when_connected(self): """Set the transport as an attribute of the TransportGateway.""" self.client = TwistedStatsDClient('localhost', 8000) self.client.host_resolved('127.0.0.1') transport = DummyTransport() self.client.connect(transport) self.assertEqual(self.client.transport_gateway.transport, transport)
def test_sets_transport_gateway_when_host_resolves(self): """As soon as the host is resolved, set the transport gateway.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() self.client.transport_gateway = None self.client.host_resolved('127.0.0.1') self.assertIsInstance(self.client.transport_gateway, TransportGateway)
def test_passes_reactor_to_gateway(self): """The client passes the reactor to the gateway as soon as the client is connected.""" self.client = TwistedStatsDClient('127.0.0.1', 8000) self.build_protocol() self.client.host_resolved('127.0.0.1') self.assertEqual(self.client.transport_gateway.reactor, self.client.reactor)
def test_calls_connect_callback_when_host_resolves(self): """As soon as the host is resolved, call back the connect_callback.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() self.client.connect_callback = Mock() self.client.host_resolved('127.0.0.1') self.assertTrue(self.client.connect_callback.called) self.client.connect_callback.assert_called_once_with()
def setup_statsd(config): from txstatsd.client import TwistedStatsDClient, StatsDClientProtocol from txstatsd.metrics.metrics import Metrics global metrics statsd = TwistedStatsDClient(config['statsd']['host'], config['statsd']['port']) metrics = Metrics(connection=statsd, namespace='smap-archiver.' + config['statsd']['prefix']) protocol = StatsDClientProtocol(statsd) reactor.listenUDP(0, protocol)
def test_sends_messages_to_queue_before_host_resolves(self): """Before the host is resolved, send messages to the DataQueue.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() message = 'some data' self.client.data_queue = Mock(spec=DataQueue) callback = Mock() self.client.data_queue.write.return_value = None result = self.client.write(message, callback) self.client.data_queue.write.assert_called_once_with(message, callback) self.assertEqual(result, None)
def test_twistedstatsd_write(self): self.client = TwistedStatsDClient('127.0.0.1', 8000) self.build_protocol() self.client.host_resolved('127.0.0.1') def ensure_bytes_sent(bytes_sent): self.assertEqual(bytes_sent, len('message')) def exercise(callback): self.client.write('message', callback=callback) d = Deferred() d.addCallback(ensure_bytes_sent) reactor.callWhenRunning(exercise, d.callback) return d
def test_sends_messages_to_gateway_after_host_resolves(self): """After the host is resolved, send messages to the TransportGateway.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() self.client.host_resolved('127.0.0.1') message = 'some data' bytes_sent = len(message) self.client.data_queue = Mock(spec=DataQueue) self.client.transport_gateway = Mock(spec=TransportGateway) callback = Mock() self.client.transport_gateway.write.return_value = bytes_sent self.assertEqual(self.client.write(message, callback), bytes_sent) self.client.transport_gateway.write.assert_called_once_with( message, callback)
def test_flushes_queued_messages_to_the_gateway_when_host_resolves(self): """As soon as the host is resolved, flush all messages to the TransportGateway.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() self.client.data_queue.write('data 1', 'callback 1') self.client.data_queue.write('data 2', 'callback 2') self.client.data_queue.write('data 3', 'callback 3') mock_gateway_write = Mock() self.patch(TransportGateway, 'write', mock_gateway_write) self.client.host_resolved('127.0.0.1') self.assertTrue(mock_gateway_write.call_count, 3) expected = [ call('data 1', 'callback 1'), call('data 2', 'callback 2'), call('data 3', 'callback 3') ] self.assertEqual(mock_gateway_write.call_args_list, expected)
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
def __init__(self, statsd_host="localhost", statsd_port=8125): self.client = TwistedStatsDClient(statsd_host, statsd_port) self._metric = Metrics(connection=self.client, namespace="autopush")
def install_stats(host, port, scheme): global metrics statsd_client = TwistedStatsDClient(host, port) metrics = Metrics(connection=statsd_client, namespace=scheme) return StatsDClientProtocol(statsd_client)
def test_starts_with_transport_gateway_if_ip(self): """The client starts without a TransportGateway.""" self.client = TwistedStatsDClient('127.0.0.1', 8000) self.build_protocol() self.assertTrue(self.client.transport_gateway is not None)
def test_starts_without_transport_gateway_if_not_ip(self): """The client starts without a TransportGateway.""" self.client = TwistedStatsDClient('localhost', 8000) self.build_protocol() self.assertTrue(self.client.transport_gateway is None)
def test_starts_with_data_queue(self): """The client starts with a DataQueue.""" self.client = TwistedStatsDClient('127.0.0.1', 8000) self.build_protocol() self.assertIsInstance(self.client.data_queue, DataQueue)