-
Notifications
You must be signed in to change notification settings - Fork 211
Add PromQL and LogQL query validation #431
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
base: main
Are you sure you want to change the base?
Conversation
This commit adds validation for PromQL and LogQL queries before execution to provide early feedback on syntax errors and reduce wasted API calls. Changes: - Add ValidatePromQL() using Prometheus's official parser - Add ValidateLogQL() using Loki's official syntax parser - Integrate inline validation into query_prometheus tool - Integrate inline validation into query_loki_logs tool - Add comprehensive unit tests for both validators - Add github.com/grafana/loki/v3 dependency for LogQL parsing Benefits: - Catches syntax errors before sending to datasource - Provides clear error messages for debugging - Reduces iteration cycles for LLM-generated queries - Uses production-grade parsers from official projects 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
sd2k
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @cbcoutinho! Thanks for the contribution, the code looks solid.
The main reason we haven't added this before is because it adds a rather large dependency on Loki just for the parsing, and another 11MB to the final binary, which felt a bit overkill; ideally we would be getting decent error messages back from Loki/Prometheus anyway if the query was invalid. Did you find that wasn't the case? Were we returning poor error messages for invalid queries?
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
Hi @sd2k, you're right that the error messages themselves are almost identical from prometheus/loki; however, this PR makes a few adjustments to that flow:
|
Description
Adds validation for PromQL and LogQL queries before execution to provide early feedback on syntax errors and reduce wasted API calls.
Fixes #430
Changes
Core Implementation
tools/query_validation.go: New validation functions using official parsersValidatePromQL(): Usesgithub.com/prometheus/prometheus/promql/parserValidateLogQL(): Usesgithub.com/grafana/loki/v3/pkg/logql/syntaxIntegration
tools/prometheus.go: Added inline validation toquery_prometheustooltools/loki.go: Added inline validation toquery_loki_logstoolTests
tools/query_validation_unit_test.go: Comprehensive unit testsDependencies
github.com/grafana/loki/v3for LogQL parsinggithub.com/prometheus/prometheusfor PromQL parsingBenefits
Testing
All tests pass:
Example Error Messages
Invalid PromQL:
Invalid LogQL:
Design Decisions
Inline Validation vs Separate Tools
validate_promqlandvalidate_logqltoolsParser Selection
github.com/prometheus/prometheus/promql/parser- official Prometheus parsergithub.com/grafana/loki/v3/pkg/logql/syntax- official Loki parserMigration Notes
This is a non-breaking change:
Related Issues
Addresses the common problem where LLMs generate invalid queries and need multiple iterations to fix syntax errors. This reduces context usage and improves the overall query generation workflow.
This PR was generated with the help of AI, and reviewed by a Human