{"id":7515,"library":"prometheus-async","title":"Prometheus Async","description":"prometheus-async provides asynchronous helpers for the `prometheus_client` library, making it easier to expose metrics from `asyncio` and `Twisted` applications. Maintained by Hynek Schlawack, the library is currently at version 26.1.0 and receives regular updates, often focusing on type hinting and internal improvements, with occasional breaking changes for major Python versions.","status":"active","version":"26.1.0","language":"en","source_language":"en","source_url":"https://github.com/hynek/prometheus-async/","tags":["prometheus","async","metrics","monitoring","asyncio","aiohttp","twisted","observability"],"install":[{"cmd":"pip install prometheus-async","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for Prometheus metrics functionality.","package":"prometheus_client","optional":false}],"imports":[{"note":"The `start_http_server` function for asyncio is located in the `web` submodule, not directly under `aio`.","wrong":"from prometheus_async.aio import start_http_server","symbol":"start_http_server","correct":"from prometheus_async.aio.web import start_http_server"},{"note":"Common decorator for timing async functions.","symbol":"time","correct":"from prometheus_async.aio import time"},{"note":"Common decorator for tracking in-progress async operations.","symbol":"track_inprogress","correct":"from prometheus_async.aio import track_inprogress"},{"note":"Generic decorator for monitoring async functions with custom metrics.","symbol":"monitor","correct":"from prometheus_async.aio import monitor"}],"quickstart":{"code":"import asyncio\nfrom prometheus_client import Gauge\nfrom prometheus_async.aio.web import start_http_server\nfrom prometheus_async.aio import time\n\n# Define a metric\nMY_GAUGE = Gauge('my_async_gauge', 'Description of my async gauge')\n\n@time(MY_GAUGE)\nasync def some_async_function():\n    \"\"\"An example async function that does some work.\"\"\"\n    print(\"Running some_async_function...\")\n    MY_GAUGE.set(10)\n    await asyncio.sleep(0.5)\n    MY_GAUGE.set(20)\n    print(\"some_async_function finished.\")\n\nasync def main():\n    # Start the Prometheus HTTP server in the background\n    # It will expose metrics on http://localhost:8000/metrics by default\n    server = await start_http_server(port=8000)\n    print(\"Prometheus metrics server started on port 8000\")\n\n    # Run our async function\n    await some_async_function()\n\n    print(\"Application finished. Metrics server still running. Press Ctrl+C to exit.\")\n    # Keep the event loop running to allow the metrics server to serve requests\n    await asyncio.Future() # This will run forever until cancelled\n\nif __name__ == \"__main__\":\n    try:\n        asyncio.run(main())\n    except KeyboardInterrupt:\n        print(\"\\nExiting.\")\n","lang":"python","description":"This example demonstrates how to start a Prometheus HTTP server using `prometheus_async.aio.web.start_http_server` and how to apply the `prometheus_async.aio.time` decorator to an asynchronous function. The metrics server runs in the background, making metrics available at `/metrics` on the specified port."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or a later version. For older Python versions, use `prometheus-async<22.1.0` (not recommended for new projects).","message":"Dropped support for Python 2.7, 3.5, and 3.6. `prometheus-async` now requires Python 3.9 or newer.","severity":"breaking","affected_versions":">=22.1.0"},{"fix":"Remove the `loop` argument when calling `start_http_server()`. Modern `asyncio` manages the event loop automatically, making explicit `loop` passing generally unnecessary and discouraged.","message":"The `loop` argument has been removed from `prometheus_async.aio.web.start_http_server()`.","severity":"breaking","affected_versions":">=22.1.0"},{"fix":"Verify that your imports (e.g., `prometheus_async.aio.web.start_http_server`) match the asynchronous framework you are using in your application.","message":"Ensure you import from the correct asynchronous backend (`aio` for asyncio/aiohttp or `twisted` for Twisted). Mixing them or using the wrong one will lead to `AttributeError` or unexpected behavior.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Change the import to `from prometheus_async.aio.web import start_http_server`.","cause":"Incorrect import path for `start_http_server` in the `aio` module.","error":"AttributeError: module 'prometheus_async.aio' has no attribute 'start_http_server'"},{"fix":"Remove the `loop=...` argument from your call to `start_http_server()`. The library handles the event loop automatically.","cause":"Attempting to pass the `loop` argument to `start_http_server()` on a version where it has been removed.","error":"TypeError: start_http_server() got an unexpected keyword argument 'loop'"},{"fix":"Run `pip install prometheus-async` to install the library. If already installed, check your virtual environment activation or `PYTHONPATH`.","cause":"`prometheus-async` library is not installed or there's a problem with your Python environment's path.","error":"ModuleNotFoundError: No module named 'prometheus_async.aio'"}]}