Skip to content

Commit

Permalink
Fix and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Nov 23, 2023
1 parent 2c0687c commit d1e0cb8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 25 deletions.
1 change: 1 addition & 0 deletions sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ def _capture_envelope(envelope):
],
)

self.spotlight = None
if self.options["spotlight"]:
self.spotlight = setup_spotlight(self.options)

Check warning on line 274 in sentry_sdk/client.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/client.py#L274

Added line #L274 was not covered by tests

Expand Down
22 changes: 8 additions & 14 deletions sentry_sdk/spotlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,16 @@ def capture_envelope(self, envelope):
logger.exception(str(e))

Check warning on line 36 in sentry_sdk/spotlight.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/spotlight.py#L34-L36

Added lines #L34 - L36 were not covered by tests


instance = None


def setup_spotlight(options):
# type: (Dict[str, Any]) -> Optional[SpotlightSidecar]
global instance

if instance is None:
url = options["spotlight"]
if isinstance(url, str):
pass
elif url is True:
url = "http://localhost:8969/stream"
else:
return None
url = options["spotlight"]

Check warning on line 42 in sentry_sdk/spotlight.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/spotlight.py#L42

Added line #L42 was not covered by tests

instance = SpotlightSidecar(url)
if isinstance(url, str):
pass

Check warning on line 45 in sentry_sdk/spotlight.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/spotlight.py#L45

Added line #L45 was not covered by tests
elif url is True:
url = "http://localhost:8969/stream"

Check warning on line 47 in sentry_sdk/spotlight.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/spotlight.py#L47

Added line #L47 was not covered by tests
else:
return None

Check warning on line 49 in sentry_sdk/spotlight.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/spotlight.py#L49

Added line #L49 was not covered by tests

return instance
return SpotlightSidecar(url)

Check warning on line 51 in sentry_sdk/spotlight.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/spotlight.py#L51

Added line #L51 was not covered by tests
58 changes: 48 additions & 10 deletions tests/test_spotlight.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,56 @@
import pytest

from sentry_sdk import Client
from sentry_sdk import Hub, capture_exception


def test_send_to_spotlight(make_client):
client = make_client(
spotlight=False,
)
assert client.spotlight is None
@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)

@pytest.fixture
def make_client(request):
def inner(**kwargs):
return Client("http://foobar@test.com/132", **kwargs)
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://foobar@test.com/132")

spotlight = Hub.current.client.spotlight
assert spotlight is not None
assert spotlight.url == "http://foobar@test.com/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!"
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ basepython =
# some random Python 3 binary, but then you get guaranteed mismatches with
# CI. Other tools such as mypy and black have options that pin the Python
# version.
linters: python3.12
linters: python3.11

commands =
{py3.7,py3.8}-boto3: pip install urllib3<2.0.0
Expand Down

0 comments on commit d1e0cb8

Please sign in to comment.