Skip to main content

prometheus query builder

Project description

Pangolier

prometheus query builder

install

pip install pangolier

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pangolier-0.2.1-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page