(
method: str,
gateway: str,
job: str,
registry: Optional[Collector],
grouping_key: Optional[Dict[str, Any]],
timeout: Optional[float],
handler: Callable,
compression: CompressionType = None,
)
| 715 | |
| 716 | |
| 717 | def _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 | |
| 760 | def _compress_payload(data: bytes, compression: CompressionType) -> Tuple[bytes, List[Tuple[str, str]]]: |
no test coverage detected