Skip to content

Fix spaces in grouping key values for push_to_gateway#1156

Open
veeceey wants to merge 1 commit intoprometheus:masterfrom
veeceey:fix/issue-1064-spaces-in-group-key-values
Open

Fix spaces in grouping key values for push_to_gateway#1156
veeceey wants to merge 1 commit intoprometheus:masterfrom
veeceey:fix/issue-1064-spaces-in-group-key-values

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Fixes the handling of spaces in grouping key values when using push_to_gateway. Previously, spaces were converted to + signs, which resulted in incorrect label values in Prometheus Pushgateway.

Problem

When pushing metrics with grouping keys containing spaces, the quote_plus function converts spaces to + signs in the URL. The Pushgateway interprets these as literal + characters rather than spaces.

Example:

push_to_gateway('http://localhost:9091', 
                job='job_name', 
                grouping_key={'label': 'value with spaces'}, 
                registry=registry)

Before fix: Label value becomes value+with+spaces
After fix: Label value is correctly value with spaces

Solution

Use base64 encoding for grouping key values containing spaces, similar to how values with slashes are already handled. This ensures the value is transmitted accurately to the Pushgateway.

Changes

  • Modified _escape_grouping_key() to check for spaces in addition to slashes
  • Added test case test_push_with_groupingkey_with_spaces() to verify the fix
  • Updated comment to clarify that both slashes and spaces trigger base64 encoding

Test Plan

  • Added new test test_push_with_groupingkey_with_spaces that verifies:
    • A grouping key value with spaces uses base64 encoding
    • The resulting URL path uses the @base64 suffix
    • The value is correctly encoded
  • All existing push_to_gateway tests pass (21 passed, 1 skipped)

Manual verification:
The fix can be verified by running the reproduction script from the issue:

from prometheus_client import push_to_gateway
from prometheus_client.metrics_core import GaugeMetricFamily
from prometheus_client.registry import Collector, CollectorRegistry

m1 = GaugeMetricFamily(name='test_metric', documentation='N/A')

class _CustomCollector(Collector):
    def collect(self):
        yield m1

registry = CollectorRegistry()
registry.register(_CustomCollector())

push_to_gateway('http://localhost:9091', 
                job='job_name', 
                grouping_key={'label': 'value with spaces'}, 
                registry=registry)

After this change, the Pushgateway will correctly show label="value with spaces" instead of label="value+with+spaces".

Fixes #1064

Use base64 encoding for grouping key values containing spaces,
similar to how values with slashes are handled. This prevents
spaces from being converted to '+' signs by quote_plus().

Fixes prometheus#1064

Signed-off-by: Varun Chawla <varun_6april@hotmail.com>
@veeceey veeceey force-pushed the fix/issue-1064-spaces-in-group-key-values branch from 3f4de1b to ff09e4a Compare February 8, 2026 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

push_to_gateway: handling of spaces in group key values

1 participant