Skip to content

Commit

Permalink
Removed last_event_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Feb 19, 2024
1 parent a07cf91 commit 458f5e9
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 128 deletions.
1 change: 0 additions & 1 deletion sentry_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"get_traceparent",
"is_initialized",
"isolation_scope",
"last_event_id",
"new_scope",
"push_scope",
"set_context",
Expand Down
7 changes: 0 additions & 7 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def overload(x):
"get_traceparent",
"is_initialized",
"isolation_scope",
"last_event_id",
"new_scope",
"push_scope",
"set_context",
Expand Down Expand Up @@ -282,12 +281,6 @@ def flush(
return Scope.get_client().flush(timeout=timeout, callback=callback)


@scopemethod
def last_event_id():
# type: () -> Optional[str]
return Scope.get_current_scope().last_event_id()


@scopemethod
def start_span(
span=None, # type: Optional[Span]
Expand Down
22 changes: 1 addition & 21 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ class Scope(object):
"_propagation_context",
"client",
"_type",
"_last_event_id",
)

def __init__(self, ty=None, client=None):
Expand All @@ -168,7 +167,6 @@ def __init__(self, ty=None, client=None):

self._name = None # type: Optional[str]
self._propagation_context = None # type: Optional[Dict[str, Any]]
self._last_event_id = None # type: Optional[str] # deprecated

self.client = NonRecordingClient() # type: sentry_sdk.client.BaseClient

Expand Down Expand Up @@ -281,16 +279,6 @@ def get_global_scope(cls):

return _global_scope

def last_event_id(self):
# type: () -> Optional[str]
"""
.. deprecated:: 2.0.0
This function is deprecated and will be removed in a future release.
Returns the last event ID.
"""
return self._last_event_id

@classmethod
def _merge_scopes(cls, additional_scope=None, additional_scope_kwargs=None):
# type: (Optional[Scope], Optional[Dict[str, Any]]) -> Scope
Expand Down Expand Up @@ -1056,15 +1044,7 @@ def capture_event(self, event, hint=None, scope=None, **scope_kwargs):
"""
scope = Scope._merge_scopes(scope, scope_kwargs)

last_event_id = Scope.get_client().capture_event(
event=event, hint=hint, scope=scope
)

is_transaction = event.get("type") == "transaction"
if last_event_id is not None and not is_transaction:
self._last_event_id = last_event_id

return last_event_id
return Scope.get_client().capture_event(event=event, hint=hint, scope=scope)

def capture_message(self, message, level=None, scope=None, **scope_kwargs):
# type: (str, Optional[str], Optional[Scope], Any) -> Optional[str]
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/django/myapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def mylogin(request):

@csrf_exempt
def handler500(request):
return HttpResponseServerError("Sentry error: %s" % sentry_sdk.last_event_id())
return HttpResponseServerError("Sentry error.")


class ClassBasedView(ListView):
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/falcon/test_falcon.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def on_get(self, req, resp):

def http500_handler(ex, req, resp, params):
sentry_sdk.capture_exception(ex)
resp.media = {"message": "Sentry error: %s" % sentry_sdk.last_event_id()}
resp.media = {"message": "Sentry error."}

app.add_error_handler(Exception, http500_handler)

Expand Down
10 changes: 3 additions & 7 deletions tests/integrations/flask/test_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
configure_scope,
capture_message,
capture_exception,
last_event_id,
Hub,
)
from sentry_sdk.integrations.logging import LoggingIntegration
Expand Down Expand Up @@ -599,7 +598,7 @@ def wsgi_app(environ, start_response):
assert event["exception"]["values"][0]["mechanism"]["type"] == "wsgi"


def test_500(sentry_init, capture_events, app):
def test_500(sentry_init, app):
sentry_init(integrations=[flask_sentry.FlaskIntegration()])

app.debug = False
Expand All @@ -611,15 +610,12 @@ def index():

@app.errorhandler(500)
def error_handler(err):
return "Sentry error: %s" % last_event_id()

events = capture_events()
return "Sentry error."

client = app.test_client()
response = client.get("/")

(event,) = events
assert response.data.decode("utf-8") == "Sentry error: %s" % event["event_id"]
assert response.data.decode("utf-8") == "Sentry error."


def test_error_in_errorhandler(sentry_init, capture_events, app):
Expand Down
12 changes: 3 additions & 9 deletions tests/integrations/quart/test_quart.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
configure_scope,
capture_message,
capture_exception,
last_event_id,
)
from sentry_sdk.integrations.logging import LoggingIntegration
import sentry_sdk.integrations.quart as quart_sentry
Expand Down Expand Up @@ -313,7 +312,7 @@ def foo():


@pytest.mark.asyncio
async def test_500(sentry_init, capture_events, app):
async def test_500(sentry_init, app):
sentry_init(integrations=[quart_sentry.QuartIntegration()])

@app.route("/")
Expand All @@ -322,17 +321,12 @@ async def index():

@app.errorhandler(500)
async def error_handler(err):
return "Sentry error: %s" % last_event_id()

events = capture_events()
return "Sentry error."

client = app.test_client()
response = await client.get("/")

(event,) = events
assert (await response.get_data(as_text=True)) == "Sentry error: %s" % event[
"event_id"
]
assert (await response.get_data(as_text=True)) == "Sentry error."


@pytest.mark.asyncio
Expand Down
26 changes: 1 addition & 25 deletions tests/integrations/starlette/test_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pytest

from sentry_sdk import last_event_id, capture_exception, capture_message
from sentry_sdk import capture_message
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.integrations.starlette import (
StarletteIntegration,
Expand Down Expand Up @@ -815,30 +815,6 @@ def test_middleware_partial_receive_send(sentry_init, capture_events):
idx += 1


def test_last_event_id(sentry_init, capture_events):
sentry_init(
integrations=[StarletteIntegration()],
)
events = capture_events()

def handler(request, exc):
capture_exception(exc)
return starlette.responses.PlainTextResponse(last_event_id(), status_code=500)

app = starlette_app_factory(debug=False)
app.add_exception_handler(500, handler)

client = TestClient(SentryAsgiMiddleware(app), raise_server_exceptions=False)
response = client.get("/custom_error")
assert response.status_code == 500

event = events[0]
assert response.content.strip().decode("ascii") == event["event_id"]
(exception,) = event["exception"]["values"]
assert exception["type"] == "Exception"
assert exception["value"] == "Too Hot"


def test_legacy_setup(
sentry_init,
capture_events,
Expand Down
28 changes: 1 addition & 27 deletions tests/integrations/starlite/test_starlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import pytest

from sentry_sdk import capture_exception, capture_message, last_event_id
from sentry_sdk import capture_message
from sentry_sdk.integrations.starlite import StarliteIntegration

from typing import Any, Dict

import starlite
from starlite import AbstractMiddleware, LoggingConfig, Starlite, get, Controller
from starlite.middleware import LoggingMiddlewareConfig, RateLimitConfig
from starlite.middleware.session.memory_backend import MemoryBackendConfig
from starlite.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
from starlite.testing import TestClient


Expand Down Expand Up @@ -291,27 +289,3 @@ def test_middleware_partial_receive_send(sentry_init, capture_events):
assert span["op"] == expected[idx]["op"]
assert span["description"].startswith(expected[idx]["description"])
assert span["tags"] == expected[idx]["tags"]


def test_last_event_id(sentry_init, capture_events):
sentry_init(
integrations=[StarliteIntegration()],
)
events = capture_events()

def handler(request, exc):
capture_exception(exc)
return starlite.response.Response(last_event_id(), status_code=500)

app = starlite_app_factory(
debug=False, exception_handlers={HTTP_500_INTERNAL_SERVER_ERROR: handler}
)

client = TestClient(app, raise_server_exceptions=False)
response = client.get("/custom_error")
assert response.status_code == 500
event = events[-1]
assert response.content.strip().decode("ascii").strip('"') == event["event_id"]
(exception,) = event["exception"]["values"]
assert exception["type"] == "Exception"
assert exception["value"] == "Too Hot"
10 changes: 3 additions & 7 deletions tests/integrations/trytond/test_trytond.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from trytond.wsgi import app as trytond_app

from werkzeug.test import Client
from sentry_sdk import last_event_id
from sentry_sdk.integrations.trytond import TrytondWSGIIntegration


Expand Down Expand Up @@ -79,13 +78,12 @@ def _(request):
@pytest.mark.skipif(
trytond.__version__.split(".") < ["5", "4"], reason="At least Trytond-5.4 required"
)
def test_rpc_error_page(sentry_init, app, capture_events, get_client):
def test_rpc_error_page(sentry_init, app, get_client):
"""Test that, after initializing the Trytond-SentrySDK integration
a custom error handler can be registered to the Trytond WSGI app so as to
inform the event identifiers to the Tryton RPC client"""

sentry_init(integrations=[TrytondWSGIIntegration()])
events = capture_events()

@app.route("/rpcerror", methods=["POST"])
def _(request):
Expand All @@ -96,8 +94,7 @@ def _(app, request, e):
if isinstance(e, TrytondBaseException):
return
else:
event_id = last_event_id()
data = TrytondUserError(str(event_id), str(e))
data = TrytondUserError("Sentry error.", str(e))
return app.make_response(request, data)

client = get_client()
Expand All @@ -121,9 +118,8 @@ def _(app, request, e):
"/rpcerror", content_type="application/json", data=json.dumps(_data)
)

(event,) = events
(content, status, headers) = response
data = json.loads(next(content))
assert status == "200 OK"
assert headers.get("Content-Type") == "application/json"
assert data == dict(id=42, error=["UserError", [event["event_id"], "foo", None]])
assert data == dict(id=42, error=["UserError", ["Sentry error.", "foo", None]])
22 changes: 0 additions & 22 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
capture_message,
start_transaction,
add_breadcrumb,
last_event_id,
Hub,
)
from sentry_sdk.integrations import (
Expand Down Expand Up @@ -108,27 +107,6 @@ def test_auto_enabling_integrations_catches_import_error(sentry_init, caplog):
), "Problem with checking auto enabling {}".format(import_string)


def test_event_id(sentry_init, capture_events):
sentry_init()
events = capture_events()

try:
raise ValueError("aha!")
except Exception:
event_id = capture_exception()
int(event_id, 16)
assert len(event_id) == 32

(event,) = events
assert event["event_id"] == event_id
assert last_event_id() == event_id

new_event_id = Hub.current.capture_event({"type": "transaction"})
assert new_event_id is not None
assert new_event_id != event_id
assert last_event_id() == event_id


def test_generic_mechanism(sentry_init, capture_events):
sentry_init()
events = capture_events()
Expand Down

0 comments on commit 458f5e9

Please sign in to comment.