async def main(): async with aiodogstatsd.Client(host="0.0.0.0", port=9125, constant_tags={"whoami": "I am Batman!"}) as client: for _ in range(5000): client.timing("fire", value=random())
def starlette_application(unused_udp_port): async def handler_hello(request): return JSONResponse({"hello": "aiodogstatsd"}) async def handler_hello_variable(request): return JSONResponse({"hello": request.path_params["name"]}) async def handler_bad_request(request): return JSONResponse({"hello": "bad"}, status_code=HTTPStatus.BAD_REQUEST) async def handler_internal_server_error(request): raise NotImplementedError() async def handler_unauthorized(request): raise HTTPException(HTTPStatus.UNAUTHORIZED) client = aiodogstatsd.Client(host="0.0.0.0", port=unused_udp_port, constant_tags={"whoami": "batman"}) return Starlette( debug=True, routes=[ Route("/hello", handler_hello), Route("/hello/{name}", handler_hello_variable), Route("/bad_request", handler_bad_request, methods=["POST"]), Route("/internal_server_error", handler_internal_server_error), Route("/unauthorized", handler_unauthorized), ], middleware=[Middleware(StatsDMiddleware, client=client)], on_startup=[client.connect], on_shutdown=[client.close], )
async def statsd_client(unused_udp_port): client = aiodogstatsd.Client(host="0.0.0.0", port=unused_udp_port, constant_tags={"whoami": "batman"}) await client.connect() yield client await client.close()
async def test_skip_if_closing(self, mocker): statsd_client = aiodogstatsd.Client() await statsd_client.connect() await statsd_client.close() mocked_queue = mocker.patch.object(statsd_client, "_pending_queue") statsd_client.increment("test_closing") mocked_queue.assert_not_called()
async def main(): client = aiodogstatsd.Client( host="0.0.0.0", port=9125, constant_tags={"whoami": "I am Batman!"} ) await client.connect() for _ in range(5000): await client.timeit(do_something(), "task_finished") await client.close()
async def main(): client = aiodogstatsd.Client( host="0.0.0.0", port=9125, constant_tags={"whoami": "I am Batman!"} ) await client.connect() for _ in range(5000): client.timing("fire", value=random()) await client.close()
async def main(): client = aiodogstatsd.Client(host="0.0.0.0", port=9125, constant_tags={"whoami": "I am Batman!"}) await client.connect() # Use threshold_ms for setting a threshold for sending the timing metric. with client.timeit("fire"): # Do action we want to time pass await client.close()
async def test_context_manager(self, unused_udp_port, statsd_server, wait_for): udp_server, collected = statsd_server async with aiodogstatsd.Client(host="0.0.0.0", port=unused_udp_port, constant_tags={"whoami": "batman" }) as statsd_client: async with udp_server: statsd_client.gauge("test_gauge", value=42, tags={"and": "robin"}) await wait_for(collected) assert collected == [b"test_gauge:42|g|#whoami:batman,and:robin"]
async def test_message_send_on_close(self, mocker): statsd_client = aiodogstatsd.Client() await statsd_client.connect() mocked_queue = mocker.patch.object(statsd_client, "_pending_queue") mocked_queue.empty = mocker.Mock() mocked_queue.empty.side_effect = [0, 1] mocked_queue.get = mocker.Mock() mocked_queue.get.side_effect = asyncio.Future await asyncio.sleep(0) mocked_queue.get.assert_called_once() await statsd_client.close() assert mocked_queue.get.call_count == 2 assert mocked_queue.empty.call_count == 2
async def get_connection(cls) -> 'StatsdConnection': """ Acquires a connection to the database from the pool. Returns: StatsdConnection: The connection that was aquired from the pool. """ config = cls.config.copy() if not config.get("constant_tags", {}).get("service"): cls.logger.debug("Creating fake Statsd connection") conn = _FakeStatsdConnection() else: cls.logger.debug("Creating real Statsd connection") conn = aiodogstatsd.Client(**config) await conn.connect() return cls(conn)
def get_application() -> Starlette: client = aiodogstatsd.Client(host="0.0.0.0", port=9125, constant_tags={"whoami": "I am Batman!"}) app = Starlette( debug=True, routes=[ Route("/hello", handler_hello), Route("/bad_request", handler_bad_request), Route("/internal_server_error", handler_internal_server_error), Route("/unauthorized", handler_unauthorized), ], middleware=[Middleware(StatsDMiddleware, client=client)], on_startup=[client.connect], on_shutdown=[client.close], ) return app
async def statsd_client(app): app["statsd"] = aiodogstatsd.Client(host="0.0.0.0", port=9125) await app["statsd"].connect() yield await app["statsd"].close()