Skip to content

Commit

Permalink
perf: use orjson in all middlewares (#70456)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed May 8, 2024
1 parent 4623b5d commit 7512ed6
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/sentry/middleware/health.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools

import orjson
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin
from rest_framework.request import Request
Expand All @@ -20,7 +21,6 @@ def process_request(self, request: Request):
return HttpResponse("ok", content_type="text/plain")

from sentry.status_checks import Problem, check_all
from sentry.utils import json

threshold = Problem.threshold(Problem.SEVERITY_CRITICAL)
results = {
Expand All @@ -29,7 +29,7 @@ def process_request(self, request: Request):
problems = list(itertools.chain.from_iterable(results.values()))

return HttpResponse(
json.dumps(
orjson.dumps(
{
"problems": [str(p) for p in problems],
"healthy": {type(check).__name__: not p for check, p in results.items()},
Expand Down
10 changes: 5 additions & 5 deletions src/sentry/middleware/integrations/parsers/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Mapping
from typing import Any

import orjson
from django.http import HttpResponse

from sentry.integrations.github.webhook import (
Expand All @@ -16,7 +17,6 @@
from sentry.models.outbox import WebhookProviderIdentifier
from sentry.services.hybrid_cloud.util import control_silo_function
from sentry.types.integrations import EXTERNAL_PROVIDERS, ExternalProviders
from sentry.utils import json

logger = logging.getLogger(__name__)

Expand All @@ -36,8 +36,8 @@ def get_integration_from_request(self) -> Integration | None:
if not self.is_json_request():
return None
try:
event = json.loads(self.request.body.decode(encoding="utf-8"))
except json.JSONDecodeError:
event = orjson.loads(self.request.body)
except orjson.JSONDecodeError:
return None
external_id = self._get_external_id(event=event)
if not external_id:
Expand All @@ -49,8 +49,8 @@ def get_response(self):
return self.get_response_from_control_silo()

try:
event = json.loads(self.request.body.decode(encoding="utf-8"))
except json.JSONDecodeError:
event = orjson.loads(self.request.body)
except orjson.JSONDecodeError:
return HttpResponse(status=400)

if event.get("installation") and event.get("action") in {"created", "deleted"}:
Expand Down
7 changes: 4 additions & 3 deletions src/sentry/middleware/integrations/parsers/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Mapping
from typing import Any

import orjson
from django.http.response import HttpResponseBase
from django.urls import resolve

Expand All @@ -16,7 +17,7 @@
from sentry.models.outbox import WebhookProviderIdentifier
from sentry.services.hybrid_cloud.util import control_silo_function
from sentry.types.integrations import EXTERNAL_PROVIDERS, ExternalProviders
from sentry.utils import json, metrics
from sentry.utils import metrics

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -87,8 +88,8 @@ def get_response_from_gitlab_webhook(self):
return self.get_default_missing_integration_response()

try:
data = json.loads(self.request.body)
except ValueError:
data = orjson.loads(self.request.body)
except orjson.JSONDecodeError:
data = {}

return self.get_response_from_webhookpayload(
Expand Down
6 changes: 3 additions & 3 deletions src/sentry/middleware/integrations/parsers/jira_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Mapping
from typing import Any

import orjson
from django.http import HttpResponse

from sentry.integrations.jira_server.webhooks import (
Expand All @@ -12,7 +13,6 @@
)
from sentry.middleware.integrations.parsers.base import BaseRequestParser
from sentry.models.outbox import WebhookProviderIdentifier
from sentry.utils import json

logger = logging.getLogger(__name__)

Expand All @@ -33,8 +33,8 @@ def get_response_from_issue_update_webhook(self):
regions = self.get_regions_from_organizations(organizations=organizations)

try:
data = json.loads(self.request.body)
except ValueError:
data = orjson.loads(self.request.body)
except orjson.JSONDecodeError:
data = {}

# We only process webhooks with changelogs
Expand Down
6 changes: 3 additions & 3 deletions src/sentry/middleware/integrations/parsers/msteams.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from functools import cached_property
from typing import Any

import orjson
import sentry_sdk
from django.http.response import HttpResponseBase

Expand All @@ -20,7 +21,6 @@
from sentry.services.hybrid_cloud.util import control_silo_function
from sentry.types.integrations import EXTERNAL_PROVIDERS, ExternalProviders
from sentry.types.region import Region, RegionResolutionError
from sentry.utils import json

logger = logging.getLogger(__name__)

Expand All @@ -37,8 +37,8 @@ class MsTeamsRequestParser(BaseRequestParser, MsTeamsWebhookMixin):
def request_data(self):
data = {}
try:
data = json.loads(self.request.body.decode(encoding="utf-8"))
except Exception as err:
data = orjson.loads(self.request.body)
except orjson.JSONDecodeError as err:
sentry_sdk.capture_exception(err)
return data

Expand Down
6 changes: 3 additions & 3 deletions src/sentry/middleware/integrations/parsers/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from collections.abc import Sequence

import orjson
import sentry_sdk
from django.http.response import HttpResponse, HttpResponseBase
from rest_framework import status
Expand All @@ -29,7 +30,6 @@
from sentry.models.outbox import WebhookProviderIdentifier
from sentry.types.integrations import EXTERNAL_PROVIDERS, ExternalProviders
from sentry.types.region import Region
from sentry.utils import json
from sentry.utils.signing import unsign

from .base import BaseRequestParser, create_async_request_payload
Expand Down Expand Up @@ -127,8 +127,8 @@ def get_response(self):
# Handle event interactions challenge request
data = None
try:
data = json.loads(self.request.body.decode(encoding="utf-8"))
except Exception:
data = orjson.loads(self.request.body)
except orjson.JSONDecodeError:
pass
if data and is_event_challenge(data):
return self.get_response_from_control_silo()
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/middleware/integrations/parsers/vsts.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import logging

import orjson
import sentry_sdk
from django.http.response import HttpResponseBase

Expand All @@ -11,7 +12,6 @@
from sentry.models.integrations.organization_integration import OrganizationIntegration
from sentry.models.outbox import WebhookProviderIdentifier
from sentry.services.hybrid_cloud.util import control_silo_function
from sentry.utils import json

logger = logging.getLogger(__name__)

Expand All @@ -25,7 +25,7 @@ class VstsRequestParser(BaseRequestParser):
@control_silo_function
def get_integration_from_request(self) -> Integration | None:
try:
data = json.loads(self.request.body.decode(encoding="utf-8"))
data = orjson.loads(self.request.body)
external_id = get_vsts_external_id(data=data)
except Exception as e:
sentry_sdk.capture_exception(e)
Expand Down
8 changes: 3 additions & 5 deletions src/sentry/middleware/integrations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from collections.abc import MutableMapping
from typing import Any, cast

import orjson
import requests
import sentry_sdk
from requests import Response
Expand All @@ -11,7 +12,6 @@
from sentry.silo.client import RegionSiloClient
from sentry.tasks.base import instrumented_task
from sentry.types.region import get_region_by_name
from sentry.utils import json

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -73,7 +73,7 @@ def convert_to_async_slack_response(

response_payload = {}
try:
response_payload = json.loads(response_body.decode(encoding="utf-8"))
response_payload = orjson.loads(response_body)
except Exception as exc:
sentry_sdk.capture_exception(exc)

Expand Down Expand Up @@ -145,9 +145,7 @@ def convert_to_async_discord_response(
# handling the request asynchronously, we extract only the data, and post it to the webhook
# that discord provides.
# https://discord.com/developers/docs/interactions/receiving-and-responding#followup-messages
response_payload = json.loads(result["response"].content.decode(encoding="utf-8")).get(
"data"
)
response_payload = orjson.loads(result["response"].content).get("data")
except Exception as e:
sentry_sdk.capture_exception(e)
integration_response = requests.post(response_url, json=response_payload)
Expand Down
5 changes: 3 additions & 2 deletions src/sentry/middleware/ratelimit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import uuid
from collections.abc import Callable

import orjson
from django.conf import settings
from django.http.request import HttpRequest
from django.http.response import HttpResponse, HttpResponseBase
Expand All @@ -18,7 +19,7 @@
)
from sentry.ratelimits.config import RateLimitConfig
from sentry.types.ratelimit import RateLimitCategory, RateLimitMeta, RateLimitType
from sentry.utils import json, metrics
from sentry.utils import metrics

DEFAULT_ERROR_MESSAGE = (
"You are attempting to use this endpoint too frequently. Limit is "
Expand Down Expand Up @@ -106,7 +107,7 @@ def process_view(
},
)
response = HttpResponse(
json.dumps(
orjson.dumps(
DEFAULT_ERROR_MESSAGE.format(
limit=request.rate_limit_metadata.limit,
window=request.rate_limit_metadata.window,
Expand Down

0 comments on commit 7512ed6

Please sign in to comment.