Skip to main content

A PromQL lexer for Pygments

Project description

pygments-promql

Python package PyPI PyPI - License

A PromQL lexer for Pygments.

This Python package provides a Pygments lexer for the Prometheus Query Language. It allows Pygments and other tools (Sphinx, Chroma, etc) to highlight PromQL queries.

PromQL syntax highlighted

Installation

Using pip

To get the latest version from pypi.org:

pip install pygments-promql

Usage

Command-line

In a terminal you can echo and pipe a query directly from stdin:

echo 'prometheus_http_requests_total{code="200"}' | pygmentize -l promql

Or use a file, for example, create the example.promql file with queries from tests/example.promql. In this case the option -l promql is not needed because the lexer will be detected based on the file extension.

Showing colorized output in a terminal:

pygmentize example.promql

To generate a PNG file:

pygmentize -f png -O "line_numbers=False,style=monokai" -o example.png example.promql

Python code

The following example:

from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments_promql import PromQLLexer

query = 'http_requests_total{handler="/api/comments"}'
print(highlight(query, PromQLLexer(), HtmlFormatter()))

Will generate this HTML output:

<div class="highlight">
    <pre>
        <span></span>
	<span class="nv">http_requests_total</span>
	<span class="p">{</span>
	<span class="nl">handler</span>
	<span class="o">=</span>
	<span class="s">&quot;/api/comments&quot;</span>
	<span class="p">}</span>
	<span class="w"></span>
    </pre>
</div>

Use HtmlFormatter(noclasses=True) to include CSS inline styles on every <span> tag.

Sphinx

In order to highlight PromQL syntax in your Sphinx documentation site you just need to add this 3 lines of Python code at the end of your site's conf.py file:

from sphinx.highlighting import lexers
from pygments_promql import PromQLLexer
lexers['promql'] = PromQLLexer()

Then you will be able to use it like this:

Here's a PromQL example:

.. code-block:: promql

	# A metric with label filtering
	go_gc_duration_seconds{instance="localhost:9090"}

Testing

If you want to test, play or contribute to this repo:

git clone https://github.com/pabluk/pygments-promql.git
cd pygments-promql/
pip install -r requirements.txt
pip install -e .
pytest -v

Project details


Download files

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

Source Distribution

pygments-promql-0.0.9.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

pygments_promql-0.0.9-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file pygments-promql-0.0.9.tar.gz.

File metadata

  • Download URL: pygments-promql-0.0.9.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pygments-promql-0.0.9.tar.gz
Algorithm Hash digest
SHA256 d31dba2028c00239cf41306d41ae3276d57e6dfe509928525da44edcfdddf8a5
MD5 57b93c7abe964ae8f771a05b647f19b5
BLAKE2b-256 a8fedcd4dfe8a9eb6e12817f865d9d8eba204dd55a8bfcb1ae4637d36c57b541

See more details on using hashes here.

File details

Details for the file pygments_promql-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: pygments_promql-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for pygments_promql-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 b6b41ebd4d8d50acf44e5d396718e67681f83ca0f929cde2a33dda1171b9e378
MD5 0f0224e4cd28ed08fb742e6f09105bd4
BLAKE2b-256 a3eadbfa6888da2b6dc245f31c81ee14067610f94cd71b628efcf2b795bf14b6

See more details on using hashes here.

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