|
| 1 | +import logging |
| 2 | +from collections.abc import Iterable |
| 3 | +from urllib.parse import urljoin |
| 4 | + |
| 5 | +from requests import RequestException |
| 6 | + |
| 7 | +from sentry.net.http import Session |
| 8 | + |
| 9 | +from . import base |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | +# The timeout for rpc calls, in seconds. |
| 14 | +# We expect these to be very quick, and never want to block more than 2 ms (4 with connect + read). |
| 15 | +RPC_TIMEOUT = 2 / 1000 # timeout in seconds |
| 16 | + |
| 17 | + |
| 18 | +class PbRealtimeMetricsStore(base.RealtimeMetricsStore): |
| 19 | + def __init__(self, target: str): |
| 20 | + self.target = target |
| 21 | + self.session = Session() |
| 22 | + |
| 23 | + def record_project_duration(self, project_id: int, duration: float) -> None: |
| 24 | + url = urljoin(self.target, "/record_spending") |
| 25 | + request = { |
| 26 | + "config_name": "symbolication-native", |
| 27 | + "project_id": project_id, |
| 28 | + "spent": duration, |
| 29 | + } |
| 30 | + try: |
| 31 | + self.session.post( |
| 32 | + url, |
| 33 | + timeout=RPC_TIMEOUT, |
| 34 | + json=request, |
| 35 | + ) |
| 36 | + except RequestException: |
| 37 | + pass |
| 38 | + |
| 39 | + def is_lpq_project(self, project_id: int) -> bool: |
| 40 | + url = urljoin(self.target, "/exceeds_budget") |
| 41 | + request = { |
| 42 | + "config_name": "symbolication-native", |
| 43 | + "project_id": project_id, |
| 44 | + } |
| 45 | + try: |
| 46 | + response = self.session.post( |
| 47 | + url, |
| 48 | + timeout=RPC_TIMEOUT, |
| 49 | + json=request, |
| 50 | + ) |
| 51 | + return response.json()["exceeds_budget"] |
| 52 | + except RequestException: |
| 53 | + return False |
| 54 | + |
| 55 | + # NOTE: The functions below are just default impls copy-pasted from `DummyRealtimeMetricsStore`. |
| 56 | + # They are not used in the actual implementation of recording budget spend, |
| 57 | + # and checking if a project is within its budget. |
| 58 | + |
| 59 | + def validate(self) -> None: |
| 60 | + pass |
| 61 | + |
| 62 | + def projects(self) -> Iterable[int]: |
| 63 | + yield from () |
| 64 | + |
| 65 | + def get_used_budget_for_project(self, project_id: int) -> float: |
| 66 | + return 0.0 |
| 67 | + |
| 68 | + def get_lpq_projects(self) -> set[int]: |
| 69 | + return set() |
| 70 | + |
| 71 | + def add_project_to_lpq(self, project_id: int) -> bool: |
| 72 | + return False |
| 73 | + |
| 74 | + def remove_projects_from_lpq(self, project_ids: set[int]) -> int: |
| 75 | + return 0 |
0 commit comments