Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
szokeasaurusrex committed Sep 23, 2024
1 parent 0196f36 commit 6d5d714
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 45 deletions.
4 changes: 0 additions & 4 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys
import weakref
from functools import wraps

import sentry_sdk
from sentry_sdk.api import continue_trace
Expand Down Expand Up @@ -157,14 +156,11 @@ async def sentry_app_handle(self, request, *args, **kwargs):

old_urldispatcher_resolve = UrlDispatcher.resolve

@wraps(old_urldispatcher_resolve)
async def sentry_urldispatcher_resolve(self, request):
# type: (UrlDispatcher, Request) -> UrlMappingMatchInfo
rv = await old_urldispatcher_resolve(self, request)

integration = sentry_sdk.get_client().get_integration(AioHttpIntegration)
if integration is None:
return rv

name = None

Expand Down
8 changes: 5 additions & 3 deletions sentry_sdk/integrations/anthropic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
package_version,
)
Expand Down Expand Up @@ -77,11 +78,10 @@ def _calculate_token_usage(result, span):
def _wrap_message_create(f):
# type: (Any) -> Any
@wraps(f)
@ensure_integration_enabled(AnthropicIntegration, f)
def _sentry_patched_create(*args, **kwargs):
# type: (*Any, **Any) -> Any
integration = sentry_sdk.get_client().get_integration(AnthropicIntegration)

if integration is None or "messages" not in kwargs:
if "messages" not in kwargs:
return f(*args, **kwargs)

try:
Expand All @@ -106,6 +106,8 @@ def _sentry_patched_create(*args, **kwargs):
span.__exit__(None, None, None)
raise exc from None

integration = sentry_sdk.get_client().get_integration(AnthropicIntegration)

with capture_internal_exceptions():
span.set_data(SPANDATA.AI_MODEL_ID, model)
span.set_data(SPANDATA.AI_STREAMING, False)
Expand Down
9 changes: 4 additions & 5 deletions sentry_sdk/integrations/atexit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import sentry_sdk
from sentry_sdk.utils import logger
from sentry_sdk.integrations import Integration
from sentry_sdk.utils import ensure_integration_enabled

from typing import TYPE_CHECKING

if TYPE_CHECKING:
Expand Down Expand Up @@ -42,16 +44,13 @@ def __init__(self, callback=None):
def setup_once():
# type: () -> None
@atexit.register
@ensure_integration_enabled(AtexitIntegration)
def _shutdown():
# type: () -> None
logger.debug("atexit: got shutdown signal")
client = sentry_sdk.get_client()
integration = client.get_integration(AtexitIntegration)

if integration is None:
return

logger.debug("atexit: got shutdown signal")
logger.debug("atexit: shutting down client")
sentry_sdk.get_isolation_scope().end_session()

client.close(callback=integration.callback)
12 changes: 4 additions & 8 deletions sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import functools
import json
import re
import sys
Expand Down Expand Up @@ -71,7 +70,7 @@ def sentry_init_error(*args, **kwargs):

def _wrap_handler(handler):
# type: (F) -> F
@functools.wraps(handler)
@ensure_integration_enabled(AwsLambdaIntegration, handler)
def sentry_handler(aws_event, aws_context, *args, **kwargs):
# type: (Any, Any, *Any, **Any) -> Any

Expand All @@ -85,12 +84,6 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
# will be the same for all events in the list, since they're all hitting
# the lambda in the same request.)

client = sentry_sdk.get_client()
integration = client.get_integration(AwsLambdaIntegration)

if integration is None:
return handler(aws_event, aws_context, *args, **kwargs)

if isinstance(aws_event, list) and len(aws_event) >= 1:
request_data = aws_event[0]
batch_size = len(aws_event)
Expand All @@ -104,6 +97,9 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
# this is empty
request_data = {}

client = sentry_sdk.get_client()
integration = client.get_integration(AwsLambdaIntegration)

configured_time = aws_context.get_remaining_time_in_millis()

with sentry_sdk.isolation_scope() as scope:
Expand Down
6 changes: 1 addition & 5 deletions sentry_sdk/integrations/bottle.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import functools

import sentry_sdk
from sentry_sdk.tracing import SOURCE_FOR_STYLE
from sentry_sdk.utils import (
Expand Down Expand Up @@ -83,12 +81,10 @@ def sentry_patched_wsgi_app(self, environ, start_response):

old_handle = Bottle._handle

@functools.wraps(old_handle)
@ensure_integration_enabled(BottleIntegration, old_handle)
def _patched_handle(self, environ):
# type: (Bottle, Dict[str, Any]) -> Any
integration = sentry_sdk.get_client().get_integration(BottleIntegration)
if integration is None:
return old_handle(self, environ)

scope = sentry_sdk.get_isolation_scope()
scope._name = "bottle"
Expand Down
6 changes: 2 additions & 4 deletions sentry_sdk/integrations/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,13 @@ def __exit__(self, exc_type, exc_value, traceback):
def _wrap_task_run(f):
# type: (F) -> F
@wraps(f)
@ensure_integration_enabled(CeleryIntegration, f)
def apply_async(*args, **kwargs):
# type: (*Any, **Any) -> Any
# Note: kwargs can contain headers=None, so no setdefault!
# Unsure which backend though.
integration = sentry_sdk.get_client().get_integration(CeleryIntegration)
if integration is None:
return f(*args, **kwargs)

kwarg_headers = kwargs.get("headers") or {}
integration = sentry_sdk.get_client().get_integration(CeleryIntegration)
propagate_traces = kwarg_headers.pop(
"sentry-propagate-traces", integration.propagate_traces
)
Expand Down
24 changes: 13 additions & 11 deletions sentry_sdk/integrations/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
import sentry_sdk
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.integrations import DidNotEnable, Integration
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
from sentry_sdk.utils import (
capture_internal_exceptions,
event_from_exception,
ensure_integration_enabled,
)

try:
from cohere.client import Client
Expand Down Expand Up @@ -130,15 +134,13 @@ def collect_chat_response_fields(span, res, include_pii):
set_data_normalized(span, "ai.warnings", res.meta.warnings)

@wraps(f)
@ensure_integration_enabled(CohereIntegration, f)
def new_chat(*args, **kwargs):
# type: (*Any, **Any) -> Any
integration = sentry_sdk.get_client().get_integration(CohereIntegration)
if "message" not in kwargs:
return f(*args, **kwargs)

if (
integration is None
or "message" not in kwargs
or not isinstance(kwargs.get("message"), str)
):
if not isinstance(kwargs.get("message"), str):
return f(*args, **kwargs)

message = kwargs.get("message")
Expand All @@ -156,6 +158,8 @@ def new_chat(*args, **kwargs):
span.__exit__(None, None, None)
raise e from None

integration = sentry_sdk.get_client().get_integration(CohereIntegration)

with capture_internal_exceptions():
if should_send_default_pii() and integration.include_prompts:
set_data_normalized(
Expand Down Expand Up @@ -223,17 +227,15 @@ def _wrap_embed(f):
# type: (Callable[..., Any]) -> Callable[..., Any]

@wraps(f)
@ensure_integration_enabled(CohereIntegration, f)
def new_embed(*args, **kwargs):
# type: (*Any, **Any) -> Any
integration = sentry_sdk.get_client().get_integration(CohereIntegration)
if integration is None:
return f(*args, **kwargs)

with sentry_sdk.start_span(
op=consts.OP.COHERE_EMBEDDINGS_CREATE,
name="Cohere Embedding Creation",
origin=CohereIntegration.origin,
) as span:
integration = sentry_sdk.get_client().get_integration(CohereIntegration)
if "texts" in kwargs and (
should_send_default_pii() and integration.include_prompts
):
Expand Down
9 changes: 4 additions & 5 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,10 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
pass


@ensure_integration_enabled(DjangoIntegration)
def _before_get_response(request):
# type: (WSGIRequest) -> None
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
if integration is None:
return

_patch_drf()

Expand All @@ -441,10 +440,11 @@ def _attempt_resolve_again(request, scope, transaction_style):
_set_transaction_name_and_source(scope, transaction_style, request)


@ensure_integration_enabled(DjangoIntegration)
def _after_get_response(request):
# type: (WSGIRequest) -> None
integration = sentry_sdk.get_client().get_integration(DjangoIntegration)
if integration is None or integration.transaction_style != "url":
if integration.transaction_style != "url":
return

scope = sentry_sdk.get_current_scope()
Expand Down Expand Up @@ -510,12 +510,11 @@ def wsgi_request_event_processor(event, hint):
return wsgi_request_event_processor


@ensure_integration_enabled(DjangoIntegration)
def _got_request_exception(request=None, **kwargs):
# type: (WSGIRequest, **Any) -> None
client = sentry_sdk.get_client()
integration = client.get_integration(DjangoIntegration)
if integration is None:
return

if request is not None and integration.transaction_style == "url":
scope = sentry_sdk.get_current_scope()
Expand Down

0 comments on commit 6d5d714

Please sign in to comment.