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/
uv sync --locked --all-extras --dev
uv run 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.2.0.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygments_promql-0.2.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pygments_promql-0.2.0.tar.gz.

File metadata

  • Download URL: pygments_promql-0.2.0.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pygments_promql-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f3f3f03d9a94e1603d6611bd235dc6bf532b033037a97b8fe4d88f661a38a63e
MD5 fcca7465a66c2a59d2f2c8806c4783cc
BLAKE2b-256 8cbc214db8106844a109d0cb77ea5adf2939d8dc769083f22c4ad6c11f39da4a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygments_promql-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for pygments_promql-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 98033a1945e63b80cfb3d4444422e54a0de22728c95d53e8e9d47c3ceee237f8
MD5 9bd7abc85d6fea70d96773bc8aa160d8
BLAKE2b-256 81acd329a5a37873f3c79b9e00b20bfa6f17939c4a4a35e5e8219dece120988e

See more details on using hashes here.

Supported by

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