diff --git a/.readthedocs.yaml b/.readthedocs.yaml new file mode 100644 index 0000000..9138a7f --- /dev/null +++ b/.readthedocs.yaml @@ -0,0 +1,13 @@ +version: "2" + +build: + os: "ubuntu-22.04" + tools: + python: "3.10" + +python: + install: + - requirements: docs/requirements.txt + +sphinx: + configuration: docs/source/conf.py diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..d0c3cbf --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line, and also +# from the environment for the first two. +SPHINXOPTS ?= +SPHINXBUILD ?= sphinx-build +SOURCEDIR = source +BUILDDIR = build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..6247f7e --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,35 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=sphinx-build +) +set SOURCEDIR=source +set BUILDDIR=build + +if "%1" == "" goto help + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The 'sphinx-build' command was not found. Make sure you have Sphinx + echo.installed, then set the SPHINXBUILD environment variable to point + echo.to the full path of the 'sphinx-build' executable. Alternatively you + echo.may add the Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% + +:end +popd diff --git a/docs/requirements.txt b/docs/requirements.txt new file mode 100644 index 0000000..53fc1f3 --- /dev/null +++ b/docs/requirements.txt @@ -0,0 +1,2 @@ +sphinx==7.1.2 +sphinx-rtd-theme==1.3.0rc1 diff --git a/docs/source/conf.py b/docs/source/conf.py new file mode 100644 index 0000000..ae7d07d --- /dev/null +++ b/docs/source/conf.py @@ -0,0 +1,30 @@ +# -- Project information + +project = "prometheus-summary" +copyright = "2025, RefaceAI" +author = "RefaceAI" + +# -- General configuration + +extensions = [ + "sphinx.ext.duration", + "sphinx.ext.doctest", + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", + "sphinx.ext.intersphinx", +] + +intersphinx_mapping = { + "python": ("https://docs.python.org/3/", None), + "sphinx": ("https://www.sphinx-doc.org/en/master/", None), +} +intersphinx_disabled_domains = ["std"] + +templates_path = ["_templates"] + +# -- Options for HTML output + +html_theme = "sphinx_rtd_theme" + +# -- Options for EPUB output +epub_show_urls = "footnote" diff --git a/docs/source/index.rst b/docs/source/index.rst new file mode 100644 index 0000000..ed3ea39 --- /dev/null +++ b/docs/source/index.rst @@ -0,0 +1,99 @@ +Welcome to prometheus-summary documentation! +============================================ + +`Prometheus-summary library `_ adds support of quantiles calculation for the Summary metric. +It is fully compatible with the official `Python Prometheus client library `_. + +Installation +------------ + +.. code-block:: console + + pip install prometheus-summary==0.1.4 + +This package can be found on `PyPI `_. + +Collecting +---------- + +Basic usage +^^^^^^^^^^^ + +.. code-block:: python + + from prometheus_summary import Summary + + s = Summary("request_latency_seconds", "Description of summary") + s.observe(4.7) + +Usage with labels +^^^^^^^^^^^^^^^^^^^^^ + +.. code-block:: python + + from prometheus_summary import Summary + + s = Summary("request_latency_seconds", "Description of summary", ["method", "endpoint"]) + s.labels(method="GET", endpoint="/profile").observe(1.2) + s.labels(method="POST", endpoint="/login").observe(3.4) + +Usage with custom quantiles and precisions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +By default, metrics are observed for the following (quantile, precision (inaccuracy)) pairs: + +``((0.50, 0.05), (0.90, 0.01), (0.99, 0.001))`` + +You can also provide your own values when creating the metric. + +.. code-block:: python + from prometheus_summary import Summary + + s = Summary( + "request_latency_seconds", "Description of summary", + invariants=((0.50, 0.05), (0.75, 0.02), (0.90, 0.01), (0.95, 0.005), (0.99, 0.001)), + ) + s.observe(4.7) + +Usage with custom time window settings +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Typically, you don't want to have a Summary representing the entire runtime of the application, +but you want to look at a reasonable time interval. This Summary metric implement a configurable sliding time window. + +The default is a time window of 10 minutes and 5 age buckets, i.e. the time window is 10 minutes wide, and +we slide it forward every 10 / 5 = 2 minutes, but you can configure this values for your own purposes. + +.. code-block:: python + from prometheus_summary import Summary + + s = Summary( + "request_latency_seconds", "Description of summary", + # time window 5 minutes wide with 10 age buckets (sliding every 30 seconds) + max_age_seconds=5 * 60, + age_buckets=10, + ) + s.observe(4.7) + +Querying +-------- + +Suppose we have a metric: + +.. code-block:: python + + from prometheus_summary import Summary + + s = Summary("request_latency_seconds", "Description of summary", ["method", "endpoint"]) + +To show request latency by `method`, `endpoint` and `quantile` use the following PromQL query: + +.. code-block:: promql + + max by (method, endpoint, quantile) (request_latency_seconds) + +To show only 99-th quantile: + +.. code-block:: promql + + max by (method, endpoint) (request_latency_seconds{quantile="0.99"})