|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +from typing import Dict, Optional |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import requests |
| 7 | + |
| 8 | +from azure.monitor.opentelemetry.exporter._constants import ( |
| 9 | + _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS, |
| 10 | + _ONE_SETTINGS_CHANGE_VERSION_KEY, |
| 11 | +) |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class OneSettingsResponse: |
| 17 | + """Response object containing OneSettings API response data. |
| 18 | +
|
| 19 | + This class encapsulates the parsed response from a OneSettings API call, |
| 20 | + including configuration settings, version information, and metadata. |
| 21 | +
|
| 22 | + Attributes: |
| 23 | + etag (Optional[str]): ETag header value for caching and conditional requests |
| 24 | + refresh_interval (int): Interval in seconds for the next configuration refresh |
| 25 | + settings (Dict[str, str]): Dictionary of configuration key-value pairs |
| 26 | + version (Optional[int]): Configuration version number for change tracking |
| 27 | + """ |
| 28 | + |
| 29 | + def __init__( |
| 30 | + self, |
| 31 | + etag: Optional[str] = None, |
| 32 | + refresh_interval: int = _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS, |
| 33 | + settings: Optional[Dict[str, str]] = None, |
| 34 | + version: Optional[int] = None |
| 35 | + ): |
| 36 | + """Initialize OneSettingsResponse with configuration data. |
| 37 | +
|
| 38 | + Args: |
| 39 | + etag (Optional[str], optional): ETag header value for caching. Defaults to None. |
| 40 | + refresh_interval (int, optional): Refresh interval in seconds. |
| 41 | + Defaults to _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS. |
| 42 | + settings (Optional[Dict[str, str]], optional): Configuration settings dictionary. |
| 43 | + Defaults to empty dict if None. |
| 44 | + version (Optional[int], optional): Configuration version number. Defaults to None. |
| 45 | + """ |
| 46 | + self.etag = etag |
| 47 | + self.refresh_interval = refresh_interval |
| 48 | + self.settings = settings or {} |
| 49 | + self.version = version |
| 50 | + |
| 51 | + |
| 52 | +def make_onesettings_request(url: str, query_dict: Optional[Dict[str, str]] = None, |
| 53 | + headers: Optional[Dict[str, str]] = None) -> OneSettingsResponse: |
| 54 | + """Make an HTTP request to the OneSettings API and parse the response. |
| 55 | +
|
| 56 | + This function handles the complete OneSettings request lifecycle including: |
| 57 | + - Making the HTTP GET request with optional query parameters and headers |
| 58 | + - Error handling for network, HTTP, and JSON parsing errors |
| 59 | + - Parsing the response into a structured OneSettingsResponse object |
| 60 | +
|
| 61 | + :param url: The OneSettings API endpoint URL to request |
| 62 | + :type url: str |
| 63 | + :param query_dict: Query parameters to include |
| 64 | + in the request URL. Defaults to None. |
| 65 | + :type query_dict: Optional[Dict[str, str]] |
| 66 | + :param headers: HTTP headers to include in the request. |
| 67 | + Common headers include 'If-None-Match' for ETag caching. Defaults to None. |
| 68 | + :type headers: Optional[Dict[str, str]] |
| 69 | +
|
| 70 | + :return: Parsed response containing configuration data and metadata. |
| 71 | + Returns a default response object if the request fails. |
| 72 | + :rtype: OneSettingsResponse |
| 73 | +
|
| 74 | + Raises: |
| 75 | + Does not raise exceptions - all errors are caught and logged, returning a |
| 76 | + default OneSettingsResponse object. |
| 77 | + """ |
| 78 | + query_dict = query_dict or {} |
| 79 | + headers = headers or {} |
| 80 | + |
| 81 | + try: |
| 82 | + result = requests.get(url, params=query_dict, headers=headers, timeout=10) |
| 83 | + result.raise_for_status() # Raises an exception for 4XX/5XX responses |
| 84 | + |
| 85 | + return _parse_onesettings_response(result) |
| 86 | + except requests.exceptions.RequestException as ex: |
| 87 | + logger.warning("Failed to fetch configuration from OneSettings: %s", str(ex)) |
| 88 | + return OneSettingsResponse() |
| 89 | + except json.JSONDecodeError as ex: |
| 90 | + logger.warning("Failed to parse OneSettings response: %s", str(ex)) |
| 91 | + return OneSettingsResponse() |
| 92 | + except Exception as ex: # pylint: disable=broad-exception-caught |
| 93 | + logger.warning("Unexpected error while fetching configuration: %s", str(ex)) |
| 94 | + return OneSettingsResponse() |
| 95 | + |
| 96 | +def _parse_onesettings_response(response: requests.Response) -> OneSettingsResponse: |
| 97 | + """Parse an HTTP response from OneSettings into a structured response object. |
| 98 | +
|
| 99 | + This function processes the OneSettings API response and extracts: |
| 100 | + - HTTP headers (ETag, refresh interval) |
| 101 | + - Response body (configuration settings, version) |
| 102 | + - Status code handling (200, 304, 4xx, 5xx) |
| 103 | +
|
| 104 | + The parser handles different HTTP status codes appropriately: |
| 105 | + - 200: New configuration data available, parse settings and version |
| 106 | + - 304: Not modified, configuration unchanged (empty settings) |
| 107 | + - 400/404/414/500: Various error conditions, logged with warnings |
| 108 | +
|
| 109 | + :param response: HTTP response object from the requests library containing |
| 110 | + the OneSettings API response with headers, status code, and content. |
| 111 | + :type response: requests.Response |
| 112 | +
|
| 113 | + :return: Structured response object containing: |
| 114 | + - etag: ETag header value for conditional requests |
| 115 | + - refresh_interval: Next refresh interval from headers |
| 116 | + - settings: Configuration key-value pairs (empty for 304/errors) |
| 117 | + - version: Configuration version number for change tracking |
| 118 | + :rtype: OneSettingsResponse |
| 119 | + Note: |
| 120 | + This function logs warnings for various error conditions but does not |
| 121 | + raise exceptions, always returning a valid OneSettingsResponse object. |
| 122 | + """ |
| 123 | + etag = None |
| 124 | + refresh_interval = _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS |
| 125 | + settings: Dict[str, str] = {} |
| 126 | + status_code = response.status_code |
| 127 | + version = None |
| 128 | + |
| 129 | + # Extract headers |
| 130 | + if response.headers: |
| 131 | + etag = response.headers.get("ETag") |
| 132 | + refresh_interval_header = response.headers.get("x-ms-onesetinterval") |
| 133 | + try: |
| 134 | + refresh_interval = int(refresh_interval_header) if refresh_interval_header else refresh_interval |
| 135 | + except (ValueError, TypeError): |
| 136 | + logger.warning("Invalid refresh interval format: %s", refresh_interval_header) |
| 137 | + refresh_interval = _ONE_SETTINGS_DEFAULT_REFRESH_INTERVAL_SECONDS |
| 138 | + |
| 139 | + # Handle different status codes |
| 140 | + if status_code == 304: |
| 141 | + # 304 Not Modified - cache stays the same |
| 142 | + pass |
| 143 | + elif status_code == 200: |
| 144 | + # 200 OK - parse new settings |
| 145 | + if response.content: |
| 146 | + try: |
| 147 | + decoded_string = response.content.decode("utf-8") |
| 148 | + config = json.loads(decoded_string) |
| 149 | + settings = config.get("settings", {}) |
| 150 | + if settings and settings.get(_ONE_SETTINGS_CHANGE_VERSION_KEY) is not None: |
| 151 | + version = int(settings.get(_ONE_SETTINGS_CHANGE_VERSION_KEY)) # type: ignore |
| 152 | + except (UnicodeDecodeError, json.JSONDecodeError) as ex: |
| 153 | + logger.warning("Failed to decode OneSettings response content: %s", str(ex)) |
| 154 | + except ValueError as ex: |
| 155 | + logger.warning("Failed to parse OneSettings change version: %s", str(ex)) |
| 156 | + elif status_code == 400: |
| 157 | + logger.warning("Bad request to OneSettings: %s", response.content) |
| 158 | + elif status_code == 404: |
| 159 | + logger.warning("OneSettings configuration not found: %s", response.content) |
| 160 | + elif status_code == 414: |
| 161 | + logger.warning("OneSettings request URI too long: %s", response.content) |
| 162 | + elif status_code == 500: |
| 163 | + logger.warning("Internal server error from OneSettings: %s", response.content) |
| 164 | + |
| 165 | + return OneSettingsResponse(etag, refresh_interval, settings, version) |
0 commit comments