Exemplo n.º 1
0
    def test_context_manager(self):
        fake_socket = FakeSocket()
        with mstatsd.Connection() as conn:
            conn.socket = fake_socket
            client = mstatsd.Client(name='ContextTester', connection=conn)
            client.get_gauge('page').send('views', 123)
            client.get_timer('page').timing('timer', 12)

        self.assertEqual(
            'ContextTester.page.views:123|g\nContextTester.page.timer:12|ms',
            fake_socket.recv())
Exemplo n.º 2
0
    def test_batched_buffer_autoflush(self):
        fake_socket = FakeSocket()
        with mstatsd.Connection() as conn:
            conn.socket = fake_socket
            client = mstatsd.Client(name='BufferedTester', connection=conn)
            counter = client.get_counter('mycounter')
            for _ in range(51):
                counter.increment()
            self.assertEqual(
                '\n'.join(['BufferedTester.mycounter:1|c' for _ in range(50)]),
                fake_socket.recv())

        self.assertEqual('BufferedTester.mycounter:1|c', fake_socket.recv())
Exemplo n.º 3
0
def get_client(dimensions=None):
    # type: (object) -> object
    """Creates statsd client

    Creates monasca-statsd client using configuration from
    config file and supplied dimensions.

    Configuration is composed out of ::

        [monitoring]
        statsd_host = 192.168.10.4
        statsd_port = 8125
        statsd_buffer = 50

    Dimensions are appended to following dictionary ::

        {
            'service': 'monitoring',
            'component': 'monasca-api'
        }

    Note:
        Passed dimensions do not override those specified in
        dictionary above

    :param dict dimensions: Optional dimensions
    :return: statsd client
    :rtype: monascastatsd.Client
    """
    dims = _DEFAULT_DIMENSIONS.copy()
    if dimensions:
        for key, val in dimensions.items():
            if key not in _DEFAULT_DIMENSIONS:
                dims[key] = val
            else:
                LOG.warn('Cannot override fixed dimension %s=%s', key,
                         _DEFAULT_DIMENSIONS[key])

    connection = monascastatsd.Connection(
        host=CONF.monitoring.statsd_host,
        port=CONF.monitoring.statsd_port,
        max_buffer_size=CONF.monitoring.statsd_buffer
    )
    client = monascastatsd.Client(name=_CLIENT_NAME,
                                  connection=connection,
                                  dimensions=dims)

    LOG.debug('Created statsd client %s[%s] = %s:%d', _CLIENT_NAME, dims,
              CONF.monitoring.statsd_host, CONF.monitoring.statsd_port)

    return client
Exemplo n.º 4
0
    def send_messages(self):
        '''Main processing for sending messages.'''
        try:
            conn = mstatsd.Connection(host=self.host, port=self.port)
            self.client = mstatsd.Client(name='statsd-generator', connection=conn)
            for index in range(1, self.num_of_iterations + 1):
                print("Starting iteration " + str(index) +
                      " of " + str(self.num_of_iterations))
                counter = self.client.get_counter('teraflops')
                counter.increment(5)
                gauge = self.client.get_gauge()
                gauge.send('num_of_teraflops',
                           random.uniform(1.0, 10.0),
                           dimensions={'origin': 'dev',
                                       'environment': 'test'})
                histogram = self.client.get_histogram('hist')
                histogram.send('file.upload.size',
                               random.randrange(1, 100),
                               dimensions={'version': '1.0'})
                set = self.client.get_set('hist')
                set.send('load_time',
                         random.randrange(1, 100),
                         dimensions={'page_name': 'mypage.html'})

                timer = self.client.get_timer('timer')

                @timer.timed('config_db_time',
                             dimensions={'db_name': 'mydb'})
                def time_db():
                    time.sleep(0.2)
                time_db()

                with timer.time('time_block'):
                    time.sleep(0.3)

                # Send some regular statsd messages
                counter = statsd.Counter('statsd_counter')
                counter += 1
                gauge = statsd.Gauge('statsd_gauge')
                gauge.send('cpu_percent',
                           random.uniform(1.0, 100.0))
                print("Completed iteration " + str(index) +
                      ".  Sleeping for " + str(self.delay) + " seconds...")
                time.sleep(self.delay)
        except Exception:
            print ("Error sending statsd messages...")
            raise
Exemplo n.º 5
0
 def setUp(self):
     conn = mstatsd.Connection()
     conn.socket = FakeSocket()
     self.client = mstatsd.Client(connection=conn,
                                  dimensions={'env': 'test'})