MCPcopy Index your code
hub / github.com/prometheus/client_python / _use_gateway

Function _use_gateway

prometheus_client/exposition.py:717–757  ·  view source on GitHub ↗
(
        method: str,
        gateway: str,
        job: str,
        registry: Optional[Collector],
        grouping_key: Optional[Dict[str, Any]],
        timeout: Optional[float],
        handler: Callable,
        compression: CompressionType = None,
)

Source from the content-addressed store, hash-verified

715
716
717def _use_gateway(
718 method: str,
719 gateway: str,
720 job: str,
721 registry: Optional[Collector],
722 grouping_key: Optional[Dict[str, Any]],
723 timeout: Optional[float],
724 handler: Callable,
725 compression: CompressionType = None,
726) -> None:
727 gateway_url = urlparse(gateway)
728 # See https://bugs.python.org/issue27657 for details on urlparse in py>=3.7.6.
729 if not gateway_url.scheme or gateway_url.scheme not in ['http', 'https']:
730 gateway = f'http://{gateway}'
731
732 gateway = gateway.rstrip('/')
733 url = '{}/metrics/{}/{}'.format(gateway, *_escape_grouping_key("job", job))
734
735 if grouping_key is None:
736 grouping_key = {}
737 url += ''.join(
738 '/{}/{}'.format(*_escape_grouping_key(str(k), str(v)))
739 for k, v in sorted(grouping_key.items()))
740
741 data = b''
742 headers: List[Tuple[str, str]] = []
743 if method != 'DELETE':
744 if registry is None:
745 registry = REGISTRY
746 data = generate_latest(registry)
747 data, headers = _compress_payload(data, compression)
748 else:
749 # DELETE requests still need Content-Type header per test expectations
750 headers = [('Content-Type', CONTENT_TYPE_PLAIN_0_0_4)]
751 if compression is not None:
752 raise ValueError('Compression is not supported for DELETE requests.')
753
754 handler(
755 url=url, method=method, timeout=timeout,
756 headers=headers, data=data,
757 )()
758
759
760def _compress_payload(data: bytes, compression: CompressionType) -> Tuple[bytes, List[Tuple[str, str]]]:

Callers 3

push_to_gatewayFunction · 0.85
pushadd_to_gatewayFunction · 0.85
delete_from_gatewayFunction · 0.85

Calls 5

_escape_grouping_keyFunction · 0.85
_compress_payloadFunction · 0.85
joinMethod · 0.80
generate_latestFunction · 0.70
handlerFunction · 0.50

Tested by

no test coverage detected