{ "info": { "author": "Mateu C\u00e0naves", "author_email": "mcanaves@hundredrooms.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Web Environment", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Internet :: WWW/HTTP" ], "description": "# hr-prometheus\n\n[![Build Status](https://travis-ci.com/HundredRooms/hr-prometheus.svg?branch=master)](https://travis-ci.com/HundredRooms/hr-prometheus)\n[![codecov](https://codecov.io/gh/HundredRooms/hr-prometheus/branch/master/graph/badge.svg)](https://codecov.io/gh/HundredRooms/hr-prometheus)\n[![PyPI version](https://badge.fury.io/py/hr-prometheus.svg)](https://badge.fury.io/py/hr-prometheus)\n\nPrometheus integration for aiohttp projects.\n\nhr-prometheus adds support for providing [aiohttp](https://aiohttp.readthedocs.io/en/stable/) applications metrics to [prometheus](https://prometheus.io/docs/introduction/overview/). It is implemented as a aiohttp middleware.\n\nCurrently, it exports the following metrics via the /metrics endpoint by default:\n\n- request_latency: Elapsed time per request in seconds.\n - Labels exported: method (HTTP method), path\n- request_count: Number of requests received.\n - Labels exported: method (HTTP method), path, status (HTTP status)\n- requests_in_progress: In progress requests.\n - Labels exported: method (HTTP method), path\n\nDefault request behaviour can be modified by passing a custom `RequestMonitor` to the middleware. You can find out how to do it in [advanced section](#advance-usage).\n\n## Installation\n\n```shell\npip install hr-prometheus\n```\n\n## Usage\n\nBriefly, the following is all you need to do to measure and export prometheus metrics from your aiohttp web application:\n\n```python\nfrom aiohttp import web\nfrom hr_prometheus import hrprometheus_middleware, hrprometheus_view\n\napp = web.Application()\napp.router.add_get(\"/metrics\", hrprometheus_view)\napp.middlewares.append(hrprometheus_middleware())\n```\n\n### Advanced usage\n\n#### Custom monitors\n\nTo modify the default behavior you simply need to create a new monitor that inherits from the `BaseRequestMonitor` and pass the class to the middleware.\n\nThis class provides two public methods. `update_init_metrics` and `update_end_metrics`.\nThese methods are executed at the beginning and end of a request respectively. Simply add the metrics you want at each point.\n\nHere's an example taken from the default monitor.\n\n```python\nfrom aiohttp import web\nfrom hr_prometheus import hrprometheus_middleware, hrprometheus_view\nfrom hr_prometheus.monitors import BaseRequestMonitor\n\n\nclass RequestMonitor(BaseRequestMonitor):\n REQUEST_COUNT = Counter(\n \"request_count\", \"Number of requests received\", [\"method\", \"path\", \"status\"]\n )\n REQUEST_LATENCY = Histogram(\n \"request_latency\", \"Elapsed time per request\", [\"method\", \"path\"]\n )\n REQUEST_IN_PROGRESS = Gauge(\n \"requests_in_progress\", \"Requests in progress\", [\"method\", \"path\"]\n )\n\n def update_init_metrics(self):\n self.REQUEST_IN_PROGRESS.labels(*self.request_description).inc()\n\n def update_end_metrics(self):\n resp_time = time.time() - self.init_time\n self.REQUEST_COUNT.labels(*self.request_description, self.response_status).inc()\n self.REQUEST_LATENCY.labels(*self.request_description).observe(resp_time)\n self.REQUEST_IN_PROGRESS.labels(*self.request_description).dec()\n\n\napp = web.Application()\napp.router.add_get(\"/metrics\", hrprometheus_view)\napp.middlewares.append(hrprometheus_middleware(RequestMonitor))\n```\n\n#### Grouping dynamic routes\nIn aiohttp you may define dynamic routes by parametrizing the route path (e.g. `/v1/resource/{resource_id}`). If you are interested in grouping the different values for a given parameter under the same metrics you can do so by specifying the fixed parameters for a named route to the request monitor (you can do so trhough the middleware for convinience).\n\nHere is an example of an api returning neighbouring cells from a matrix\n```python\nfrom aiohttp import web\nfrom hr_prometheus import hrprometheus_middleware\nfrom my_project.views import get_cell_neighbour_view\n\nmiddleware = hrprometheus_middleware(fixed_routes_parameter={\"get_cell_neighbour\": [\"cell_id\"]})\napp = web.Application(middlewares=[middleware])\napp.add_route(\"GET\", \"/cell_neighbour/{cell_id}/direction/{direction}\", get_cell_neighbour_view, name=\"get_cell_neighbour\")\n```\nThis way requests with path `/cell_neighbour/1948/direction/north` and `/cell_neighbour/874/direction/north` are both collapsed into `\"/cell_neighbour/{cell_id}/direction/north\"`\n\nThis is especially useful when you have a wide range of possible values for a path parameter and you are only interested in the overall monitoring, thus avoiding metrics namespace pollution.", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/hundredrooms/hr-prometheus", "keywords": "", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "hr-prometheus", "package_url": "https://pypi.org/project/hr-prometheus/", "platform": "", "project_url": "https://pypi.org/project/hr-prometheus/", "project_urls": { "Homepage": "https://github.com/hundredrooms/hr-prometheus" }, "release_url": "https://pypi.org/project/hr-prometheus/0.2.0/", "requires_dist": null, "requires_python": "", "summary": "Prometheus integration for aiohttp framework.", "version": "0.2.0" }, "last_serial": 4637656, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "d201606596bc99984b2d36b79dcd0fa2", "sha256": "1950924e7295293eeec5fd430af47306890b24717dffe726d26e91b92a2e02d7" }, "downloads": -1, "filename": "hr-prometheus-0.1.0.tar.gz", "has_sig": false, "md5_digest": "d201606596bc99984b2d36b79dcd0fa2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3562, "upload_time": "2018-12-18T13:31:12", "url": "https://files.pythonhosted.org/packages/54/93/a1cb23166fd5a1f5fe06c55027ea5f2cb7f1bc9b19a5552cf3f80aaa9075/hr-prometheus-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "30602924d2100067e18f6ff264469cb2", "sha256": "184af6deccfa94e29a0fd564a5f51ddec722841004b629dab8de0d3484c77c30" }, "downloads": -1, "filename": "hr-prometheus-0.1.1.tar.gz", "has_sig": false, "md5_digest": "30602924d2100067e18f6ff264469cb2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3568, "upload_time": "2018-12-18T14:02:49", "url": "https://files.pythonhosted.org/packages/f0/3f/bdebdb0aa5f16c251f272783ba64b0a6b94a1a1c72450a416776b9cde2fe/hr-prometheus-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "9606a8b0efcc1881baf1eaa88d12af00", "sha256": "877f069cecd64eaba7b241e85c1847e1dc335344f83a32b029b64a6ec717750a" }, "downloads": -1, "filename": "hr-prometheus-0.2.0.tar.gz", "has_sig": false, "md5_digest": "9606a8b0efcc1881baf1eaa88d12af00", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4552, "upload_time": "2018-12-27T11:45:40", "url": "https://files.pythonhosted.org/packages/d8/9b/09316b5388ce4f471517ef94562779fb0b8f9339349c3709bcbda68332a7/hr-prometheus-0.2.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9606a8b0efcc1881baf1eaa88d12af00", "sha256": "877f069cecd64eaba7b241e85c1847e1dc335344f83a32b029b64a6ec717750a" }, "downloads": -1, "filename": "hr-prometheus-0.2.0.tar.gz", "has_sig": false, "md5_digest": "9606a8b0efcc1881baf1eaa88d12af00", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4552, "upload_time": "2018-12-27T11:45:40", "url": "https://files.pythonhosted.org/packages/d8/9b/09316b5388ce4f471517ef94562779fb0b8f9339349c3709bcbda68332a7/hr-prometheus-0.2.0.tar.gz" } ] }