-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Send to Spotlight sidecar (#2524)
Add Spotlight option to SDK. This allows sending envelopes to the Spotlight sidecar. --------- Co-authored-by: Neel Shah <[email protected]> Co-authored-by: Anton Pirker <[email protected]>
- Loading branch information
1 parent
ea55387
commit 088431e
Showing
4 changed files
with
127 additions
and
3 deletions.
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
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,51 @@ | ||
import io | ||
import urllib3 | ||
|
||
from sentry_sdk._types import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Any | ||
from typing import Dict | ||
from typing import Optional | ||
|
||
from sentry_sdk.utils import logger | ||
from sentry_sdk.envelope import Envelope | ||
|
||
|
||
class SpotlightClient(object): | ||
def __init__(self, url): | ||
# type: (str) -> None | ||
self.url = url | ||
self.http = urllib3.PoolManager() | ||
|
||
def capture_envelope(self, envelope): | ||
# type: (Envelope) -> None | ||
body = io.BytesIO() | ||
envelope.serialize_into(body) | ||
try: | ||
req = self.http.request( | ||
url=self.url, | ||
body=body.getvalue(), | ||
method="POST", | ||
headers={ | ||
"Content-Type": "application/x-sentry-envelope", | ||
}, | ||
) | ||
req.close() | ||
except Exception as e: | ||
logger.exception(str(e)) | ||
|
||
|
||
def setup_spotlight(options): | ||
# type: (Dict[str, Any]) -> Optional[SpotlightClient] | ||
|
||
url = options.get("spotlight") | ||
|
||
if isinstance(url, str): | ||
pass | ||
elif url is True: | ||
url = "http://localhost:8969/stream" | ||
else: | ||
return None | ||
|
||
return SpotlightClient(url) |
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,56 @@ | ||
import pytest | ||
|
||
from sentry_sdk import Hub, capture_exception | ||
|
||
|
||
@pytest.fixture | ||
def capture_spotlight_envelopes(monkeypatch): | ||
def inner(): | ||
envelopes = [] | ||
test_spotlight = Hub.current.client.spotlight | ||
old_capture_envelope = test_spotlight.capture_envelope | ||
|
||
def append_envelope(envelope): | ||
envelopes.append(envelope) | ||
return old_capture_envelope(envelope) | ||
|
||
monkeypatch.setattr(test_spotlight, "capture_envelope", append_envelope) | ||
return envelopes | ||
|
||
return inner | ||
|
||
|
||
def test_spotlight_off_by_default(sentry_init): | ||
sentry_init() | ||
assert Hub.current.client.spotlight is None | ||
|
||
|
||
def test_spotlight_default_url(sentry_init): | ||
sentry_init(spotlight=True) | ||
|
||
spotlight = Hub.current.client.spotlight | ||
assert spotlight is not None | ||
assert spotlight.url == "http://localhost:8969/stream" | ||
|
||
|
||
def test_spotlight_custom_url(sentry_init): | ||
sentry_init(spotlight="http://[email protected]/132") | ||
|
||
spotlight = Hub.current.client.spotlight | ||
assert spotlight is not None | ||
assert spotlight.url == "http://[email protected]/132" | ||
|
||
|
||
def test_spotlight_envelope(sentry_init, capture_spotlight_envelopes): | ||
sentry_init(spotlight=True) | ||
envelopes = capture_spotlight_envelopes() | ||
|
||
try: | ||
raise ValueError("aha!") | ||
except Exception: | ||
capture_exception() | ||
|
||
(envelope,) = envelopes | ||
payload = envelope.items[0].payload.json | ||
|
||
assert payload["exception"]["values"][0]["value"] == "aha!" |