{"id":15374504,"url":"https://github.com/lispython/pyprometheus","last_synced_at":"2025-04-15T12:31:44.853Z","repository":{"id":62582730,"uuid":"83200051","full_name":"Lispython/pyprometheus","owner":"Lispython","description":"Prometheus instrumentation library for Python applications (with UWSGI support)","archived":false,"fork":false,"pushed_at":"2023-08-24T20:09:21.000Z","size":59,"stargazers_count":13,"open_issues_count":8,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-28T21:12:15.787Z","etag":null,"topics":["prometheus","prometheus-client","prometheus-client-library","prometheus-uwsgi","python","python27"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Lispython.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-26T10:21:01.000Z","updated_at":"2022-03-22T17:38:46.000Z","dependencies_parsed_at":"2022-11-03T21:34:21.340Z","dependency_job_id":null,"html_url":"https://github.com/Lispython/pyprometheus","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lispython%2Fpyprometheus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lispython%2Fpyprometheus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lispython%2Fpyprometheus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Lispython%2Fpyprometheus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Lispython","download_url":"https://codeload.github.com/Lispython/pyprometheus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249072279,"owners_count":21208155,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["prometheus","prometheus-client","prometheus-client-library","prometheus-uwsgi","python","python27"],"created_at":"2024-10-01T13:58:56.246Z","updated_at":"2025-04-15T12:31:44.580Z","avatar_url":"https://github.com/Lispython.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Prometheus instrumentation library for Python applications\n============================================================\n\nThe unofficial Python 2 and 3 client for `Prometheus`_.\n\n.. image:: https://travis-ci.org/Lispython/pyprometheus.svg?branch=master\n    :target: https://travis-ci.org/Lispython/pyprometheus\n\n\n\nFeatures\n--------\n\n- Four types of metric are supported: Counter, Gauge, Summary(without quantiles) and Histogram.\n- InMemoryStorage (do not use it for multiprocessing apps)\n- UWSGI storage - share metrics between processes\n- UWAGI flush storage - sync metrics with uwsgi sharedarea by flush call\n- time decorator\n- time context manager\n\n\n\nINSTALLATION\n------------\n\nTo use pyprometheus use pip or easy_install:\n\n:code:`pip install pyprometheus`\n\nor\n\n:code:`easy_install pyprometheus`\n\n\nHOW TO INSTRUMENTING CODE\n-------------------------\n\nGauge\n~~~~~\n\nA gauge is a metric that represents a single numerical value that can arbitrarily go up and down.::\n\n   from pyprometheus import Gauge\n   from pyprometheus import BaseRegistry, LocalMemoryStorage\n\n   storage = LocalMemoryStorage()\n   registry = CollectorRegistry(storage=storage)\n   gauge = Gauge(\"job_in_progress\", \"Description\", registry=registry)\n\n   gauge.inc(10)\n   gauge.dec(5)\n   gauge.set(21.1)\n\n\nutilities::\n\n  gauge.set_to_current_time()   # Set to current unixtime\n\n  # Increment when entered, decrement when exited.\n  @gauge.track_in_progress()\n  def f():\n      pass\n\n  with gauge.track_in_progress():\n      pass\n\n\n  with gauge.time():\n      time.sleep(10)\n\n\n\nCounter\n~~~~~~~\n\nA counter is a cumulative metric that represents a single numerical value that only ever goes up.::\n\n   from pyprometheus import Counter\n   from pyprometheus import BaseRegistry, LocalMemoryStorage\n\n   storage = LocalMemoryStorage()\n   registry = CollectorRegistry(storage=storage)\n   counter = Counter(\"requests_total\", \"Description\", registry=registry)\n\n   counter.inc(10)\n\n\n\n\n\nSummary\n~~~~~~~\n\nSimilar to a histogram, a summary samples observations (usually things like request durations and response sizes).::\n\n   from pyprometheus import Summary\n   from pyprometheus import BaseRegistry, LocalMemoryStorage\n\n   storage = LocalMemoryStorage()\n   registry = CollectorRegistry(storage=storage)\n   s = Summary(\"requests_duration_seconds\", \"Description\", registry=registry)\n\n   s.observe(0.100)\n\n\nutilities for timing code::\n\n   @gauge.time()\n   def func():\n      time.sleep(10)\n\n   with gauge.time():\n      time.sleep(10)\n\n\n\nHistogram\n~~~~~~~~~\n\nA histogram samples observations (usually things like request durations or response sizes) and counts them in configurable buckets. It also provides a sum of all observed values.::\n\n  from pyprometheus import Summary\n   from pyprometheus import BaseRegistry, LocalMemoryStorage\n\n   storage = LocalMemoryStorage()\n   registry = CollectorRegistry(storage=storage)\n   histogram = Histogram(\"requests_duration_seconds\", \"Description\", registry=registry)\n\n   histogram.observe(1.1)\n\nutilities for timing code::\n\n   @histogram.time()\n   def func():\n      time.sleep(10)\n\n   with histogram.time():\n      time.sleep(10)\n\n\n\nLabels\n~~~~~~\n\nAll metrics can have labels, allowing grouping of related time series.\n\n\nExample::\n\n    from pyprometheus import Counter\n    c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])\n    c.labels('get', '/').inc()\n    c.labels('post', '/submit').inc()\n\nor labels as keyword arguments::\n\n    from pyprometheus import Counter\n    c = Counter('my_requests_total', 'HTTP Failures', ['method', 'endpoint'])\n    c.labels(method='get', endpoint='/').inc()\n    c.labels(method='post', endpoint='/submit').inc()\n\n\n\nSTORAGES\n--------\n\nCurrently library support 2 storages: LocalMemoryStorage and UWSGIStorage\n\nEvery registry MUST have link to storage::\n\n  from pyprometheus import BaseRegistry, LocalMemoryStorage\n\n  storage = LocalMemoryStorage()\n  registry = CollectorRegistry(storage=storage)\n\n\nUse LocalMemoryStorage\n~~~~~~~~~~~~~~~~~~~~~~\n\nSimple storage that store samples to application memory. It can be used with threads.::\n\n  from pyprometheus import BaseRegistry, LocalMemoryStorage\n\n  storage = LocalMemoryStorag()\n\n\nUse UWSGIStorage\n~~~~~~~~~~~~~~~~\n\nUWSGIStorage allow to use `uwsgi sharedarea`_ to sync metrics between processes.::\n\n  from pyprometheus.contrib.uwsgi_features import UWSGICollector, UWSGIStorage\n\n  SHAREDAREA_ID = 0\n  storage = UWSGIStorage(SHAREDAREA_ID)\n\n\n\nalso need to configure UWSGI sharedaread pages.\n\n\n\n\nEXPORTING\n---------\n\nLibrary have some helpers to export metrics\n\nTo text format\n~~~~~~~~~~~~~~\n\nYou can convert registry to text format::\n\n\n  from pyprometheus import BaseRegistry, LocalMemoryStorage\n  from pyprometheus.utils.exposition import registry_to_text\n  from pyprometheus import Gauge\n\n  storage = LocalMemoryStorage()\n  registry = CollectorRegistry(storage=storage)\n  g = Gauge('raid_status', '1 if raid array is okay', registry=registry)\n  g.set(1)\n  print(registry_to_text(registry))\n\n\n\nText file export\n~~~~~~~~~~~~~~~~\n\nThis is useful for monitoring cronjobs, or for writing cronjobs to expose metrics about a machine system.::\n\n  from pyprometheus import BaseRegistry, LocalMemoryStorage\n  from pyprometheus.utils.exposition import registry_to_text, write_to_textfile\n  from pyprometheus import Gauge\n\n  storage = LocalMemoryStorage()\n  registry = CollectorRegistry(storage=storage)\n  g = Gauge('raid_status', '1 if raid array is okay', registry=registry)\n  g.set(1)\n  write_to_textfile(registry, \"/path/to/file/metrics.prom\")\n\n\nYou can configure `text file collector`_ to use generated file.\n\n\nTODO\n----\n\nSome features that we plan to do:\n\n- [ ] Add mmap storage\n- [ ] Add features for async frameworks\n- [ ] Optimize UWSGI storage byte pad\n- [ ] Add quantiles\n\n\n\nEXAMPLE PROJECT\n---------------\n\nWe create `example project`_ to show hot to use pyprometheus in real project.\n\n\nCONTRIBUTE\n----------\n\nFork https://github.com/Lispython/pyprometheus/ , create commit and pull request to ``develop``.\n\n\n\n.. _`example project`: http://github.com/Lispython/pyprometheus_demo\n.. _`text file collector`: https://github.com/prometheus/node_exporter#textfile-collector\n.. _`uwsgi sharedarea`: http://uwsgi-docs.readthedocs.io/en/latest/SharedArea.html\n.. _`Prometheus`: http://prometheus.io\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flispython%2Fpyprometheus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flispython%2Fpyprometheus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flispython%2Fpyprometheus/lists"}