class MtGraphiteEmitter(IEmitter): def get_emitter_protocol(self): return 'mtgraphite' def init(self, url, timeout=1, max_retries=5, emit_format='graphite'): self.url = url self.timeout = timeout self.max_retries = max_retries self.emit_per_line = True if emit_format != 'graphite': raise EmitterUnsupportedFormat('Not supported: %s' % emit_format) self.formatter = write_in_graphite_format self.mtgraphite_client = MTGraphiteClient(self.url) def emit(self, frame, compress=False, metadata={}, snapshot_num=0): """ :param compress: :param metadata: :param snapshot_num: :return: """ iostream = self.format(frame) if self.emit_per_line: iostream.seek(0) num = self.mtgraphite_client.send_messages(iostream.readlines()) else: num = self.mtgraphite_client.send_messages([iostream.getvalue()]) logger.debug('Pushed %d messages to mtgraphite queue' % num)
class MtGraphiteEmitter(IEmitter): def get_emitter_protocol(self): return 'mtgraphite' def init(self, url, timeout=1, max_retries=5, emit_format='graphite'): self.url = url self.timeout = timeout self.max_retries = max_retries self.emit_per_line = True if emit_format != 'graphite': raise EmitterUnsupportedFormat('Not supported: %s' % emit_format) self.formatter = write_in_graphite_format self.mtgraphite_client = MTGraphiteClient(self.url) def emit(self, frame, compress=False, metadata={}, snapshot_num=0, **kwargs): """ :param compress: :param metadata: :param snapshot_num: :return: """ iostream = self.format(frame) if self.emit_per_line: iostream.seek(0) num = self.mtgraphite_client.send_messages(iostream.readlines()) else: num = self.mtgraphite_client.send_messages([iostream.getvalue()]) logger.debug('Pushed %d messages to mtgraphite queue' % num)
def init(self, url, timeout=1, max_retries=5, emit_format='graphite'): self.url = url self.timeout = timeout self.max_retries = max_retries self.emit_per_line = True if emit_format != 'graphite': raise EmitterUnsupportedFormat('Not supported: %s' % emit_format) self.formatter = write_in_graphite_format self.mtgraphite_client = MTGraphiteClient(self.url)
def test_send_bad_password(self, *args): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123/crawler:password', batch_send_every_t=1000, batch_send_every_n=3) assert mt.next_timeout == 2000 m1 = mt.construct_message('space', 'group', 'cpu', 100, 1) m2 = mt.construct_message('space', 'group', 'cpu', 100, 2) m3 = mt.construct_message('space', 'group', 'cpu', 100, 3) with self.assertRaises(MTGraphiteInvalidTenant): mt.send_messages([m1, m2, m3]) assert mt.msgset == [m1, m2, m3]
class MtGraphiteEmitter(BaseEmitter): def __init__(self, url, timeout=1, max_retries=5, emit_per_line=True): BaseEmitter.__init__(self, url, timeout=timeout, max_retries=max_retries, emit_per_line=emit_per_line) self.mtgraphite_client = MTGraphiteClient(self.url) def emit(self, iostream, compress=False, metadata={}, snapshot_num=0): """ :param iostream: a CStringIO used to buffer the formatted features. :param compress: :param metadata: :param snapshot_num: :return: """ if self.emit_per_line: iostream.seek(0) num = self.mtgraphite_client.send_messages(iostream.readlines()) else: num = self.mtgraphite_client.send_messages([iostream.getvalue()]) logger.debug("Pushed %d messages to mtgraphite queue" % num)
def test_init(self, *args): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123/crawler:password', batch_send_every_t=1, batch_send_every_n=10) assert not mt.conn assert not mt.socket assert mt.next_timeout == 1001 assert mt.host == '2.2.2.2' assert mt.port == '123' assert mt.tenant == 'crawler' assert mt.password == 'password' args[0].assert_called()
def test_send(self, *args): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123/crawler:password', batch_send_every_t=1000, batch_send_every_n=3) assert mt.next_timeout == 2000 with self.assertRaises(TypeError): mt.send_messages(1) m1 = mt.construct_message('space', 'group', 'cpu', 100, 1) m2 = mt.construct_message('space', 'group', 'cpu', 100, 2) with self.assertRaises(TypeError): mt.send_messages(m1) # we will not send anything yet as send_every_n is 3 mt.send_messages([m1, m2]) assert mt.msgset == [m1, m2] # now we should send something m3 = mt.construct_message('space', 'group', 'cpu', 100, 3) mt.send_messages([m3]) assert mt.msgset == [] mt.close() assert mt.conn is None
def __init__(self, url, timeout=1, max_retries=5, emit_per_line=True): BaseEmitter.__init__(self, url, timeout=timeout, max_retries=max_retries, emit_per_line=emit_per_line) self.mtgraphite_client = MTGraphiteClient(self.url)
def test_init_bad_urls(self, *args): with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123/crawler') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123/:password') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123/') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2:123') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2/crawler') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://2.2.2.2/crawler:password') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://:234/crawler:password') with self.assertRaises(ValueError): mt = MTGraphiteClient('mtgraphite://') with self.assertRaises(ValueError): mt = MTGraphiteClient('http://1.2.3.4:234/crawler:password') with self.assertRaises(ValueError): mt = MTGraphiteClient('host.com:234/crawler:password') with self.assertRaises(ValueError): mt = MTGraphiteClient('host') mt = MTGraphiteClient('mtgraphite://host.com:234/crawler:password') assert mt