Skip to content

Commit

Permalink
add docs (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
lexdene authored Mar 24, 2024
1 parent 26918c2 commit abfdddd
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 187 deletions.
14 changes: 14 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2

build:
os: ubuntu-lts-latest
tools:
python: latest

sphinx:
configuration: docs/conf.py
fail_on_warning: true

formats:
- pdf
- epub
191 changes: 8 additions & 183 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,188 +1,13 @@
# Pangolier

prometheus query builder
build PromQL by Python code.

## install
[![pypi version]][pypi]
[![Documentation Status]][Documentation]

pip install pangolier
see docs at: https://pangolier.readthedocs.io

## usage

### simple case

For a metric with filters:

from pangolier.metrics import Metric

print(Metric('http_requests_total').filter(
job='prometheus',
group='canary'
).to_str())

output:

http_requests_total{job="prometheus", group="canary"}

### pretty output

Add `pretty=True` in `to_str` for better readability:

from pangolier.metrics import Metric

print(Metric('http_requests_total').filter(
job='prometheus',
group='canary'
).to_str(pretty=True))

output:

http_requests_total{
job="prometheus",
group="canary"
}

I will always use `pretty=True` in rest of this document.

### functions

Prometheus functions can be built by name. For example:

from pangolier.functions import function

abs = function('abs')
print(abs(Metric('http_requests_total')).to_str(pretty=True))

output:

abs(
http_requests_total
)

`range_function` should be used for functions accept a `range-vector`.

from pangolier.functions import range_function

rate = range_function('rate')
print(rate(Metric('http_requests_total'), timespan='5m').to_str(pretty=True))

output:

rate(
http_requests_total[5m]
)

`aggregation_operator` shoule be used for aggregation operators:

from pangolier.functions import aggregation_operator

sum_ = aggregation_operator('sum')
print(sum_(
Metric('http_requests_total'),
by=['job', 'group'],
).to_str(pretty=True))

output:

sum by(
job, group
)(
http_requests_total
)

combine them all together:

histogram_quantile = function('histogram_quantile')
rate = range_function('rate')
sum_ = aggregation_operator('sum')

print(histogram_quantile(
0.9,
sum_(
rate(
Metric('http_request_duration_seconds_bucket'),
timespan='5m',
),
by=['le']
)
).to_str(pretty=True))

output:

histogram_quantile(
0.9,
sum by(
le
)(
rate(
http_request_duration_seconds_bucket[5m]
)
)
)

### binary operators

support following binary operators:

* `+` (addition)
* `-` (subtraction)
* `*` (multiplication)
* `/` (division)
* `%` (modulo)
* `^` (power/exponentiation)

For example, divide one metric with another:

from pangolier.metrics import Metric
from pangolier.functions import range_function

rate = range_function('rate')
print((
rate(
Metric('foo').filter(
group='canary'
),
timespan='5m'
) / rate(
Metric('bar').filter(
group='canary'
),
timespan='5m'
)
).to_str(pretty=True))

output:

rate(
foo{
group="canary"
}[5m]
) / rate(
bar{
group="canary"
}[5m]
)

For operation with modifier:

from pangolier.metrics import Metric, BinOp, GroupLeft

print(BinOp(
'*',
Metric('foo'),
Metric('bar'),
on=['interface', 'job'],
group=GroupLeft('node', 'resource'),
).to_str(pretty=True))

output:

foo * on(
interface, job
) group_left(
node, resource
) bar


## about name

[Pangolier](https://dota2.fandom.com/wiki/Pangolier)
[pypi version]: https://img.shields.io/pypi/v/pangolier
[pypi]: https://pypi.org/project/pangolier
[Documentation Status]: https://readthedocs.org/projects/pangolier/badge
[Documentation]: https://pangolier.readthedocs.io
7 changes: 7 additions & 0 deletions docs/about.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
about
=====

about name
----------

`Pangolier <https://dota2.fandom.com/wiki/Pangolier>`_
71 changes: 71 additions & 0 deletions docs/bin-op.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
binary operators
================

support following binary operators:

* ``+`` (addition)
* ``-`` (subtraction)
* ``*`` (multiplication)
* ``/`` (division)
* ``%`` (modulo)
* ``^`` (power/exponentiation)

For example, divide one metric with another:

.. code-block:: python
from pangolier.metrics import Metric
from pangolier.functions import range_function
rate = range_function('rate')
print((
rate(
Metric('foo').filter(
group='canary'
),
timespan='5m'
) / rate(
Metric('bar').filter(
group='canary'
),
timespan='5m'
)
).to_str(pretty=True))
output:

.. code-block::
rate(
foo{
group="canary"
}[5m]
) / rate(
bar{
group="canary"
}[5m]
)
For operation with modifier:

.. code-block:: python
from pangolier.metrics import Metric, BinOp, GroupLeft
print(BinOp(
'*',
Metric('foo'),
Metric('bar'),
on=['interface', 'job'],
group=GroupLeft('node', 'resource'),
).to_str(pretty=True))
output:

.. code-block::
foo * on(
interface, job
) group_left(
node, resource
) bar
3 changes: 3 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
project = 'pangolier'
copyright = 'Attribution-NonCommercial-ShareAlike 4.0'
version = '0.2.2'
97 changes: 97 additions & 0 deletions docs/functions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
functions
=========

Prometheus functions can be built by name. For example:

.. code-block:: python
from pangolier.functions import function
abs = function('abs')
print(abs(
Metric('http_requests_total')
).to_str(pretty=True))
output:

.. code-block::
abs(
http_requests_total
)
``range_function`` should be used for functions accept a ``range-vector``.

.. code-block:: python
from pangolier.functions import range_function
rate = range_function('rate')
print(rate(
Metric('http_requests_total'),
timespan='5m'
).to_str(pretty=True))
output:

.. code-block::
rate(
http_requests_total[5m]
)
``aggregation_operator`` shoule be used for aggregation operators:

.. code-block:: python
from pangolier.functions import aggregation_operator
sum_ = aggregation_operator('sum')
print(sum_(
Metric('http_requests_total'),
by=['job', 'group'],
).to_str(pretty=True))
output:

.. code-block::
sum by(
job, group
)(
http_requests_total
)
combine them all together:

.. code-block:: python
histogram_quantile = function('histogram_quantile')
rate = range_function('rate')
sum_ = aggregation_operator('sum')
print(histogram_quantile(
0.9,
sum_(
rate(
Metric('http_request_duration_seconds_bucket'),
timespan='5m',
),
by=['le']
)
).to_str(pretty=True))
output:

.. code-block::
histogram_quantile(
0.9,
sum by(
le
)(
rate(
http_request_duration_seconds_bucket[5m]
)
)
)
Loading

0 comments on commit abfdddd

Please sign in to comment.