def test_batching_1(self): MTU = 24 collector = MetricCollector(MTU) for i in range(8): met_name = "m%i" % i collector.push_gauge(met_name, 100 + i) result = list(collector.flush()) self.assertEqual([ b"m0:100|g\nm1:101|g", b"m2:102|g\nm3:103|g", b"m4:104|g\nm5:105|g", b"m6:106|g\nm7:107|g" ], result)
def test_batching_1(self): MTU = 512 collector = MetricCollector(MTU) for i in range(8): met_name = "m%i" % i collector.push_gauge(met_name, 100 + i) result = list(collector.flush()) print('RESULT', result) self.assertEqual([ b"m0:100.000000|g\nm1:101.000000|g\n" b"m2:102.000000|g\nm3:103.000000|g\n" b"m4:104.000000|g\nm5:105.000000|g\n" b"m6:106.000000|g\nm7:107.000000|g" ], result)
class LowLevelStatsdClient(object): def __init__(self, host, port, packet_size=512, flush_interval=0.5): self.host = host self.port = port self.collector = MetricCollector(packet_size) self._udp_client = None self._running = False self._done = asyncio.Future() self.flush_interval = flush_interval def send_timer(self, name, value, rate): self.collector.push_timer(name, value, rate) def send_gauge(self, name, value, rate): self.collector.push_gauge(name, value, rate) def send_counter(self, name, value, rate): self.collector.push_counter(name, value, rate) async def run(self): self._udp_client = UDPClient(self.host, self.port) work = [self._udp_client.run()] self._running = True async def ticker(): while self._running: await asyncio.sleep(self.flush_interval) messages = self.collector.flush() for msg in messages: self._udp_client.send_nowait(msg) work.append(ticker()) await asyncio.gather(*work) self._done.set_result(True) async def stop(self): self._running = False await self._udp_client.stop() await self._done
def encode_gauge(name, val, rate=1.0, delta=False): collector = MetricCollector() collector.push_gauge(name, val, rate, delta) result = list(collector.flush()) assert len(result) == 1 return result[0]