A simple proxy service for Prometheus that rewrites labels in queries and results.
- Intercepts Prometheus API requests and rewrites label names according to configured rules
- Supports label rewriting in both queries and results
- Handles compressed (gzip) responses from Prometheus
- Configurable via YAML file
- Transparent pass-through of authentication headers
- Designed for easy extension with more complex rewriting rules
The proxy is configured via a YAML file. Here's an example:
target_prometheus: "http://localhost:9090"
mappings:
- direction: "query"
rules:
- source_label: "instance"
target_label: "host"
- source_label: "job"
target_label: "service"
- direction: "result"
rules:
- source_label: "instance"
target_label: "host"
- source_label: "job"
target_label: "service"target_prometheus: The URL of the upstream Prometheus servermappings: A list of mapping configurationsdirection: The direction to apply the rules to (query,result, orboth)rules: A list of label mapping rulessource_label: The original label nametarget_label: The new label name
go build -o prom-relabel-proxy ./cmd/prom-relabel./prom-relabel-proxy --config=configs/config.yaml --listen=:8080--config: Path to the configuration file (default:configs/config.yaml)--listen: Address to listen on (default::8080)--debug: Enable detailed debug logging (default:false)
With the example configuration above, a query like:
http://localhost:8080/api/v1/query?query=up{instance="localhost:9090",job="prometheus"}
Will be rewritten to:
http://localhost:9090/api/v1/query?query=up{host="localhost:9090",service="prometheus"}
And the labels in the response will be rewritten back from host to instance and from service to job.
The proxy automatically detects and handles gzip-compressed responses from Prometheus:
- Decompresses responses before applying label transformations
- Re-compresses responses before sending them back to the client
- Preserves all original headers and compression settings
When run with the --debug flag, the proxy provides detailed logging about:
- Incoming requests and how they're rewritten
- Response handling, including compression detection and decompression
- Label transformations in both directions
- Content of requests and responses (truncated for readability)
This can be helpful when troubleshooting label rewriting issues or understanding how the proxy is transforming your queries and results.
For Kubernetes deployment, you can create a ConfigMap for the configuration and deploy the proxy as a Service.
- Support for more complex transformation rules (regex, conditionals)
- Metrics about proxy operations
- Caching for performance optimization
- Multiple upstream Prometheus servers