|
5 | 5 | import threading |
6 | 6 | from dataclasses import dataclass |
7 | 7 | from contextlib import contextmanager |
8 | | -from typing import Generator |
| 8 | +from typing import Generator, Optional |
9 | 9 | import logging |
| 10 | +from requests.adapters import HTTPAdapter |
| 11 | +from databricks.sql.auth.retry import DatabricksRetryPolicy, CommandType |
10 | 12 |
|
11 | 13 | logger = logging.getLogger(__name__) |
12 | 14 |
|
@@ -81,3 +83,70 @@ def execute( |
81 | 83 |
|
82 | 84 | def close(self): |
83 | 85 | self.session.close() |
| 86 | + |
| 87 | + |
| 88 | +class TelemetryHTTPAdapter(HTTPAdapter): |
| 89 | + """ |
| 90 | + Custom HTTP adapter to prepare our DatabricksRetryPolicy before each request. |
| 91 | + This ensures the retry timer is started and the command type is set correctly, |
| 92 | + allowing the policy to manage its state for the duration of the request retries. |
| 93 | + """ |
| 94 | + |
| 95 | + def send(self, request, **kwargs): |
| 96 | + self.max_retries.command_type = CommandType.OTHER |
| 97 | + self.max_retries.start_retry_timer() |
| 98 | + return super().send(request, **kwargs) |
| 99 | + |
| 100 | + |
| 101 | +class TelemetryHttpClient: # TODO: Unify all the http clients in the PySQL Connector |
| 102 | + """Singleton HTTP client for sending telemetry data.""" |
| 103 | + |
| 104 | + _instance: Optional["TelemetryHttpClient"] = None |
| 105 | + _lock = threading.Lock() |
| 106 | + |
| 107 | + TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_COUNT = 3 |
| 108 | + TELEMETRY_RETRY_DELAY_MIN = 1.0 |
| 109 | + TELEMETRY_RETRY_DELAY_MAX = 10.0 |
| 110 | + TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_DURATION = 30.0 |
| 111 | + |
| 112 | + def __init__(self): |
| 113 | + """Initializes the session and mounts the custom retry adapter.""" |
| 114 | + retry_policy = DatabricksRetryPolicy( |
| 115 | + delay_min=self.TELEMETRY_RETRY_DELAY_MIN, |
| 116 | + delay_max=self.TELEMETRY_RETRY_DELAY_MAX, |
| 117 | + stop_after_attempts_count=self.TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_COUNT, |
| 118 | + stop_after_attempts_duration=self.TELEMETRY_RETRY_STOP_AFTER_ATTEMPTS_DURATION, |
| 119 | + delay_default=1.0, |
| 120 | + force_dangerous_codes=[], |
| 121 | + ) |
| 122 | + adapter = TelemetryHTTPAdapter(max_retries=retry_policy) |
| 123 | + self.session = requests.Session() |
| 124 | + self.session.mount("https://", adapter) |
| 125 | + self.session.mount("http://", adapter) |
| 126 | + |
| 127 | + @classmethod |
| 128 | + def get_instance(cls) -> "TelemetryHttpClient": |
| 129 | + """Get the singleton instance of the TelemetryHttpClient.""" |
| 130 | + if cls._instance is None: |
| 131 | + with cls._lock: |
| 132 | + if cls._instance is None: |
| 133 | + logger.debug("Initializing singleton TelemetryHttpClient") |
| 134 | + cls._instance = TelemetryHttpClient() |
| 135 | + return cls._instance |
| 136 | + |
| 137 | + def post(self, url: str, **kwargs) -> requests.Response: |
| 138 | + """ |
| 139 | + Executes a POST request using the configured session. |
| 140 | +
|
| 141 | + This is a blocking call intended to be run in a background thread. |
| 142 | + """ |
| 143 | + logger.debug("Executing telemetry POST request to: %s", url) |
| 144 | + return self.session.post(url, **kwargs) |
| 145 | + |
| 146 | + def close(self): |
| 147 | + """Closes the underlying requests.Session.""" |
| 148 | + logger.debug("Closing TelemetryHttpClient session.") |
| 149 | + self.session.close() |
| 150 | + # Clear the instance to allow for re-initialization if needed |
| 151 | + with TelemetryHttpClient._lock: |
| 152 | + TelemetryHttpClient._instance = None |
0 commit comments