|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from ocp_resources.resource import Resource |
| 6 | +from ocp_utilities.monitoring import Prometheus |
| 7 | +from timeout_sampler import TimeoutExpiredError, TimeoutSampler |
| 8 | + |
| 9 | +from ocp_scale_utilities.constants import TIMEOUT_5MIN, TIMEOUT_30SEC |
| 10 | + |
| 11 | +LOGGER = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +class MonitorResourceAPIServerRequests: |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + prometheus: Prometheus, |
| 18 | + resource_class: type[Resource], |
| 19 | + idle_requests_value: float, |
| 20 | + time_duration_seconds: int = TIMEOUT_5MIN, |
| 21 | + ): |
| 22 | + """ |
| 23 | + Monitor API Server Requests for a particular Resource |
| 24 | +
|
| 25 | + Args: |
| 26 | + prometheus (Prometheus): Prometheus object from ocp_utilities |
| 27 | + resource_class (Resource): Resource class to monitor |
| 28 | + time_duration_seconds (int, optional): Time duration to use with prometheus query |
| 29 | + idle_requests_value (float, optional): Minimum value indicating an 'idle' state |
| 30 | + """ |
| 31 | + self.prometheus = prometheus |
| 32 | + self.resource_class = resource_class |
| 33 | + self.time_duration_seconds = time_duration_seconds |
| 34 | + self.idle_requests_value = idle_requests_value |
| 35 | + |
| 36 | + self.apiserver_requests_query = ( |
| 37 | + "sum by (resource) (rate(apiserver_request_total{" |
| 38 | + f'group="{self.resource_class.api_group}",' |
| 39 | + f'resource="{self.resource_class.kind.lower()}s"' |
| 40 | + f"}}[{self.time_duration_seconds}s]))" |
| 41 | + ) |
| 42 | + |
| 43 | + def _initial_wait(self) -> None: |
| 44 | + """ |
| 45 | + The initial state is unknown, and will be unknown, it cannot be guaranteed. |
| 46 | + The code calling this could be the first code that upsets the cluster, or not. |
| 47 | + Wait for the cluster to either stay silent or become noisy. |
| 48 | + """ |
| 49 | + initial_silence_count = 0 |
| 50 | + initial_noise_count = 0 |
| 51 | + sampler = TimeoutSampler( |
| 52 | + wait_timeout=TIMEOUT_30SEC, |
| 53 | + sleep=5, |
| 54 | + func=self.prometheus.query_sampler, |
| 55 | + query=self.apiserver_requests_query, |
| 56 | + ) |
| 57 | + try: |
| 58 | + for sample in sampler: |
| 59 | + if sample: |
| 60 | + value = float(sample[0]["value"][1]) |
| 61 | + if value < self.idle_requests_value: |
| 62 | + initial_noise_count = 0 |
| 63 | + initial_silence_count += 1 |
| 64 | + else: |
| 65 | + initial_silence_count = 0 |
| 66 | + initial_noise_count += 1 |
| 67 | + |
| 68 | + if initial_silence_count > 5 or initial_noise_count > 2: |
| 69 | + break |
| 70 | + except TimeoutExpiredError: |
| 71 | + pass |
| 72 | + |
| 73 | + def wait_for_idle(self) -> None: |
| 74 | + """ |
| 75 | + Wait for 'idle' cluster state based on provided Resource |
| 76 | + """ |
| 77 | + self._initial_wait() |
| 78 | + |
| 79 | + sampler = TimeoutSampler( |
| 80 | + wait_timeout=self.time_duration_seconds * 2, |
| 81 | + sleep=5, |
| 82 | + func=self.prometheus.query_sampler, |
| 83 | + query=self.apiserver_requests_query, |
| 84 | + ) |
| 85 | + sample = None |
| 86 | + try: |
| 87 | + for sample in sampler: |
| 88 | + if sample and float(sample[0]["value"][1]) < self.idle_requests_value: |
| 89 | + return |
| 90 | + except TimeoutExpiredError: |
| 91 | + LOGGER.error( |
| 92 | + f"Metric value: {sample} of {self.apiserver_requests_query!r} " |
| 93 | + f"is not below minimum: {self.idle_requests_value}" |
| 94 | + ) |
| 95 | + raise |
0 commit comments