From 467b5662c8ea00cdd316df334f3073e82504cc97 Mon Sep 17 00:00:00 2001 From: ritesh-avesha Date: Fri, 8 Dec 2023 17:42:45 +0530 Subject: [PATCH 1/2] feat(): Added gzip /snappy compression support --- docs/content/exporting/pushgateway.md | 9 +++++ prometheus_client/exposition.py | 48 +++++++++++++++++++++++---- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/docs/content/exporting/pushgateway.md b/docs/content/exporting/pushgateway.md index f1ea5e43..c2496b01 100644 --- a/docs/content/exporting/pushgateway.md +++ b/docs/content/exporting/pushgateway.md @@ -14,6 +14,10 @@ g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finis g.set_to_current_time() push_to_gateway('localhost:9091', job='batchA', registry=registry) ``` +To use compression (either gzip or snappy), add these params: +``` +push_to_gateway('localhost:9091', job='batchA', registry=registry, use_compression=True, compression_type="gzip") +``` A separate registry is used, as the default registry may contain other metrics such as those from the Process Collector. @@ -64,3 +68,8 @@ g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finis g.set_to_current_time() push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler) ``` + +To use compression with with authentication(either gzip or snappy), add these params: +``` +push_to_gateway('localhost:9091', job='batchA', registry=registry,handler=my_auth_handler, use_compression=True, compression_type="gzip") +``` diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index f2b7442b..94ef2c5a 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -485,6 +485,8 @@ def push_to_gateway( grouping_key: Optional[Dict[str, Any]] = None, timeout: Optional[float] = 30, handler: Callable = default_handler, + use_compression: Optional[bool] = False, + compression_type: Optional[str] = None, ) -> None: """Push metrics to the given pushgateway. @@ -521,10 +523,15 @@ def push_to_gateway( failure. 'content' is the data which should be used to form the HTTP Message Body. + `use_compression` is a boolean indicating whether to compress the data + `compression_type` is a string indicating the compression type to use + if use_compression is True. Currently only 'gzip' + and `snappy` are supported. If None, then compression + will not be used. This overwrites all metrics with the same job and grouping_key. This uses the PUT HTTP method.""" - _use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler) + _use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler, use_compression, compression_type) def pushadd_to_gateway( @@ -534,6 +541,8 @@ def pushadd_to_gateway( grouping_key: Optional[Dict[str, Any]] = None, timeout: Optional[float] = 30, handler: Callable = default_handler, + use_compression: Optional[bool] = False, + compression_type: Optional[str] = None, ) -> None: """PushAdd metrics to the given pushgateway. @@ -552,10 +561,15 @@ def pushadd_to_gateway( will be carried out by a default handler. See the 'prometheus_client.push_to_gateway' documentation for implementation requirements. + `use_compression` is a boolean indicating whether to compress the data + `compression_type` is a string indicating the compression type to use + if use_compression is True. Currently only 'gzip' + and `snappy` are supported. If None, then compression + will not be used. This replaces metrics with the same name, job and grouping_key. This uses the POST HTTP method.""" - _use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler) + _use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler, use_compression, compression_type) def delete_from_gateway( @@ -595,6 +609,8 @@ def _use_gateway( grouping_key: Optional[Dict[str, Any]], timeout: Optional[float], handler: Callable, + use_compression: Optional[bool] = False, + compression_type: Optional[str] = None, ) -> None: gateway_url = urlparse(gateway) # See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6. @@ -615,11 +631,31 @@ def _use_gateway( url += ''.join( '/{}/{}'.format(*_escape_grouping_key(str(k), str(v))) for k, v in sorted(grouping_key.items())) + + if use_compression: + + if compression_type == 'gzip': + headers = [('Content-Type', CONTENT_TYPE_LATEST), ('Content-Encoding', 'gzip')] + data = gzip.compress(data) + handler(url=url, method=method, timeout=timeout, headers=headers, data=data)() + + elif compression_type == 'snappy': + try: + import snappy + headers = [('Content-Type', CONTENT_TYPE_LATEST), ('Content-Encoding', 'snappy')] + # Currently pushgateways works with snappy framing format + data = snappy.snappy.StreamCompressor().compress(data) + handler(url=url, method=method, timeout=timeout, headers=headers, data=data)() + except ImportError: + raise ImportError('Snappy compression requires the snappy module') - handler( - url=url, method=method, timeout=timeout, - headers=[('Content-Type', CONTENT_TYPE_LATEST)], data=data, - )() + else: + raise ValueError(f'Unsupported compression type: {compression_type}') + else: + handler( + url=url, method=method, timeout=timeout, + headers=[('Content-Type', CONTENT_TYPE_LATEST)], data=data, + )() def _escape_grouping_key(k, v): From a4ddaf15f0bada8d94b832442702a4a1d3a5e72e Mon Sep 17 00:00:00 2001 From: ritesh-avesha <104001014+ritesh-avesha@users.noreply.github.com> Date: Sat, 8 Nov 2025 17:30:53 +0530 Subject: [PATCH 2/2] Revert "feat(): Added gzip /snappy compression support" --- docs/content/exporting/pushgateway.md | 9 ----- prometheus_client/exposition.py | 48 ++++----------------------- 2 files changed, 6 insertions(+), 51 deletions(-) diff --git a/docs/content/exporting/pushgateway.md b/docs/content/exporting/pushgateway.md index c2496b01..f1ea5e43 100644 --- a/docs/content/exporting/pushgateway.md +++ b/docs/content/exporting/pushgateway.md @@ -14,10 +14,6 @@ g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finis g.set_to_current_time() push_to_gateway('localhost:9091', job='batchA', registry=registry) ``` -To use compression (either gzip or snappy), add these params: -``` -push_to_gateway('localhost:9091', job='batchA', registry=registry, use_compression=True, compression_type="gzip") -``` A separate registry is used, as the default registry may contain other metrics such as those from the Process Collector. @@ -68,8 +64,3 @@ g = Gauge('job_last_success_unixtime', 'Last time a batch job successfully finis g.set_to_current_time() push_to_gateway('localhost:9091', job='batchA', registry=registry, handler=my_auth_handler) ``` - -To use compression with with authentication(either gzip or snappy), add these params: -``` -push_to_gateway('localhost:9091', job='batchA', registry=registry,handler=my_auth_handler, use_compression=True, compression_type="gzip") -``` diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 94ef2c5a..f2b7442b 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -485,8 +485,6 @@ def push_to_gateway( grouping_key: Optional[Dict[str, Any]] = None, timeout: Optional[float] = 30, handler: Callable = default_handler, - use_compression: Optional[bool] = False, - compression_type: Optional[str] = None, ) -> None: """Push metrics to the given pushgateway. @@ -523,15 +521,10 @@ def push_to_gateway( failure. 'content' is the data which should be used to form the HTTP Message Body. - `use_compression` is a boolean indicating whether to compress the data - `compression_type` is a string indicating the compression type to use - if use_compression is True. Currently only 'gzip' - and `snappy` are supported. If None, then compression - will not be used. This overwrites all metrics with the same job and grouping_key. This uses the PUT HTTP method.""" - _use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler, use_compression, compression_type) + _use_gateway('PUT', gateway, job, registry, grouping_key, timeout, handler) def pushadd_to_gateway( @@ -541,8 +534,6 @@ def pushadd_to_gateway( grouping_key: Optional[Dict[str, Any]] = None, timeout: Optional[float] = 30, handler: Callable = default_handler, - use_compression: Optional[bool] = False, - compression_type: Optional[str] = None, ) -> None: """PushAdd metrics to the given pushgateway. @@ -561,15 +552,10 @@ def pushadd_to_gateway( will be carried out by a default handler. See the 'prometheus_client.push_to_gateway' documentation for implementation requirements. - `use_compression` is a boolean indicating whether to compress the data - `compression_type` is a string indicating the compression type to use - if use_compression is True. Currently only 'gzip' - and `snappy` are supported. If None, then compression - will not be used. This replaces metrics with the same name, job and grouping_key. This uses the POST HTTP method.""" - _use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler, use_compression, compression_type) + _use_gateway('POST', gateway, job, registry, grouping_key, timeout, handler) def delete_from_gateway( @@ -609,8 +595,6 @@ def _use_gateway( grouping_key: Optional[Dict[str, Any]], timeout: Optional[float], handler: Callable, - use_compression: Optional[bool] = False, - compression_type: Optional[str] = None, ) -> None: gateway_url = urlparse(gateway) # See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6. @@ -631,31 +615,11 @@ def _use_gateway( url += ''.join( '/{}/{}'.format(*_escape_grouping_key(str(k), str(v))) for k, v in sorted(grouping_key.items())) - - if use_compression: - - if compression_type == 'gzip': - headers = [('Content-Type', CONTENT_TYPE_LATEST), ('Content-Encoding', 'gzip')] - data = gzip.compress(data) - handler(url=url, method=method, timeout=timeout, headers=headers, data=data)() - - elif compression_type == 'snappy': - try: - import snappy - headers = [('Content-Type', CONTENT_TYPE_LATEST), ('Content-Encoding', 'snappy')] - # Currently pushgateways works with snappy framing format - data = snappy.snappy.StreamCompressor().compress(data) - handler(url=url, method=method, timeout=timeout, headers=headers, data=data)() - except ImportError: - raise ImportError('Snappy compression requires the snappy module') - else: - raise ValueError(f'Unsupported compression type: {compression_type}') - else: - handler( - url=url, method=method, timeout=timeout, - headers=[('Content-Type', CONTENT_TYPE_LATEST)], data=data, - )() + handler( + url=url, method=method, timeout=timeout, + headers=[('Content-Type', CONTENT_TYPE_LATEST)], data=data, + )() def _escape_grouping_key(k, v):