Ejemplo n.º 1
0
    def setUp(self):
        self.receiver = MetricDatagramReceiver()
        self.receiver.peerName = 'localhost'

        self.addr = ('127.0.0.1', 9999)

        self._receiver_mock = Mock()
        self._receiver_patch = patch.object(MetricReceiver, 'metricReceived',
                                            self._receiver_mock)
        self._receiver_patch.start()
Ejemplo n.º 2
0
class TestMetricDatagramReceiver(TestCase):
    def setUp(self):
        self.receiver = MetricDatagramReceiver()
        self.receiver.peerName = 'localhost'

        self.addr = ('127.0.0.1', 9999)

        self._receiver_mock = Mock()
        self._receiver_patch = patch.object(MetricReceiver, 'metricReceived',
                                            self._receiver_mock)
        self._receiver_patch.start()

    def tearDown(self):
        self._receiver_patch.stop()

    def test_valid_datagramReceived(self):
        """ metricReceived should be called with valid metric """
        metric = b'carbon.foo 1 2'
        self.receiver.datagramReceived(metric, self.addr)
        MetricReceiver.metricReceived.assert_called_once_with(
            'carbon.foo', (2, 1))

    def test_invalid_datagramReceived(self):
        """ metricReceived should not be called with missing timestamp """
        metric = b'carbon.foo 1'
        self.receiver.datagramReceived(metric, self.addr)
        metric = b'c' * 401
        self.receiver.datagramReceived(metric, self.addr)
        MetricReceiver.metricReceived.assert_not_called()

    def test_utf8_datagramReceived(self):
        """ metricReceived should be called with UTF-8 metricname """
        metric = b'\xd0\xb6\xd0\xbe\xd0\xbf\xd0\xb0 42 -1'
        self.receiver.datagramReceived(metric, self.addr)
        MetricReceiver.metricReceived.assert_called_once_with('жопа', (-1, 42))

    def test_multiple_datagramReceived(self):
        """ metricReceived should only be called with valid lines """
        metric = b'lines 1 2\nare 3 4\nnot\nvalid 5 6\n'
        self.receiver.datagramReceived(metric, self.addr)
        MetricReceiver.metricReceived.assert_has_calls([
            call('lines', (2, 1)),
            call('are', (4, 3)),
            call('valid', (6, 5))
        ])
Ejemplo n.º 3
0
def setupReceivers(root_service, settings):
    from carbon.protocols import MetricLineReceiver, MetricPickleReceiver, MetricDatagramReceiver

    for protocol, interface, port in [
        (MetricLineReceiver, settings.LINE_RECEIVER_INTERFACE,
         settings.LINE_RECEIVER_PORT),
        (MetricPickleReceiver, settings.PICKLE_RECEIVER_INTERFACE,
         settings.PICKLE_RECEIVER_PORT)
    ]:
        if port:
            factory = ServerFactory()
            factory.protocol = protocol
            service = TCPServer(port, factory, interface=interface)
            service.setServiceParent(root_service)

    if settings.ENABLE_UDP_LISTENER:
        service = UDPServer(int(settings.UDP_RECEIVER_PORT),
                            MetricDatagramReceiver(),
                            interface=settings.UDP_RECEIVER_INTERFACE)
        service.setServiceParent(root_service)

    if settings.ENABLE_AMQP:
        from carbon import amqp_listener
        amqp_host = settings.AMQP_HOST
        amqp_port = settings.AMQP_PORT
        amqp_user = settings.AMQP_USER
        amqp_password = settings.AMQP_PASSWORD
        amqp_verbose = settings.AMQP_VERBOSE
        amqp_vhost = settings.AMQP_VHOST
        amqp_spec = settings.AMQP_SPEC
        amqp_exchange_name = settings.AMQP_EXCHANGE

        factory = amqp_listener.createAMQPListener(
            amqp_user,
            amqp_password,
            vhost=amqp_vhost,
            spec=amqp_spec,
            exchange_name=amqp_exchange_name,
            verbose=amqp_verbose)
        service = TCPClient(amqp_host, amqp_port, factory)
        service.setServiceParent(root_service)

    if settings.ENABLE_MANHOLE:
        from carbon import manhole

        factory = manhole.createManholeListener()
        service = TCPServer(settings.MANHOLE_PORT,
                            factory,
                            interface=settings.MANHOLE_INTERFACE)
        service.setServiceParent(root_service)
Ejemplo n.º 4
0
def createBaseService(config):
    from carbon.conf import settings

    from carbon.protocols import (MetricLineReceiver, MetricPickleReceiver,
                                  MetricDatagramReceiver)

    root_service = CarbonRootService()
    root_service.setName(settings.program)

    use_amqp = settings.get("ENABLE_AMQP", False)
    if use_amqp:
        from carbon import amqp_listener

        amqp_host = settings.get("AMQP_HOST", "localhost")
        amqp_port = settings.get("AMQP_PORT", 5672)
        amqp_user = settings.get("AMQP_USER", "guest")
        amqp_password = settings.get("AMQP_PASSWORD", "guest")
        amqp_verbose = settings.get("AMQP_VERBOSE", False)
        amqp_vhost = settings.get("AMQP_VHOST", "/")
        amqp_spec = settings.get("AMQP_SPEC", None)
        amqp_exchange_name = settings.get("AMQP_EXCHANGE", "graphite")

    for interface, port, backlog, protocol in (
        (settings.LINE_RECEIVER_INTERFACE, settings.LINE_RECEIVER_PORT,
         settings.LINE_RECEIVER_BACKLOG, MetricLineReceiver),
        (settings.PICKLE_RECEIVER_INTERFACE, settings.PICKLE_RECEIVER_PORT,
         settings.PICKLE_RECEIVER_BACKLOG, MetricPickleReceiver)):
        if port:
            factory = ServerFactory()
            factory.protocol = protocol
            service = TCPServer(int(port),
                                factory,
                                interface=interface,
                                backlog=backlog)
            service.setServiceParent(root_service)

    if settings.ENABLE_UDP_LISTENER:
        service = UDPServer(int(settings.UDP_RECEIVER_PORT),
                            MetricDatagramReceiver(),
                            interface=settings.UDP_RECEIVER_INTERFACE)
        service.setServiceParent(root_service)

    if use_amqp:
        factory = amqp_listener.createAMQPListener(
            amqp_user,
            amqp_password,
            vhost=amqp_vhost,
            spec=amqp_spec,
            exchange_name=amqp_exchange_name,
            verbose=amqp_verbose)
        service = TCPClient(amqp_host, int(amqp_port), factory)
        service.setServiceParent(root_service)

    if settings.ENABLE_MANHOLE:
        from carbon import manhole

        factory = manhole.createManholeListener()
        service = TCPServer(int(settings.MANHOLE_PORT),
                            factory,
                            interface=settings.MANHOLE_INTERFACE)
        service.setServiceParent(root_service)

    if settings.USE_WHITELIST:
        from carbon.regexlist import WhiteList, BlackList
        WhiteList.read_from(settings["whitelist"])
        BlackList.read_from(settings["blacklist"])

    # Instantiate an instrumentation service that will record metrics about
    # this service.
    from carbon.instrumentation import InstrumentationService

    service = InstrumentationService()
    service.setServiceParent(root_service)

    return root_service