def _realise_connection(cls, async):
        """Return a configured statsd client connection."""
        servers = config.statsd.servers
        if servers is None:
            raise LookupError('Unable to obtain the statsd configuration')

        servers = map(None, [server.strip() for server in servers.split(";")])
        if not servers:
            raise LookupError('Unable to obtain the statsd configuration')

        connections = []
        for server in servers:
            statsd_host, statsd_port = server.split(":")
            statsd_port = int(statsd_port)

            if async:
                connection = TwistedStatsDClient.create(statsd_host,
                                                        statsd_port)
                connection.disconnect_callback = \
                    lambda: cls._disconnect_connection(connection)
                protocol = StatsDClientProtocol(connection)

                from twisted.internet import reactor
                reactor.listenUDP(0, protocol)
            else:
                connection = UdpStatsDClient(statsd_host, statsd_port)
                connection.connect()

            connections.append(connection)

        if len(connections) == 1:
            return connections[0]

        return ConsistentHashingClient(connections)
 def __init__(self,
              statsd_host="localhost",
              statsd_port=8125,
              namespace="aplt"):
     self.client = TwistedStatsDClient.create(statsd_host, statsd_port)
     self._metric = Metrics(connection=self.client, namespace=namespace)
     self._stat_protocol = None
Exemple #3
0
    def _realise_connection(cls, async):
        """Return a configured statsd client connection."""
        servers = config.statsd.servers
        if servers is None:
            raise LookupError('Unable to obtain the statsd configuration')

        servers = map(None, [server.strip() for server in servers.split(";")])
        if not servers:
            raise LookupError('Unable to obtain the statsd configuration')

        connections = []
        for server in servers:
            statsd_host, statsd_port = server.split(":")
            statsd_port = int(statsd_port)

            if async:
                connection = TwistedStatsDClient.create(
                    statsd_host, statsd_port)
                connection.disconnect_callback = \
                    lambda: cls._disconnect_connection(connection)
                protocol = StatsDClientProtocol(connection)

                from twisted.internet import reactor
                reactor.listenUDP(0, protocol)
            else:
                connection = UdpStatsDClient(statsd_host, statsd_port)
                connection.connect()

            connections.append(connection)

        if len(connections) == 1:
            return connections[0]

        return ConsistentHashingClient(connections)
Exemple #4
0
    def test_twistedstatsd_with_malformed_address_and_errback(self):
        exceptions_captured = []

        def capture_exception_raised(failure):
            exception = failure.getErrorMessage()
            self.assertTrue(exception.startswith("DNS lookup failed"))
            exceptions_captured.append(exception)

        self.client = TwistedStatsDClient.create("256.0.0.0", 1, resolver_errback=capture_exception_raised)
        self.build_protocol()
        yield self.client.resolve_later

        self.assertEqual(len(exceptions_captured), 1)
    def test_twistedstatsd_with_malformed_address_and_errback(self):
        exceptions_captured = []

        def capture_exception_raised(failure):
            exception = failure.getErrorMessage()
            self.assertTrue(exception.startswith("DNS lookup failed"))
            exceptions_captured.append(exception)

        self.client = TwistedStatsDClient.create(
            '256.0.0.0', 1, resolver_errback=capture_exception_raised)
        self.build_protocol()
        yield self.client.resolve_later

        self.assertEqual(len(exceptions_captured), 1)
Exemple #6
0
    def test_twistedstatsd_write_with_host_resolved(self):
        self.client = TwistedStatsDClient.create("localhost", 8000)
        self.build_protocol()
        yield self.client.resolve_later

        def ensure_bytes_sent(bytes_sent):
            self.assertEqual(bytes_sent, len("message"))
            self.assertEqual(self.client.host, "127.0.0.1")

        def exercise(callback):
            self.client.write("message", callback=callback)

        d = Deferred()
        d.addCallback(ensure_bytes_sent)
        reactor.callWhenRunning(exercise, d.callback)
        yield d
    def test_twistedstatsd_write_with_host_resolved(self):
        self.client = TwistedStatsDClient.create('localhost', 8000)
        self.build_protocol()
        yield self.client.resolve_later

        def ensure_bytes_sent(bytes_sent):
            self.assertEqual(bytes_sent, len('message'))
            self.assertEqual(self.client.host, '127.0.0.1')

        def exercise(callback):
            self.client.write('message', callback=callback)

        d = Deferred()
        d.addCallback(ensure_bytes_sent)
        reactor.callWhenRunning(exercise, d.callback)
        yield d
Exemple #8
0
    def build_target_redirect_udp(self, host, port):
        if self.service is None:
            return lambda *args: True

        port = int(port)
        d = defer.Deferred()
        self.ready.addCallback(lambda _: d)

        client = TwistedStatsDClient.create(
            host, port, connect_callback=lambda: d.callback(None))
        protocol = StatsDClientProtocol(client)

        udp_service = UDPServer(0, protocol)
        udp_service.setServiceParent(self.service)

        def redirect_udp_target(metric_type, key, fields):
            message = self.rebuild_message(metric_type, key, fields)
            client.write(message)
            yield metric_type, key, fields
        return redirect_udp_target
Exemple #9
0
    def build_target_redirect_udp(self, host, port):
        if self.service is None:
            return lambda *args: True

        port = int(port)
        d = defer.Deferred()
        self.ready.addCallback(lambda _: d)

        client = TwistedStatsDClient.create(
            host, port, connect_callback=lambda: d.callback(None))
        protocol = StatsDClientProtocol(client)

        udp_service = UDPServer(0, protocol)
        udp_service.setServiceParent(self.service)

        def redirect_udp_target(metric_type, key, fields):
            message = self.rebuild_message(metric_type, key, fields)
            client.write(message)
            yield metric_type, key, fields

        return redirect_udp_target
Exemple #10
0
 def __init__(self, statsd_host="localhost", statsd_port=8125):
     self.client = TwistedStatsDClient.create(statsd_host, statsd_port)
     self._metric = Metrics(connection=self.client, namespace="autopush")