-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hook up peanutbutter
as an LPQ backend
#69187
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9701c70
Hook up `peanutbutter` as an LPQ backend
Swatinem 0bd308e
Merge branch 'master' into swatinem/peanutbutter-http
loewenheim 19027c7
Enable pb for tests
loewenheim 23a6a8e
Only enable peanutbutter in tests
loewenheim c106558
Merge branch 'master' into swatinem/peanutbutter-http
loewenheim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import logging | ||
from collections.abc import Iterable | ||
from urllib.parse import urljoin | ||
|
||
from requests import RequestException | ||
|
||
from sentry.net.http import Session | ||
|
||
from . import base | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# The timeout for rpc calls, in seconds. | ||
# We expect these to be very quick, and never want to block more than 2 ms (4 with connect + read). | ||
RPC_TIMEOUT = 2 / 1000 # timeout in seconds | ||
|
||
|
||
class PbRealtimeMetricsStore(base.RealtimeMetricsStore): | ||
def __init__(self, target: str): | ||
self.target = target | ||
self.session = Session() | ||
|
||
def record_project_duration(self, project_id: int, duration: float) -> None: | ||
url = urljoin(self.target, "/record_spending") | ||
request = { | ||
"config_name": "symbolication-native", | ||
"project_id": project_id, | ||
"spent": duration, | ||
} | ||
try: | ||
self.session.post( | ||
url, | ||
timeout=RPC_TIMEOUT, | ||
json=request, | ||
) | ||
except RequestException: | ||
pass | ||
|
||
def is_lpq_project(self, project_id: int) -> bool: | ||
url = urljoin(self.target, "/exceeds_budget") | ||
request = { | ||
"config_name": "symbolication-native", | ||
"project_id": project_id, | ||
} | ||
try: | ||
response = self.session.post( | ||
url, | ||
timeout=RPC_TIMEOUT, | ||
json=request, | ||
) | ||
return response.json()["exceeds_budget"] | ||
except RequestException: | ||
return False | ||
|
||
# NOTE: The functions below are just default impls copy-pasted from `DummyRealtimeMetricsStore`. | ||
# They are not used in the actual implementation of recording budget spend, | ||
# and checking if a project is within its budget. | ||
|
||
def validate(self) -> None: | ||
pass | ||
|
||
def projects(self) -> Iterable[int]: | ||
yield from () | ||
|
||
def get_used_budget_for_project(self, project_id: int) -> float: | ||
return 0.0 | ||
|
||
def get_lpq_projects(self) -> set[int]: | ||
return set() | ||
|
||
def add_project_to_lpq(self, project_id: int) -> bool: | ||
return False | ||
|
||
def remove_projects_from_lpq(self, project_ids: set[int]) -> int: | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from math import floor | ||
from random import random | ||
|
||
from sentry.processing.realtime_metrics.pb import PbRealtimeMetricsStore | ||
|
||
|
||
def test_invalid_target(): | ||
# there is no grpc service at that addr | ||
store = PbRealtimeMetricsStore(target="http://localhost:12345") | ||
store.record_project_duration(1, 123456789) | ||
assert not store.is_lpq_project(1) | ||
|
||
|
||
def test_pb_works(): | ||
store = PbRealtimeMetricsStore(target="http://localhost:4433") | ||
|
||
project_id = floor(random() * (1 << 32)) | ||
assert not store.is_lpq_project(project_id) | ||
store.record_project_duration(project_id, 123456789) | ||
assert store.is_lpq_project(project_id) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@asottile-sentry related to your comment in #67232 (comment):
I removed the wrapper to start this server on demand. but that also means that needs to be running in the background, which is a bit overkill as this is otherwise not used for a local devserver.
Although its also very low overhead.