Skip to content

feat: add ability to parse promql with grafana templates inside #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

com30n
Copy link

@com30n com30n commented Nov 4, 2022

Hello!

I'm writing a script, which can download grafana dashboard, patch promql expressions (add some labels, rename labels, etc),
and push it back to grafana.

For parsing promql I'm using the lexer, and while doing some tests I faced the problem:
at some dashboards promql expressions contain grafana templates.

here is an example of the expression:
vault_route_create_${mountpoint}_{quantile="0.99"}

and it made a problem while parsing it, because the Metrics regex doesn't contain an expression to parse strings inside ${} symbols.

So, the little fix adds the ability to parse promql expressions that contain grafana templates inside.

P.S. While making the changes I found that the tests is not using local PromQLLexer class, so I added a new test, which is testing the new changes, with new test cases.

@pabluk
Copy link
Owner

pabluk commented Nov 23, 2022

Hi @com30n, sorry for the late reply and thanks for your contribution!

It's an interesting use case, and it could be great to have support for that kind of expressions from Grafana.

@@ -157,7 +157,8 @@ class PromQLLexer(RegexLexer):
(r"==|!=|>=|<=|<|>", Operator),
(r"and|or|unless", Operator.Word),
# Metrics
(r"[_a-zA-Z][_a-zA-Z0-9]+", Name.Variable),
(r"[_a-zA-Z][_a-zA-Z0-9]+(\$\{?[_a-zA-Z0-9]*\})?[_a-zA-Z0-9]*", Name.Variable),
Copy link
Owner

@pabluk pabluk Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be better to implement this specific rules creating a subclass of PromQLLexer, something like:

class PromQLGrafanaLexer(PromQLLexer):
    name = "PromQLGrafana"
    aliases = ["promqlgrafana"]
    filenames = ["*.promql"]

    # only overwriting the needed tokens
    tokens["root"] = {...}  # new rules for Grafana
...

and adding the class to:

__all__ = ["PromQLLexer", "PromQLGrafanaLexer"]

that will allow to choose a specific lexer to users only wanting pure promql or Grafana one.

@pabluk
Copy link
Owner

pabluk commented Nov 23, 2022

By the way, requirements were updated on #16 and #17 so you can rebase this branch to pass tests.

@pabluk pabluk added the enhancement New feature or request label Nov 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants