promql-exec is a small Go CLI to test PromQL queries against a Prometheus-compatible endpoint. Connection profiles and reusable query definitions are kept separate for better reuse.
go mod download
go build -o promql-exec- Define connection profiles in
profiles/profiles.yaml(copy fromprofiles/profiles.sample.yaml). - Define PromQL queries as
.yamlfiles in thequeries/directory (for example based onqueries/queries.sample.yaml). - Combine a profile and a query when running the CLI.
# List available profiles
./promql-exec -list-profiles
# List available queries
./promql-exec -list-queries
# Execute a query with a profile
./promql-exec -profile example -query http-requests-rate
# Pretty-print the JSON response
./promql-exec -profile example -query list-jobs -pretty
# Verbose output (shows rendered query and values)
./promql-exec -profile example -query http-requests-rate -verbose
# Override template values from the command line
./promql-exec -profile example -query http-requests-rate \
-set job=my-job -set instance=my-instance:9090
# Use a custom profiles file and queries directory
./promql-exec -profiles profiles/custom.yaml -queries my-queries/ \
-profile dev -query customDefines connection profiles with server connection details and optional template values. The file is listed in .gitignore and should be copied from profiles/profiles.sample.yaml and adjusted locally:
profiles:
example:
serverAddress: "https://prometheus.example.com"
user: "your-user"
password: "your-password"
values:
job: "example-job"
instance: "example-instance:9090"values in a profile are a free key/value set used to populate template variables in your queries:
- Keys correspond to template names in the queries (for example
jobβ{{ .job }},instanceβ{{ .instance }}orzoneβ{{ .zone }}). - Optional: you do not have to define any
values. If they are missing, template variables stay empty or can be provided via-set. - Sample files:
profiles/profiles.sample.yamldefinesjobandinstance.queries/queries.sample.yamluses{{ .job }}and{{ .instance }}in the PromQL queries.
Defines reusable queries in YAML files. All .yaml and .yml files in the directory are loaded and merged.
Example: queries/example.yaml
queries:
http-requests-rate:
description: "Rate of HTTP requests per second"
query: 'sum(rate(http_requests_total{job="{{ .job }}", instance="{{ .instance }}"}[1m]))'
list-jobs:
description: "List all jobs"
query: 'group by (job) (up)'Template variables in queries (for example {{ .job }}) are replaced with the values from the selected profile (or CLI overrides).
Benefits:
- Organize queries by topic in separate files.
- Add new query files without changing existing ones.
- Keep large sets of queries manageable.
Values can be defined in the profile and/or passed via the command line:
# Use values from the profile
./promql-exec -profile example -query http-requests-rate
# Override a single value
./promql-exec -profile example -query http-requests-rate -set job=my-job
# Override multiple values
./promql-exec -profile example -query http-requests-rate -set job=my-job -set instance=my-instance:9090Precedence:
- CLI values (
-set) have highest precedence. - Profile values are used when no CLI values are given.
- Missing values result in empty template variables.
# Show all available jobs (see sample query file)
./promql-exec -profile example -query list-jobs -pretty
# HTTP requests rate for the example profile
./promql-exec -profile example -query http-requests-rate
# Same query with a different instance
./promql-exec -profile example -query http-requests-rate -set instance=another-instance:9090- β Separate connection profiles and queries
- β Reusable query definitions in YAML
- β Template variable support (profiles + CLI overrides)
- β CLI values override profile values
- β Multi-file query definitions (queries/ directory)
- β Basic authentication support
- β Verbose and pretty-print modes
- β Profile/query listing
promql-exec/
βββ promql-exec # Binary
βββ .gitignore # Ignore real profiles/queries
βββ profiles/ # Local connection profiles (gitignored)
β βββ profiles.sample.yaml # Sample profiles (in the repo)
β βββ profiles.yaml # Your local profiles (do not commit)
βββ queries/ # Query definitions
β βββ queries.sample.yaml # Sample queries (in the repo)
β βββ *.yaml # Your local queries (do not commit)
βββ README.md
This project is licensed under the MIT License. See LICENSE for details.
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
